Class: RobotLab::MCP::Transports::StreamableHTTP
- Defined in:
- lib/robot_lab/mcp/transports/streamable_http.rb
Overview
Streamable HTTP transport for MCP servers
Supports session management and reconnection.
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#close ⇒ self
Close the HTTP connection.
-
#connect ⇒ self
Connect to the MCP server via HTTP.
-
#connected? ⇒ Boolean
Check if the transport is connected.
-
#initialize(config) ⇒ StreamableHTTP
constructor
Creates a new StreamableHTTP transport.
-
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
-
#session_id ⇒ String?
Returns the session identifier.
Constructor Details
#initialize(config) ⇒ StreamableHTTP
Creates a new StreamableHTTP transport.
23 24 25 26 27 28 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 23 def initialize(config) super @client = nil @connected = false @session_id = config[:session_id] end |
Instance Method Details
#close ⇒ self
Close the HTTP connection.
95 96 97 98 99 100 101 102 103 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 95 def close return self unless @connected @client&.close @connected = false @client = nil self end |
#connect ⇒ self
Connect to the MCP server via HTTP.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 34 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 result = send_initialize @session_id ||= result.dig(:serverInfo, :sessionId) end self rescue LoadError => e raise MCPError, "async-http gem required for HTTP transport: #{e.}" end |
#connected? ⇒ Boolean
Check if the transport is connected.
108 109 110 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 108 def connected? @connected end |
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
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 88 89 90 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 63 def send_request() raise MCPError, "Not connected" unless @connected require "async" Async do headers = { "Content-Type" => "application/json", "Accept" => "application/json" } headers["X-Session-ID"] = @session_id if @session_id # Add auth if configured if @config[:auth_provider] auth_header = @config[:auth_provider].call headers["Authorization"] = auth_header if auth_header end response = @client.post( @config[:url], headers, [.to_json] ) body = response.read JSON.parse(body, symbolize_names: true) end.wait end |
#session_id ⇒ String?
Returns the session identifier.
115 116 117 |
# File 'lib/robot_lab/mcp/transports/streamable_http.rb', line 115 def session_id @session_id end |