Class: LLM::MCP::Transport::Stdio

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(command:) ⇒ LLM::MCP::Transport::Stdio

Returns a new Stdio transport instance.

Parameters:



15
16
17
# File 'lib/llm/mcp/transport/stdio.rb', line 15

def initialize(command:)
  @command = command
end

Instance Method Details

#write(message) ⇒ void

This method returns an undefined value.

Writes a message to the MCP process.

Parameters:

  • message (Hash)

    The message to write

Raises:

  • (LLM::Error)

    When the transport is not running



48
49
50
51
52
53
54
# File 'lib/llm/mcp/transport/stdio.rb', line 48

def write(message)
  if command.alive?
    command.write(LLM.json.dump(message))
  else
    raise LLM::MCP::Error, "MCP transport is not running"
  end
end

#startvoid

This method returns an undefined value.

Starts an MCP process over a stdio transport. This method is non-blocking and returns immediately.

Raises:

  • (LLM::Error)

    When the transport is already running



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

#stopvoid

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

#read_nonblockHash

Reads a message from the MCP process without blocking.

Returns:

  • (Hash)

    The next message from the MCP process

Raises:

  • (LLM::Error)

    When the transport is not running

  • (IO::WaitReadable)

    When no complete message is available to read



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

#waitvoid

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

#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