Skip to content

fix: answer a server request whose client reply is malformed - #716

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/malformed-client-response-handling
Nov 30, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/malformed-client-response-handling

Conversation

@yorzi

@yorzi yorzi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A client reply with a malformed error now 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 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 (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 the send_request waiting for it only returns once its timeout passes (600 seconds by default) with RequestTimeoutError:

POST status: 500
send_request: MCP::Server::RequestTimeoutError: sampling/createMessage request timed out after 2 seconds

On stdio, send_request reads the client's frames itself while it waits (lib/mcp/server/transports/stdio_transport.rb:158). 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 waiting on. 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 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 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.
  • 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. bundle exec rake test, bundle exec rake rubocop, bundle exec yard --no-output and bundle exec rake conformance:test (baseline) pass locally on Ruby 4.0.9.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed (none needed)

Additional context

The message for a malformed error reads (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.

## 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.
@koic
koic merged commit 8659de7 into modelcontextprotocol:main Nov 30, 2026
13 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A malformed client reply to a server request gives 500 and leaves the request waiting for its timeout

2 participants