Repository navigation
Conversation
A POST whose JSON-RPC object has a string `method`, an `id` and also a `result` or an `error` was never answered. `JSONRPCMessageKind` checks `method` first and called it a request, so `StatefulHTTPServerTransport` opened an SSE stream and kept the request's `httpRequestContexts` and `openRequestIDs` entries, and `StatelessHTTPServerTransport` created an exchange and waited. `Server` tries `AnyResponse` before `AnyRequest`, and `Response` ignores `method`, so it read the body as a response to an unknown request and sent nothing. The POST hung, the stateful transport kept that state until the session ended, and an `initialize` with a `result` opened a session in which a real `initialize` was then refused. Fix: classify such a message as `.invalid` when its `id` is readable, so both transports answer HTTP 400 with -32600 and the `id`, and keep no state for it. An `id` that cannot be read keeps its own reason. A batch item of this shape on the other transports now gets -32600 as well, since `Server.Batch.Item` uses the same classifier. Adds tests to `RequestIDValidationTests` for both HTTP transports, an `initialize` with a `result`, the classifier, and a batch item. Introduced in 6112a39 ("V0.11.x (modelcontextprotocol#199)").
…n error - Add a CHANGELOG entry under Fixes: a request with a string or integer `id` that also has a `result` or an `error` gets -32600 with its `id` (HTTP 400, or per item in a batch), an answer that carries a `method` is refused over HTTP, and a single such message on the other transports is still read as a response.
A client may answer a server-to-client request with a message that also carries a `method`. The HTTP server transports answer such a message with -32600, but they did not give it to `Server`, so the server's request was never resolved and waited forever. Fix: when `Server` reads a request that also has a `result` or an `error` as a response (`AnyResponse` decodes the bytes the transport has), both transports still give it to the server after the -32600, as they do for an answer whose `jsonrpc` is wrong. One `Server` would read as a request is not given to it, since the transport has refused it, and neither is a request whose `id` cannot be read. Adds tests: a pending `roots/list` answered this way completes, and only the message `Server` reads as a response reaches it, on both transports.
The entry said that, before this change, the stateless HTTP transport neither gave an answer carrying a `method` to `Server` nor answered the POST. It did give it to `Server`, under the internal `id` it puts in place of the client's, so the answer never matched the pending request. Neither transport answered the POST.
The stateless transport had no end-to-end test for an answer that also carries a `method`; only the hand-off to its receive stream was tested. The new test answers the round-trip bridge's `elicitation/create` at 2025-11-25 with an extra `method`, and checks that the POST gets 400 and -32600 with the `id` and that the tool call still completes. Without the hand-off to `Server` it times out.
The HTTP server transports answered a POST that `Server` reads as a response with 400 and -32600, while still giving it to `Server`, which completed its pending request with it. The client saw a refusal for an answer the server had acted on. `JSONRPCMessageKind` now classifies a message with a `method`, a readable `id` and a `result` or an `error` as a response when it decodes as `Response<AnyMethod>`, as `Server` and `Client` read a single message. Both transports then accept it with 202 through their `.response` path, and `Server.Batch.Item` hands such a batch item to the pending request instead of answering -32600. A message that does not read as a response, such as `"error": 5`, still gets -32600 with its `id`. `readsAsAnswer` is removed. `HTTPClientTransport.answeredRequestID` reads the same classifier, so a server answer with an extra `method` now ends the wait on its stream.
jeanmatthieu
left a comment
There was a problem hiding this comment.
Thanks, this fixes a real hang, and the tests cover the shapes well.
I pushed one commit on top (666d8a4) for the review note on refused answers. Before it, an answer to a server request that also had a method got 400 and -32600, but the transport still passed it to Server, which completed its request with it. The client saw a refusal for an answer the server had already used.
Rather than adding return .accepted to the readsAsAnswer branches, I moved the decision into JSONRPCMessageKind. A message with a method, a readable id and a result or an error is now a .response when it decodes as Response<AnyMethod>. That is how Server and Client already read a single message. So:
- Both HTTP server transports accept it with 202 through their existing
.responsepath. On the stateless transport that includes the normalized body and the header checks.readsAsAnswerand the special branches are gone. - A batch item of this shape now completes the pending server request instead of getting -32600, because
Server.Batch.Itemalready passes.responseitems tohandleResponse. This covers the follow-up mentioned under Breaking Changes. - A message that doesn't read as a response (
"error": 5,"error": {}, an error withoutcode) still gets -32600 with itsid, over HTTP and in a batch. HTTPClientTransport.answeredRequestIDuses the same classifier, so a server answer with an extramethodnow ends the wait on its stream.
The Mcp-Method check on the stateless .response path doesn't affect this, because 2026-07-28 has no server-to-client requests (usesStandardHeaders == !allowsServerRequests).
One trade-off: a malformed request whose result reads as a response, with an id that matches nothing pending, now gets 202 and is ignored. That matches stdio and leaves nothing hanging.
I updated the tests that expected 400 on this path, split the answerMember and classifier tests into the response and invalid cases, added a batched answer with a method, and rewrote the CHANGELOG entry. swift test, the DocC build with --warnings-as-errors, and conformance at 2025-11-25 and 2026-07-28 all pass.
Approving. (pending CI)
Motivation and Context
A POST whose JSON-RPC object has a string
method, anid, and also aresultor anerroris never answered.JSONRPCMessageKindchecksmethodfirst and calls it a request,so
StatefulHTTPServerTransportopens an SSE stream and keeps the request'shttpRequestContextsandopenRequestIDsentries, andStatelessHTTPServerTransportcreatesan exchange and waits.
ServertriesAnyResponsebeforeAnyRequest, andResponseignoresmethod, so it reads the body as a response to an unknown request and sends nothing. The POSThangs, the stateful transport keeps that state until the session ends, and an
initializewith a
resultopens a session in which a realinitializethen gets "Session alreadyinitialized". Introduced in 6112a39 (V0.11.x (#199)).
JSONRPCMessageKindclassifies such a message asServerandClientread it: as aresponse when it decodes as
Response<AnyMethod>, with itsmethodignored. Otherwise itis invalid, with the reason "result and error must not appear in a request", for example
"error": 5. Anidthat cannot be read keeps its own reason.existing
.responsepath and give it to the server, so an answer to a server-to-clientrequest that carries an extra
methodcompletes that request. They answer one that doesnot with HTTP 400 and -32600 carrying the
id, through their existing invalid-request path,and open no stream, exchange or session for it.
Server.Batch.Itemuses the same classifier, so a batch item that reads as a responseanswers the server's pending request, and one that does not gets its own -32600.
HTTPClientTransportuses it too, so a server answer with an extramethodends the waiton its stream.
How Has This Been Tested?
Extends
RequestIDValidationTests:resultor anerrorthat does not read as aresponse gets 400 with -32600 and the
idand keeps no request context. One that does readas a response gets 202 and keeps no context either. That includes
"result": nulland"result": {}, "error": 5.2026-07-28.
initializewith aresult: opens no session, with and without the default validators."error": 5gets -32600, and a batched answerwith a
methodcompletes the server's pendingroots/list.A pending
roots/listanswered with an extramethodcompletes and its POST gets 202(
HTTPServerTransportTests). On both transports only a message that reads as a responsereaches the server. Over the stateless transport, the round-trip bridge's
elicitation/create, answered with an extramethod, gets 202 and completes the tool call(
RequestLifecycleTests).HTTPClientTransport.answeredRequestIDnames the request such ananswer is for (
HTTPClientReliabilityTests).Without the fix these fail: the stateless POST gets no answer and the stateful one gets an SSE
stream.
swift testpasses, the docs build with--warnings-as-errors, and conformance passesat 2025-11-25 and 2026-07-28. I did not run
scripts/run-interop.sh --check, because Go isnot installed on this machine.
Breaking Changes
None in the API. Over HTTP, a request with a
resultor anerrorthat does not read as aresponse now gets 400 instead of a stream or exchange that is never answered. That includes a
request with a malformed
errorsuch as"error": 5, whichServerused to serve and answeras a request. One that reads as a response is now accepted with 202 and handled as a response.
An answer to a server-to-client request that carries an extra
methodtherefore completes thatrequest. Before, neither transport answered the POST, and the stateless one gave the answer to
Serverunder anotherid, so it never matched.A malformed request whose
resultreads as a response, with anidthat names no pendingrequest, gets 202 and is ignored, as on stdio.
On the stdio, Network and in-memory transports a single such message is handled by
Serverasbefore. In a JSON-RPC batch, an item that reads as a response now answers the server's pending
request instead of being served as a request, and one that does not gets -32600.
Types of changes