Class: MCP::Server::Transports::StreamableHTTPTransport
- Defined in:
- lib/mcp/server/transports/streamable_http_transport.rb
Instance Method Summary collapse
- #close ⇒ Object
- #handle_request(request) ⇒ Object
-
#initialize(server) ⇒ StreamableHTTPTransport
constructor
A new instance of StreamableHTTPTransport.
- #send_notification(method, params = nil, session_id: nil) ⇒ Object
Methods inherited from Transport
#handle_json_request, #open, #send_response
Constructor Details
#initialize(server) ⇒ StreamableHTTPTransport
11 12 13 14 15 16 |
# File 'lib/mcp/server/transports/streamable_http_transport.rb', line 11 def initialize(server) super # { session_id => { stream: stream_object } @sessions = {} @mutex = Mutex.new end |
Instance Method Details
#close ⇒ Object
31 32 33 34 35 |
# File 'lib/mcp/server/transports/streamable_http_transport.rb', line 31 def close @mutex.synchronize do @sessions.each_key { |session_id| cleanup_session_unsafe(session_id) } end end |
#handle_request(request) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mcp/server/transports/streamable_http_transport.rb', line 18 def handle_request(request) case request.env["REQUEST_METHOD"] when "POST" handle_post(request) when "GET" handle_get(request) when "DELETE" handle_delete(request) else [405, { "Content-Type" => "application/json" }, [{ error: "Method not allowed" }.to_json]] end end |
#send_notification(method, params = nil, session_id: nil) ⇒ Object
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mcp/server/transports/streamable_http_transport.rb', line 37 def send_notification(method, params = nil, session_id: nil) notification = { jsonrpc: "2.0", method:, } notification[:params] = params if params @mutex.synchronize do if session_id # Send to specific session session = @sessions[session_id] return false unless session && session[:stream] begin send_to_stream(session[:stream], notification) true rescue IOError, Errno::EPIPE => e MCP.configuration.exception_reporter.call( e, { session_id: session_id, error: "Failed to send notification" }, ) cleanup_session_unsafe(session_id) false end else # Broadcast to all connected SSE sessions sent_count = 0 failed_sessions = [] @sessions.each do |sid, session| next unless session[:stream] begin send_to_stream(session[:stream], notification) sent_count += 1 rescue IOError, Errno::EPIPE => e MCP.configuration.exception_reporter.call( e, { session_id: sid, error: "Failed to send notification" }, ) failed_sessions << sid end end # Clean up failed sessions failed_sessions.each { |sid| cleanup_session_unsafe(sid) } sent_count end end end |