Skip to content

fix: refuse a duplicate SSE request that loses the stream claim - #699

Open
yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-sse-request-ownership-race
Open

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-sse-request-ownership-race

Conversation

@yorzi

@yorzi yorzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Closes the race #521 left between the stream claim and the in-flight registration: a duplicate request that loses the claim no longer runs.

Motivation and Context

#521 refuses a POST that reuses the id of a request in flight in its session, with 409, before anything is registered (lib/mcp/server/transports/streamable_http_transport.rb:2489). Two POSTs sent together under the same id can both pass that check, since neither is registered until its SSE body runs. Their bodies then claim the id's stream (:2526) and register the id in ServerSession in two separate steps, under two locks. The body that loses the stream claim still dispatches (:2545 writes its response). When it registers the id before the owner does:

  • the duplicate runs the tool, and its request-scoped messages (progress, server-to-client requests) go to the owner's stream;
  • the owner is refused by Server;
  • once the duplicate has finished, the owner registers the id and runs the tool a second time.

On main (c2ce3c1), with the owner held after its claim and the duplicate run meanwhile, the tool runs twice and the owner's stream carries the duplicate's progress:

OWNER STREAM:
data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"tok","progress":1,"message":"frame-of-run-1"}}

data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"tok","progress":1,"message":"frame-of-run-2"}}

data: {"jsonrpc":"2.0","id":"req-1","result":{"content":[{"type":"text","text":"done"}],"isError":false}}

DUPLICATE STREAM:
data: {"jsonrpc":"2.0","id":"req-1","result":{"content":[{"type":"text","text":"done"}],"isError":false}}

invocations=2

The claim is now the decision: a body that finds the id's stream taken answers on its own stream with the same Invalid Request error the 409 carries, with the request's id, and does not dispatch. The message is shared with the 409 through a constant. Requests that reach the claim with no session or no id dispatch as before, and nothing changes for the stream owner, resumption (SEP-1699), or subscriptions/listen streams, which do not go through this claim.

How Has This Been Tested?

New tests in test/mcp/server/transports/streamable_http_transport_test.rb:

  • Two POSTs with the same id are answered before either runs; the first is held after it claimed the stream and before it registered, and the second runs meanwhile. The tool runs once, for the owner, whose stream gets its progress and result, and the duplicate's stream gets the Invalid Request error with its id. This fails against main (the tool runs twice).
  • Two sessions using the same id at the same time both run (guard).

Dispatching the refused request, answering it with nothing, or leaving out its id each fails a test. The #521 tests (409 refusal, sequential reuse, a finishing request leaving another's stream) pass unchanged. On Ruby 3.4.4: bundle exec rake test (2716 runs, 0 failures, 3 skips), bundle exec rake rubocop (no offenses), and bundle exec rake conformance:test (baseline check passed for server and client).

Breaking Changes

None. A client that sends two requests with the same id at once, which the spec forbids, gets an Invalid Request error for the second instead of having it run.

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 (no user-facing behavior to document: only a client violating the spec is affected)

Additional context

Only a client that reuses an id within its own session is affected, so this is about keeping one request's messages and side effects from being duplicated or misrouted, not about isolation between clients. A fuller alternative is to reserve the id when the POST is answered rather than when its body runs, which would turn the duplicate into a 409; it needs care for a body the Rack server never calls, so this keeps the smaller change. Only Ruby 3.4.4 was run locally; CI covers the rest of the matrix.

## Motivation and Context

modelcontextprotocol#521 refuses a POST that reuses the id of a request in flight in its session, with 409 before anything is registered.
Two POSTs sent together under the same id can both pass that check, since neither is registered until its SSE body
runs. Their bodies then claim the id's stream and register the id in `ServerSession` in two separate steps. The body
that loses the stream claim still dispatches, and when it registers the id before the owner does, the duplicate runs
the tool, its request-scoped messages (progress, server-to-client requests) go to the owner's stream, and the owner is
refused. Once the duplicate finishes, the owner registers the id and runs the tool a second time.

The claim is now the decision: a body that finds the id's stream taken answers on its own stream with the same Invalid
Request error the 409 carries, with the request's id, and does not dispatch. Requests that reach the claim with no
session or no id dispatch as before, and the stream owner is unchanged.

## How Has This Been Tested?

A new test in `test/mcp/server/transports/streamable_http_transport_test.rb` answers two POSTs with the same id, holds
the first after it claimed the stream and before it registered, and runs the second meanwhile. The tool runs once, for
the owner, whose stream gets its progress and result, and the duplicate's stream gets the Invalid Request error with its
id. It fails against the previous library, where the tool runs twice and the duplicate's progress reaches the owner's
stream. Another new test guards two sessions that use the same id at the same time. Dispatching the refused request,
answering it with nothing, or leaving out its id each fails a test.

## Breaking Changes

None. A client that sends two requests with the same id at once (which the spec forbids) gets an Invalid Request error
for the second instead of running it.

This branch has not been deployed

No deployments
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.

1 participant