Repository navigation
Conversation
## 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
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.
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 inServerSessionin two separate steps, under two locks. The body that loses the stream claim still dispatches (:2545writes its response). When it registers the id before the owner does:Server;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: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/listenstreams, which do not go through this claim.How Has This Been Tested?
New tests in
test/mcp/server/transports/streamable_http_transport_test.rb:main(the tool runs twice).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), andbundle 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
Checklist
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.