Skip to content

fix: check the ids of SSE messages in the HTTP client - #718

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/http-client-sse-id-validation
Nov 29, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/http-client-sse-id-validation

Conversation

@yorzi

@yorzi yorzi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

MCP::Client::HTTP no longer returns another request's response from a POST's SSE stream, and a server request whose id it cannot write back no longer fails the request being sent.

Fixes #717

Motivation and Context

MCP::Client::HTTP reads the SSE stream a POST is answered with through SSEStream, which has two gaps that MCP::Client::Stdio does not have.

It takes the first event carrying result or error as the response, whatever its id (lib/mcp/client/http.rb:216). A response to another request on the stream is returned as the answer to the request being sent, and the stream is closed before the real response arrives. Client::Stdio only returns a response whose id is the awaited one.

It answers a server-to-client request on the stream unless its integer id is beyond 128 bits (:222). An id such as 1e400, which the JSON parser reads as Infinity, passes that check, and writing the answer raises JSON::GeneratorError out of send_request, so the request being sent fails because of a message the client could never answer. Client::Stdio already skips a ping whose id cannot be written back (#628).

SSEStream now gets the id of the request being sent and skips a response that carries a different id. A response carrying no id is still taken: it is how a server refuses a request whose id it could not read, and the POST stream answers only that request. Server-to-client requests whose id is a non-finite number are dropped unanswered, like those with an oversized integer id. A request with an id that is malformed but can be written back (an object, for example) is still answered with Method not found, as test_a_ping_with_a_malformed_id_is_answered_with_method_not_found requires. The standalone GET listener passes no request id and is unchanged.

How Has This Been Tested?

New tests in test/mcp/client/http_test.rb:

  • A response carrying another request's id is skipped and the request's own response is returned (the other one was returned before).
  • ping and elicitation/create requests with the ids 1e400 and -1e400 are not answered, and the request being sent still gets its response (it raised JSON::GeneratorError before).
  • An error response carrying no id is still returned. This one passed before too; it guards the id-less case.

The SSE fixture of test_connect_raises_on_an_initialize_response_that_is_not_json_rpc_2_0 answered initialize with the id "1", while the client sends a UUID. The fixture now carries no id, so the test still checks the missing jsonrpc member rather than the id.

Not passing the request id, dropping the id check, also skipping responses carrying no id, or skipping only oversized integer ids each 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 for a server that answers each request with its own id.

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)

## Motivation and Context

`MCP::Client::HTTP` reads the SSE stream a POST is answered with through `SSEStream`, which has two gaps that
`MCP::Client::Stdio` does not have.

It takes the first event carrying `result` or `error` as the response, whatever its `id`. A response to another
request on the stream is returned as the answer to the request being sent, and the stream is closed before the real
response arrives. `Client::Stdio` only returns a response whose `id` is the awaited one.

It answers a server-to-client request on the stream unless its integer id is beyond 128 bits. An id such as `1e400`,
which the JSON parser reads as `Infinity`, passes that check, and writing the answer raises `JSON::GeneratorError` out
of `send_request`, so the request being sent fails because of a message the client could never answer.
`Client::Stdio` already skips a ping whose id cannot be written back (modelcontextprotocol#628).

`SSEStream` now gets the id of the request being sent and skips a response that carries a different id. A response
carrying no id is still taken: it is how a server refuses a request whose id it could not read, and the POST stream
answers only that request. Server-to-client requests whose id is a non-finite number are dropped unanswered, like
those with an oversized integer id. A request with an id that is malformed but can be written back (an object, for
example) is still answered with Method not found, as an existing test requires.

## How Has This Been Tested?

New tests in `test/mcp/client/http_test.rb`: a response carrying another request's id is skipped and the request's
own response is returned (the other one was returned before); `ping` and `elicitation/create` requests with the ids
`1e400` and `-1e400` are not answered and the request being sent still gets its response (it raised
`JSON::GeneratorError` before); an error response carrying no id is still returned. The first two fail against the
previous library; the third passed before and guards the id-less case.

The SSE fixture of `test_connect_raises_on_an_initialize_response_that_is_not_json_rpc_2_0` answered `initialize`
with the id `"1"`, while the client sends a UUID. The fixture now carries no id, so the test still checks the missing
`jsonrpc` member rather than the id.

Not passing the request id, dropping the id check, also skipping responses carrying no id, or skipping only oversized
integer ids each fails one of these tests.

## Breaking Changes

None for a server that answers each request with its own id.

Fixes modelcontextprotocol#717.
@koic
koic merged commit 4962426 into modelcontextprotocol:main Nov 29, 2026
13 checks passed
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.

The HTTP client returns another request's SSE response and crashes on a server request with id 1e400

2 participants