Class: SwarmSDK::V3::MCP::StdioTransport
- Inherits:
-
Object
- Object
- SwarmSDK::V3::MCP::StdioTransport
- Defined in:
- lib/swarm_sdk/v3/mcp/stdio_transport.rb
Overview
Subprocess-based JSON-RPC transport for MCP stdio servers
Spawns a child process and communicates via JSON-RPC over stdin/stdout. Performs the MCP protocol initialization handshake automatically. Implements the same duck type as ‘MCP::Client::HTTP` (`send_request(request:)`).
Constant Summary collapse
- PROTOCOL_VERSION =
"2024-11-05"
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Whether the subprocess is still running.
-
#close ⇒ void
Gracefully shut down the subprocess.
-
#initialize(command:, args: [], env: {}) ⇒ StdioTransport
constructor
Create a new stdio transport, spawn the subprocess, and initialize the MCP session.
-
#send_request(request:) ⇒ Hash
Send a JSON-RPC request and return the response.
Constructor Details
#initialize(command:, args: [], env: {}) ⇒ StdioTransport
Create a new stdio transport, spawn the subprocess, and initialize the MCP session
32 33 34 35 36 37 |
# File 'lib/swarm_sdk/v3/mcp/stdio_transport.rb', line 32 def initialize(command:, args: [], env: {}) @stdin, @stdout, @wait_thread = Open3.popen2( env.transform_keys(&:to_s), command, *args ) initialize_session end |
Instance Method Details
#alive? ⇒ Boolean
Whether the subprocess is still running
82 83 84 |
# File 'lib/swarm_sdk/v3/mcp/stdio_transport.rb', line 82 def alive? @wait_thread&.alive? || false end |
#close ⇒ void
This method returns an undefined value.
Gracefully shut down the subprocess
Closes stdin to signal the subprocess to exit, then waits briefly before sending SIGTERM if still alive.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/swarm_sdk/v3/mcp/stdio_transport.rb', line 65 def close @stdin&.close unless @stdin&.closed? return unless @wait_thread # Give the process a moment to exit gracefully return if @wait_thread.join(2) # Force terminate if still running Process.kill("TERM", @wait_thread.pid) if @wait_thread.alive? @wait_thread.join(2) rescue Errno::ESRCH, Errno::ECHILD # Process already exited end |
#send_request(request:) ⇒ Hash
Send a JSON-RPC request and return the response
Writes the request as a single JSON line to the subprocess stdin, then reads lines from stdout until a response with an “id” field is received (skipping server-initiated notifications).
53 54 55 56 57 |
# File 'lib/swarm_sdk/v3/mcp/stdio_transport.rb', line 53 def send_request(request:) @stdin.puts(JSON.generate(request)) @stdin.flush read_response end |