From 3616a355d8edee4d140a4bab6ea08b19ca5da1bb Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 30 Nov 2026 01:52:40 +0900 Subject: [PATCH] Treat a `null` or `false` `error` member of a client reply as an error on stdio ## Motivation and Context While the stdio transport waits for the reply to a request it sent to the client, it decides whether the reply is an error by the truthiness of its `error` member. A reply such as `{"jsonrpc":"2.0","id":1,"error":null}` or one with `"error": false` is therefore taken for a success, and `send_request` returns `nil`, the `result` the reply does not have. The Streamable HTTP transport decides by the presence of the member, so the same reply ends the request there with the "Client returned an error" `StandardError`. The stdio transport now decides by the presence of the member too. A reply that carries an `error` member of any value raises that `StandardError`, with an empty code and message when the member is not an object. Follow-up to #716, which made both transports treat an `error` member that is not an object as an error reply. ## How Has This Been Tested? New tests in `test/mcp/server/transports/stdio_transport_test.rb` for an `error` member of `null` and of `false`. Before the change `send_request` returns `nil` for both. Further tests there for a reply that carries `result` together with such a member. Before the change it is returned as a success. ## Breaking Changes None for a client that follows JSON-RPC 2.0. A reply that carries both `result` and an `error` member of `null` or `false`, which JSON-RPC 2.0 does not allow, was returned as a success on stdio and is now an error, as it is on Streamable HTTP. --- lib/mcp/server/transports/stdio_transport.rb | 4 +- .../server/transports/stdio_transport_test.rb | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/lib/mcp/server/transports/stdio_transport.rb b/lib/mcp/server/transports/stdio_transport.rb index b2b2f665..aa84f270 100644 --- a/lib/mcp/server/transports/stdio_transport.rb +++ b/lib/mcp/server/transports/stdio_transport.rb @@ -157,8 +157,8 @@ def send_request(method, params = nil) # A frame that is not an object is not the response; it is answered like any other frame, as `open` does. if parsed.is_a?(Hash) && parsed[:id] == request_id && !parsed.key?(:method) - if (error = parsed[:error]) - error = {} unless error.is_a?(Hash) + if parsed.key?(:error) + error = parsed[:error].is_a?(Hash) ? parsed[:error] : {} raise StandardError, "Client returned an error for #{method} request (code: #{error[:code]}): #{error[:message]}" end diff --git a/test/mcp/server/transports/stdio_transport_test.rb b/test/mcp/server/transports/stdio_transport_test.rb index f302810e..4a86bbbc 100644 --- a/test/mcp/server/transports/stdio_transport_test.rb +++ b/test/mcp/server/transports/stdio_transport_test.rb @@ -499,6 +499,72 @@ class StdioTransportTest < ActiveSupport::TestCase end end + [nil, false].each do |member| + test "send_request raises a client error whose error member is #{member.inspect}" do + reader, writer = IO.pipe + output = StringIO.new + original_stdin = $stdin + original_stdout = $stdout + + begin + $stdin = reader + $stdout = output + @transport.instance_variable_set(:@open, true) + + Thread.new do + sleep(0.05) # Wait for request to be written to `StringIO`. + request = JSON.parse(output.string.lines.first, symbolize_names: true) + writer.puts({ jsonrpc: "2.0", id: request[:id], error: member }.to_json) + writer.flush + end + + error = assert_raises(StandardError) do + @transport.send_request("sampling/createMessage", { messages: [] }) + end + + assert_instance_of(StandardError, error) + assert_equal("Client returned an error for sampling/createMessage request (code: ): ", error.message) + ensure + $stdin = original_stdin + $stdout = original_stdout + writer.close + reader.close + end + end + + test "send_request raises a client error for a reply with a result and an error member that is #{member.inspect}" do + reader, writer = IO.pipe + output = StringIO.new + original_stdin = $stdin + original_stdout = $stdout + + begin + $stdin = reader + $stdout = output + @transport.instance_variable_set(:@open, true) + + Thread.new do + sleep(0.05) # Wait for request to be written to `StringIO`. + request = JSON.parse(output.string.lines.first, symbolize_names: true) + writer.puts({ jsonrpc: "2.0", id: request[:id], result: { content: "test response" }, error: member }.to_json) + writer.flush + end + + error = assert_raises(StandardError) do + @transport.send_request("sampling/createMessage", { messages: [] }) + end + + assert_instance_of(StandardError, error) + assert_equal("Client returned an error for sampling/createMessage request (code: ): ", error.message) + ensure + $stdin = original_stdin + $stdout = original_stdout + writer.close + reader.close + end + end + end + test "send_request reports a client reply whose JSON key is not valid UTF-8 as a parse failure" do reader, writer = IO.pipe