Class: LLM::MCP::Transport::Stdio
- Inherits:
-
Object
- Object
- LLM::MCP::Transport::Stdio
- Defined in:
- lib/llm/mcp/transport/stdio.rb
Overview
The LLM::MCP::Transport::Stdio class provides a stdio transport for LLM::MCP. It sends JSON-RPC messages to an MCP process over stdin and stdout and delegates process lifecycle management to LLM::MCP::Command.
Instance Method Summary collapse
-
#initialize(command:) ⇒ LLM::MCP::Transport::Stdio
constructor
Returns a new Stdio transport instance.
-
#persist! ⇒ LLM::MCP::Transport::Stdio
(also: #persistent)
This method is a no-op for stdio transports.
-
#read_nonblock ⇒ Hash
Reads a message from the MCP process without blocking.
-
#start ⇒ void
Starts an MCP process over a stdio transport.
-
#stop ⇒ void
Closes the connection to the MCP process.
-
#wait ⇒ void
Waits for the command to exit.
-
#write(message) ⇒ void
Writes a message to the MCP process.
Constructor Details
#initialize(command:) ⇒ LLM::MCP::Transport::Stdio
Returns a new Stdio transport instance.
15 16 17 |
# File 'lib/llm/mcp/transport/stdio.rb', line 15 def initialize(command:) @command = command end |
Instance Method Details
#persist! ⇒ LLM::MCP::Transport::Stdio Also known as: persistent
This method is a no-op for stdio transports
84 85 86 |
# File 'lib/llm/mcp/transport/stdio.rb', line 84 def persist! self end |
#read_nonblock ⇒ Hash
Reads a message from the MCP process without blocking.
64 65 66 67 68 69 70 |
# File 'lib/llm/mcp/transport/stdio.rb', line 64 def read_nonblock if command.alive? LLM.json.load(command.read_nonblock) else raise LLM::MCP::Error, "MCP transport is not running" end end |
#start ⇒ void
This method returns an undefined value.
Starts an MCP process over a stdio transport. This method is non-blocking and returns immediately.
25 26 27 28 29 30 31 |
# File 'lib/llm/mcp/transport/stdio.rb', line 25 def start if command.alive? raise LLM::MCP::Error, "MCP transport is already running" else command.start end end |
#stop ⇒ void
This method returns an undefined value.
Closes the connection to the MCP process. This method is idempotent and can be called multiple times without error.
37 38 39 |
# File 'lib/llm/mcp/transport/stdio.rb', line 37 def stop command.stop end |
#wait ⇒ void
This method returns an undefined value.
Waits for the command to exit. This method is blocking and will return only after the process has exited.
77 78 79 |
# File 'lib/llm/mcp/transport/stdio.rb', line 77 def wait command.wait end |