22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/stack-service-base/mcp/mcp_helper.rb', line 22
def self.included(base)
base.class_eval do
error McpProcessor::ParseError do |err|
status err.status
err.body
end
get '/mcp' do
content_type :json
MCP_PROCESSOR.root_endpoint
end
post '/mcp' do
request.body&.rewind
response_body =
begin
MCP_PROCESSOR.rpc_endpoint(request.body.read.to_s)
rescue McpProcessor::ParseError => e
status e.status
e.body
end
if response_body.nil?
status 202
'Content-Length' => '0'
''
elsif McpHelper.transport == :json
content_type :json
response_body
else
content_type 'text/event-stream'
['Cache-Control'] = 'no-cache'
['X-Accel-Buffering'] = 'no'
['mcp-session-id'] = SecureRandom.uuid
stream true do |s|
s.callback { LOGGER.debug "stream closed: #{s}" }
s << "event: message\ndata: #{response_body}\n\n"
s.close
end
end
end
end
end
|