Class: RobotLab::MCP::Transports::WebSocket
- Defined in:
- lib/robot_lab/mcp/transports/websocket.rb
Overview
WebSocket transport for MCP servers
Uses async-websocket for non-blocking communication.
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#close ⇒ self
Close the WebSocket connection.
-
#connect ⇒ self
Connect to the MCP server via WebSocket.
-
#connected? ⇒ Boolean
Check if the transport is connected.
-
#initialize(config) ⇒ WebSocket
constructor
Creates a new WebSocket transport.
-
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
Constructor Details
#initialize(config) ⇒ WebSocket
Creates a new WebSocket transport.
18 19 20 21 22 23 |
# File 'lib/robot_lab/mcp/transports/websocket.rb', line 18 def initialize(config) super @connection = nil @connected = false @pending_requests = {} end |
Instance Method Details
#close ⇒ self
Close the WebSocket connection.
71 72 73 74 75 76 77 78 79 |
# File 'lib/robot_lab/mcp/transports/websocket.rb', line 71 def close return self unless @connected @connection&.close @connected = false @connection = nil self end |
#connect ⇒ self
Connect to the MCP server via WebSocket.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/robot_lab/mcp/transports/websocket.rb', line 29 def connect return self if @connected require "async" require "async/websocket/client" url = @config[:url] Async do endpoint = Async::HTTP::Endpoint.parse(url) @connection = Async::WebSocket::Client.connect(endpoint) @connected = true # Initialize MCP protocol send_initialize end self rescue LoadError => e raise MCPError, "async-websocket gem required for WebSocket transport: #{e.}" end |
#connected? ⇒ Boolean
Check if the transport is connected.
84 85 86 |
# File 'lib/robot_lab/mcp/transports/websocket.rb', line 84 def connected? @connected end |
#send_request(message) ⇒ Hash
Send a JSON-RPC request to the MCP server.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/robot_lab/mcp/transports/websocket.rb', line 56 def send_request() raise MCPError, "Not connected" unless @connected Async do @connection.write(.to_json) @connection.flush response_text = @connection.read JSON.parse(response_text, symbolize_names: true) end.wait end |