Class: Kward::MCP::StdioTransport
- Inherits:
-
Object
- Object
- Kward::MCP::StdioTransport
- Defined in:
- lib/kward/mcp/stdio_transport.rb
Overview
JSON-RPC transport for local MCP servers that communicate over stdio.
Constant Summary collapse
- DEFAULT_TIMEOUT_SECONDS =
10
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(command:, args: [], env: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS) ⇒ StdioTransport
constructor
A new instance of StdioTransport.
- #notify(method, params = nil) ⇒ Object
- #request(method, params = nil) ⇒ Object
Constructor Details
#initialize(command:, args: [], env: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS) ⇒ StdioTransport
Returns a new instance of StdioTransport.
13 14 15 16 17 18 19 20 |
# File 'lib/kward/mcp/stdio_transport.rb', line 13 def initialize(command:, args: [], env: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS) @command = command.to_s @args = Array(args).map(&:to_s) @env = env || {} @timeout_seconds = timeout_seconds || DEFAULT_TIMEOUT_SECONDS @mutex = Mutex.new @started = false end |
Instance Method Details
#close ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/kward/mcp/stdio_transport.rb', line 38 def close @stdin&.close unless @stdin&.closed? @stdout&.close unless @stdout&.closed? @stderr&.close unless @stderr&.closed? @stderr_thread&.kill terminate_process rescue IOError nil end |
#notify(method, params = nil) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/kward/mcp/stdio_transport.rb', line 31 def notify(method, params = nil) @mutex.synchronize do start ({ jsonrpc: "2.0", method: method, params: params }.compact) end end |
#request(method, params = nil) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/kward/mcp/stdio_transport.rb', line 22 def request(method, params = nil) @mutex.synchronize do start id = next_id ({ jsonrpc: "2.0", id: id, method: method, params: params }.compact) read_response(id) end end |