Repository navigation
fix: answer a server request whose client reply is malformed - #716
Merged
koic merged 1 commit intoNov 30, 2026
Merged
Conversation
## Motivation and Context When the server waits on a request it sent to the client (`ping`, `sampling/createMessage`, `elicitation/create`, `roots/list`), both server transports assume the client's reply is a JSON object whose `error` member, when present, is an object too. On Streamable HTTP, a reply whose `error` is not an object (`"bad"`, `null`, `[]`, `123`) raises inside `handle_response`. The POST carrying it is answered with 500 Internal server error, and nothing is pushed to the pending request's queue, so the `send_request` waiting for it only returns once its timeout passes (600 seconds by default) with `RequestTimeoutError`. On stdio, `send_request` reads the client's frames itself while it waits. A frame that is not an object (`[]`) raises `TypeError` there, ending the wait with that error instead of answering the frame with Invalid Request, as the `open` loop does, and keeping to wait for the response. A reply whose `error` is an array, string or number raises `TypeError` instead of the client-error `StandardError` a well-formed error gives. Both transports now treat a reply with a malformed `error` as an error reply without code or message, so `send_request` raises its usual "Client returned an error" `StandardError` at once and the HTTP POST is accepted with 202. On stdio, a frame that is not an object is no longer taken for the response; it goes to the same dispatch as any other frame received while waiting. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb`: replies whose `error` is `"bad"`, `null`, `[]` and `123` are each accepted with 202 and end the pending `send_request` with the client-error `StandardError`. Before the change the first POST got 500 and the waiting request ended with `RequestTimeoutError` after its timeout. New tests in `test/mcp/server/transports/stdio_transport_test.rb`: a reply whose `error` is an array raises the client-error `StandardError` (it raised `TypeError`), and a `[]` frame received while waiting is answered with Invalid Request and the response that follows it is returned (it raised `TypeError`). Reverting any one of the three guards fails one of these tests. ## Breaking Changes None. Fixes modelcontextprotocol#715.
7 of 16 tasks
7 of 13 tasks
koic
added a commit
that referenced
this pull request
Nov 30, 2026
…r 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A client reply with a malformed
errornow ends the server's waiting request at once instead of giving 500 and a wait until the timeout, and a non-object frame received on stdio while waiting no longer ends the wait.Fixes #715
Motivation and Context
When the server waits on a request it sent to the client (
ping,sampling/createMessage,elicitation/create,roots/list), both server transports assume the client's reply is a JSON object whoseerrormember, when present, is an object too.On Streamable HTTP, a reply whose
erroris not an object ("bad",null,[],123) raises insidehandle_response(lib/mcp/server/transports/streamable_http_transport.rb:2390). The POST carrying it is answered with 500, and nothing is pushed to the pending request's queue, so thesend_requestwaiting for it only returns once its timeout passes (600 seconds by default) withRequestTimeoutError:On stdio,
send_requestreads the client's frames itself while it waits (lib/mcp/server/transports/stdio_transport.rb:158). A frame that is not an object ([]) raisesTypeErrorthere, ending the wait with that error instead of answering the frame with Invalid Request, as theopenloop does, and waiting on. A reply whoseerroris an array, string or number raisesTypeErrorinstead of the client-errorStandardErrora well-formed error gives.Both transports now treat a reply with a malformed
erroras an error reply without code or message, sosend_requestraises its usualClient returned an error for <method> request (code: ):at once, and the HTTP POST is accepted with 202. On stdio, a frame that is not an object is no longer taken for the response; it goes to the same dispatch as any other frame received while waiting.How Has This Been Tested?
test/mcp/server/transports/streamable_http_transport_test.rb: replies whoseerroris"bad",null,[]and123are each accepted with 202 and end the pendingsend_requestwith the client-errorStandardError. Before the change the first POST got 500, and the waiting request ended withRequestTimeoutErrorafter its timeout.test/mcp/server/transports/stdio_transport_test.rb: a reply whoseerroris an array raises the client-errorStandardError(it raisedTypeError), and a[]frame received while waiting is answered with Invalid Request and the response that follows it is returned (it raisedTypeError).Reverting any one of the three guards fails one of these tests.
bundle exec rake test,bundle exec rake rubocop,bundle exec yard --no-outputandbundle exec rake conformance:test(baseline) pass locally on Ruby 4.0.9.Breaking Changes
None.
Types of changes
Checklist
Additional context
The message for a malformed
errorreads(code: ):because the reply carries neither; it keeps the one format both transports already raise. A different wording is easy to change if you prefer one.