Skip to content

Commit 648c6ac

Browse files
authored
Merge pull request #628 from koic/skip_a_stdio_ping_whose_id_cannot_be_encoded
Skip a stdio ping whose id cannot be encoded
2 parents a8baa7a + 42e8cd5 commit 648c6ac

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎lib/mcp/client/stdio.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,10 @@ def answerable_ping?(parsed)
484484
parsed["method"] == MCP::Methods::PING && json_rpc_id?(parsed["id"]) && JsonRpcHandler.valid_version?(parsed["jsonrpc"])
485485
end
486486

487+
# An id the pong could not write back is skipped too: a String that is not valid UTF-8, or a number
488+
# such as `1e400`, which the parser reads as an infinite Float that JSON cannot represent.
487489
def json_rpc_id?(id)
488-
id.is_a?(String) || id.is_a?(Numeric)
490+
(id.is_a?(String) && id.valid_encoding?) || id.is_a?(Integer) || (id.is_a?(Float) && id.finite?)
489491
end
490492

491493
def ensure_running!

‎test/mcp/client/stdio_test.rb‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,38 @@ def test_a_ping_with_a_malformed_id_is_not_answered
324324
stdout_write.close
325325
end
326326

327+
def test_a_ping_whose_id_is_not_valid_utf_8_is_not_answered
328+
# Checked on the parsed frame, since how such a String arrives depends on the json gem and the Ruby version
329+
# (a raw byte, or a lone surrogate escape on json 2.x); the pong for it would fail in `JSON.generate`.
330+
transport = Stdio.new(command: "ruby", args: ["server.rb"])
331+
ping = { "jsonrpc" => "2.0", "id" => "p\xFF".dup.force_encoding(Encoding::UTF_8), "method" => "ping" }
332+
333+
refute(transport.send(:answerable_ping?, ping))
334+
assert(transport.send(:answerable_ping?, ping.merge("id" => "p")))
335+
end
336+
337+
def test_a_ping_whose_id_parses_to_an_infinite_float_is_not_answered_and_the_request_completes
338+
written = []
339+
server = lambda do |requests, frames|
340+
answer_handshake(requests, frames)
341+
request = JSON.parse(requests.gets)
342+
# `1e400` is valid JSON that the parser reads as `Float::INFINITY`, which `JSON.generate` refuses.
343+
frames.puts('{"jsonrpc":"2.0","id":1e400,"method":"ping"}')
344+
write_frame(frames, { jsonrpc: "2.0", id: "srv-ping-2", method: "ping" })
345+
# The single pong must answer the second ping; the response follows only after it arrives.
346+
written << JSON.parse(requests.gets)
347+
write_frame(frames, { jsonrpc: "2.0", id: request["id"], result: { tools: [] } })
348+
end
349+
350+
with_scripted_server(server) do |transport|
351+
transport.connect
352+
response = transport.send_request(request: { jsonrpc: "2.0", id: "test-id", method: "tools/list" })
353+
354+
assert_equal("test-id", response["id"])
355+
end
356+
assert_equal([{ "jsonrpc" => "2.0", "id" => "srv-ping-2", "result" => {} }], written)
357+
end
358+
327359
def test_send_request_skips_notifications
328360
stdin_read, stdin_write = IO.pipe
329361
stdout_read, stdout_write = IO.pipe

0 commit comments

Comments
 (0)