Class: RobotLab::MCP::Transports::SSE
- Defined in:
- lib/robot_lab/mcp/transports/sse.rb
Overview
Server-Sent Events transport for MCP servers
Uses async-http for SSE streaming.
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#close ⇒ self
Close the SSE connection.
-
#connect ⇒ self
Connect to the MCP server via SSE.
-
#connected? ⇒ Boolean
Check if the transport is connected.
-
#initialize(config) ⇒ SSE
constructor
Creates a new SSE transport.
-
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
Constructor Details
#initialize(config) ⇒ SSE
Creates a new SSE transport.
18 19 20 21 22 23 |
# File 'lib/robot_lab/mcp/transports/sse.rb', line 18 def initialize(config) super @client = nil @connected = false @event_queue = [] end |
Instance Method Details
#close ⇒ self
Close the SSE connection.
80 81 82 83 84 85 86 87 88 |
# File 'lib/robot_lab/mcp/transports/sse.rb', line 80 def close return self unless @connected @client&.close @connected = false @client = nil self end |
#connect ⇒ self
Connect to the MCP server via SSE.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/robot_lab/mcp/transports/sse.rb', line 29 def connect return self if @connected require "async" require "async/http/client" require "async/http/endpoint" url = @config[:url] Async do endpoint = Async::HTTP::Endpoint.parse(url) @client = Async::HTTP::Client.new(endpoint) @connected = true # Initialize MCP protocol send_initialize end self rescue LoadError => e raise MCPError, "async-http gem required for SSE transport: #{e.}" end |
#connected? ⇒ Boolean
Check if the transport is connected.
93 94 95 |
# File 'lib/robot_lab/mcp/transports/sse.rb', line 93 def connected? @connected end |
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/robot_lab/mcp/transports/sse.rb', line 57 def send_request() raise MCPError, "Not connected" unless @connected require "async" require "async/http/body/writable" Async do # POST the request response = @client.post( @config[:url], { "Content-Type" => "application/json" }, [.to_json] ) # Read response body = response.read JSON.parse(body, symbolize_names: true) end.wait end |