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 Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
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.
15 16 17 18 19 20 21 22 |
# File 'lib/kward/mcp/stdio_transport.rb', line 15 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 Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
13 14 15 |
# File 'lib/kward/mcp/stdio_transport.rb', line 13 def command @command end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/kward/mcp/stdio_transport.rb', line 40 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
33 34 35 36 37 38 |
# File 'lib/kward/mcp/stdio_transport.rb', line 33 def notify(method, params = nil) @mutex.synchronize do start ({ jsonrpc: "2.0", method: method, params: params }.compact) end end |
#request(method, params = nil) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/kward/mcp/stdio_transport.rb', line 24 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 |