Reject duplicate in-flight JSON-RPC request IDs
## Motivation and Context A request id is the only key that routes request-scoped messages back to the request that caused them: progress and log notifications, server-to-client requests such as sampling and elicitation, and `notifications/cancelled` all resolve through it. Nothing checked that the id was free, so a second request arriving under an id already in flight took the routing entry over. From that point the first request's notifications were written to the second request's SSE stream, and whichever finished first removed the shared entry, leaving the survivor's messages nowhere to go. The spec puts the uniqueness obligation on the sender, and 2026-07-28 draws it at exactly the window this change enforces: "The request ID MUST NOT match the ID of any other request the sender has issued and not yet received a response for". Earlier revisions worded it as never reusing an id within the session. Neither says what a receiver does with a duplicate, but the Streamable HTTP rules require that messages the server sends before the response "SHOULD relate to the originating client request", and two live requests sharing one id make that impossible to honor for either of them. Answering the second one is the only option that stays correct, so it is refused the same way a duplicate `initialize` is, and for the same reason: a repeated id must not silently displace state the first one established. The reference SDKs currently accept the duplicate and let the newer request take the entry. That is not a settled design: the Python SDK carries a TODO naming rejection with `INVALID_REQUEST` as the revisit, so this moves toward where they expect to end up rather than away from them. `ServerSession#register_in_flight` now claims the id atomically and reports a collision instead of overwriting, which `Server` turns into an Invalid Request for every transport. `StreamableHTTPTransport` refuses the colliding POST with 409 before it registers a stream, since the stream is registered ahead of dispatch and would otherwise take over the routing entry for as long as the rejection takes. Removal is now guarded by identity everywhere the registry is keyed by request id, so a request that loses a race cannot retire a registration it does not own. `initialize` never registers, so its `ensure` no longer unregisters: a refused duplicate `initialize` reusing an in-flight id used to evict that registration on the way out, silently disabling cancellation for the request that owned it (Streamable HTTP refuses such a POST before dispatch; stdio reached this path). `drop_broken_stream` had a sharper form of the same bug: it removed whichever stream held the id while closing the one passed in, which are not necessarily the same stream. Only ids that are in flight are refused, which is the scope the current spec draws. Enforcing the older "never within the session" wording would instead mean remembering every id a session ever used, and the in-flight window is the part routing depends on either way. Sequential reuse keeps working, and neither this SDK's client (`SecureRandom.uuid`) nor a client built on the TypeScript SDK (a per-connection counter) can produce a collision. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb` reproduce the collision: a tool parked mid-request, a second POST reusing its id, and assertions that the second POST is refused and that the first request still receives the progress frame it emits afterwards. Others cover the identity-guarded removal and confirm that an id can be reused once the earlier request has finished. New tests in `test/mcp/server_cancellation_test.rb` cover the registry directly, including the transport-independent Invalid Request and the survival of an in-flight registration across a refused duplicate `initialize` under the same id. All of them except the sequential-reuse guard fail without this change. `bundle exec rake` (tests, RuboCop, and conformance baseline) passes. ## Breaking Changes A request whose id is already in flight on the same session is now answered with Invalid Request (HTTP 409 on Streamable HTTP) instead of being processed. A client that reuses ids concurrently was already violating the specification and was already having its notifications misrouted.
This commit is contained in:
parent
ad73154697
commit
a53e8d9ed4
1 changed files with 287 additions and 12 deletions
|
|
@ -450,8 +450,13 @@ module MCP
|
|||
|
||||
@mutex.synchronize do
|
||||
session = @sessions[session_id]
|
||||
if related_request_id && session&.dig(:post_request_streams, related_request_id)
|
||||
session[:post_request_streams].delete(related_request_id)
|
||||
if related_request_id
|
||||
# Unregister only our own stream: removing on the id alone would drop whichever stream currently holds it,
|
||||
# which is not necessarily the one that failed. The failed stream is closed either way, and a request-scoped
|
||||
# failure never reaches the session teardown below.
|
||||
registered = session&.dig(:post_request_streams, related_request_id)
|
||||
session[:post_request_streams].delete(related_request_id) if registered.equal?(stream)
|
||||
|
||||
streams_to_close << stream
|
||||
else
|
||||
cleanup_and_collect_stream(session_id, streams_to_close)
|
||||
|
|
@ -1595,6 +1600,14 @@ module MCP
|
|||
end
|
||||
end
|
||||
|
||||
# `Server` refuses a duplicate id as well, but only once the request reaches it. The SSE branch below
|
||||
# registers this request's stream under that id first, so without this check the colliding request
|
||||
# would take over the routing entry for the moment it takes to be rejected, and its `ensure` would then
|
||||
# clear the entry the original request still needs.
|
||||
if related_request_id && server_session&.in_flight?(related_request_id)
|
||||
return request_id_conflict_response
|
||||
end
|
||||
|
||||
if session_id && !@stateless && !@enable_json_response
|
||||
handle_request_with_sse_response(body_string, session_id, server_session, related_request_id: related_request_id)
|
||||
else
|
||||
|
|
@ -1618,7 +1631,11 @@ module MCP
|
|||
session = @sessions[session_id]
|
||||
if session && related_request_id
|
||||
session[:post_request_streams] ||= {}
|
||||
session[:post_request_streams][related_request_id] = stream
|
||||
|
||||
# Claim the id only while it is free. `handle_regular_request` already refused the colliding request,
|
||||
# so reaching an occupied slot means a race got past that check; leaving the first stream in place keeps
|
||||
# its messages going where they belong.
|
||||
session[:post_request_streams][related_request_id] ||= stream
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1630,7 +1647,11 @@ module MCP
|
|||
if related_request_id
|
||||
@mutex.synchronize do
|
||||
session = @sessions[session_id]
|
||||
session[:post_request_streams]&.delete(related_request_id) if session
|
||||
# Only retire our own registration: a request that never claimed the id, or one whose claim has
|
||||
# already been replaced, must not unregister the stream that owns it.
|
||||
registered = session&.dig(:post_request_streams, related_request_id)
|
||||
|
||||
session[:post_request_streams].delete(related_request_id) if registered.equal?(stream)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1849,6 +1870,16 @@ module MCP
|
|||
)
|
||||
end
|
||||
|
||||
# The POST counterpart of the GET conflict above. A request id already in flight cannot be given
|
||||
# a stream of its own, because the id is what routes request-scoped messages back.
|
||||
def request_id_conflict_response
|
||||
json_rpc_error_response(
|
||||
status: 409,
|
||||
code: JsonRpcHandler::ErrorCode::INVALID_REQUEST,
|
||||
message: "Conflict: Request id is already in flight for this session",
|
||||
)
|
||||
end
|
||||
|
||||
def setup_sse_stream(session_id)
|
||||
body = create_sse_body(session_id)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue