Class: MCP::Client::Stdio

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/client/stdio.rb

Constant Summary collapse

CLOSE_TIMEOUT =

Seconds to wait for the server process to exit before sending SIGTERM. Matches the Python and TypeScript SDKs’ shutdown timeout: github.com/modelcontextprotocol/python-sdk/blob/v1.26.0/src/mcp/client/stdio/__init__.py#L48 github.com/modelcontextprotocol/typescript-sdk/blob/v1.27.1/src/client/stdio.ts#L221

2
STDERR_READ_SIZE =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, args: [], env: nil, read_timeout: nil) ⇒ Stdio

Returns a new instance of Stdio.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mcp/client/stdio.rb', line 24

def initialize(command:, args: [], env: nil, read_timeout: nil)
  @command = command
  @args = args
  @env = env
  @read_timeout = read_timeout
  @stdin = nil
  @stdout = nil
  @stderr = nil
  @wait_thread = nil
  @stderr_thread = nil
  @started = false
  @initialized = false
  @server_info = nil
  # Serializes writes to `@stdin` so a request line and a notification line emitted from
  # different threads (e.g. cancellation) cannot interleave on the wire.
  @write_mutex = Mutex.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



22
23
24
# File 'lib/mcp/client/stdio.rb', line 22

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



22
23
24
# File 'lib/mcp/client/stdio.rb', line 22

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/mcp/client/stdio.rb', line 22

def env
  @env
end

#server_infoObject (readonly)

Returns the value of attribute server_info.



22
23
24
# File 'lib/mcp/client/stdio.rb', line 22

def server_info
  @server_info
end

Instance Method Details

#closeObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mcp/client/stdio.rb', line 194

def close
  return unless @started

  @stdin.close
  @stdout.close
  @stderr.close

  begin
    Timeout.timeout(CLOSE_TIMEOUT) { @wait_thread.value }
  rescue Timeout::Error
    begin
      Process.kill("TERM", @wait_thread.pid)
      Timeout.timeout(CLOSE_TIMEOUT) { @wait_thread.value }
    rescue Timeout::Error
      begin
        Process.kill("KILL", @wait_thread.pid)
      rescue Errno::ESRCH
        nil
      end
    rescue Errno::ESRCH
      nil
    end
  end

  @stderr_thread.join(CLOSE_TIMEOUT)
  @started = false
  @initialized = false
  @server_info = nil
end

#connect(client_info: nil, protocol_version: nil, capabilities: {}) ⇒ Hash

Performs the MCP ‘initialize` handshake: sends an `initialize` request followed by the required `notifications/initialized` notification. The server’s ‘InitializeResult` (protocol version, capabilities, server info, instructions) is cached on the transport and returned.

Idempotent: a second call returns the cached ‘InitializeResult` without contacting the server. After `close`, state is cleared and `connect` will handshake again. Spawns the subprocess via `start` if it has not been started yet.

modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#initialization

Parameters:

  • client_info (Hash, nil) (defaults to: nil)

    ‘{ name:, version: }` identifying the client. Defaults to `{ name: “mcp-ruby-client”, version: MCP::VERSION }`.

  • protocol_version (String, nil) (defaults to: nil)

    Protocol version to offer. Defaults to ‘MCP::Configuration::LATEST_STABLE_PROTOCOL_VERSION`.

  • capabilities (Hash) (defaults to: {})

    Capabilities advertised by the client. Defaults to ‘{}`.

Returns:

  • (Hash)

    The server’s ‘InitializeResult`.

Raises:

  • (RequestHandlerError)

    If the server responds with a JSON-RPC error, a malformed result, or an unsupported protocol version.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mcp/client/stdio.rb', line 61

def connect(client_info: nil, protocol_version: nil, capabilities: {})
  return @server_info if @initialized

  start unless @started

  client_info ||= { name: "mcp-ruby-client", version: MCP::VERSION }
  protocol_version ||= MCP::Configuration::LATEST_STABLE_PROTOCOL_VERSION

  init_request = {
    jsonrpc: JsonRpcHandler::Version::V2_0,
    id: SecureRandom.uuid,
    method: MCP::Methods::INITIALIZE,
    params: {
      protocolVersion: protocol_version,
      capabilities: capabilities,
      clientInfo: client_info,
    },
  }

  write_message(init_request)
  response = read_response(init_request)

  if response.key?("error")
    error = response["error"]
    raise RequestHandlerError.new(
      "Server initialization failed: #{error["message"]}",
      { method: MCP::Methods::INITIALIZE },
      error_type: :internal_error,
    )
  end

  unless response["result"].is_a?(Hash)
    raise RequestHandlerError.new(
      "Server initialization failed: missing result in response",
      { method: MCP::Methods::INITIALIZE },
      error_type: :internal_error,
    )
  end

  @server_info = response["result"]

  negotiated_protocol_version = @server_info["protocolVersion"]
  unless MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.include?(negotiated_protocol_version)
    # Per spec, if the client does not support the server's returned protocol version,
    # the client SHOULD disconnect. Roll back the cached `InitializeResult` before
    # raising so a retry starts without a stale `server_info`.
    # https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#version-negotiation
    @server_info = nil
    raise RequestHandlerError.new(
      "Server initialization failed: unsupported protocol version #{negotiated_protocol_version.inspect}",
      { method: MCP::Methods::INITIALIZE },
      error_type: :internal_error,
    )
  end

  begin
    notification = {
      jsonrpc: JsonRpcHandler::Version::V2_0,
      method: MCP::Methods::NOTIFICATIONS_INITIALIZED,
    }
    write_message(notification)
  rescue StandardError
    @server_info = nil
    raise
  end

  @initialized = true
  @server_info
end

#connected?Boolean

Returns true once ‘connect` (or the implicit handshake on the first `send_request`) has completed. Returns false before the handshake and after `close`.

Returns:

  • (Boolean)


134
135
136
# File 'lib/mcp/client/stdio.rb', line 134

def connected?
  @initialized
end

#send_notification(notification:) ⇒ Object

Sends a JSON-RPC notification (no response expected). Used by ‘Client#cancel` to deliver `notifications/cancelled` for an in-flight request.



158
159
160
161
162
163
164
# File 'lib/mcp/client/stdio.rb', line 158

def send_notification(notification:)
  start unless @started
  connect unless @initialized

  @write_mutex.synchronize { write_message(notification) }
  nil
end

#send_request(request:) ⇒ Object

Transports may yield once the request line has been written to ‘@stdin`. `MCP::Client#dispatch_with_cancellation` uses this signal to ensure a `notifications/cancelled` write does not race ahead of the request write on the wire. The yield happens inside `@write_mutex`, so any subsequent `send_notification` write waits for the mutex and is guaranteed to land after the request.



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mcp/client/stdio.rb', line 142

def send_request(request:)
  start unless @started
  unless @initialized
    warn("Calling `MCP::Client::Stdio#send_request` without calling `MCP::Client#connect` is deprecated. Use `MCP::Client#connect` before sending requests instead.", uplevel: 1)
    connect
  end

  @write_mutex.synchronize do
    write_message(request)
    yield if block_given?
  end
  read_response(request)
end

#startObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mcp/client/stdio.rb', line 166

def start
  raise "MCP::Client::Stdio already started" if @started

  spawn_env = @env || {}
  @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(spawn_env, @command, *@args)
  @stdout.set_encoding("UTF-8")
  @stdin.set_encoding("UTF-8")

  # Drain stderr in the background to prevent the pipe buffer from filling up,
  # which would cause the server process to block and deadlock.
  @stderr_thread = Thread.new do
    loop do
      @stderr.readpartial(STDERR_READ_SIZE)
    end
  rescue IOError
    nil
  end

  @started = true
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOEXEC => e
  raise RequestHandlerError.new(
    "Failed to spawn server process: #{e.message}",
    {},
    error_type: :internal_error,
    original_error: e,
  )
end