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.
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.
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 a message with a stringmethod, a readableidand aresultor anerroras invalid, with the reason "result and error must not appear in arequest". An
idthat cannot be read keeps its own reason.id, throughtheir existing invalid-request path, and open no stream, exchange or session for it.
Serverreads such a message as a response, both transports also give it to theserver, so an answer to a server-to-client request that carries an extra
methodcompletesthat request. A message
Serverwould read as a request is not given to it.How Has This Been Tested?
Extends
RequestIDValidationTests: the stateful transport answers 400 with -32600 and theidand keeps no request context; the stateless transport answers within 5 seconds at2025-11-25 and 2026-07-28; an
initializewith aresultopens no session, with and withoutthe default validators; the classifier's cases, including
"result": null,"error": 5andunreadable ids; and a batch item on a message stream. A pending
roots/listanswered with anextra
methodcompletes (HTTPServerTransportTests), and on both transports only the messageServerreads as a response reaches it. Over the stateless transport, the round-trip bridge'selicitation/createanswered with an extramethodcompletes the tool call(
RequestLifecycleTests). Without the fix these fail: the stateless POST gets no answer andthe stateful one gets an SSE stream. Without the hand-off to
Server, the pending requestsnever complete.
swift testpasses, and the docs build with--warnings-as-errors. I did notrun
scripts/run-interop.sh --check, because Go is not installed on this machine.Breaking Changes
None in the API. Over HTTP, such a message now gets 400 instead of a stream or exchange that
is never answered. That includes a request with any
errormember, even a malformed one suchas
"error": 5, whichServerused to serve and answer as a request. An answer to aserver-to-client request that carries an extra
methodalso gets 400, andServertakes it asthe answer; before, the stateless transport gave it to
Serverunder anotherid, so it nevermatched. On the stdio, Network and in-memory transports a single such message is handled by
Serveras before, while the same object in a batch now gets -32600, becauseServer.Batch.Itemuses the same classifier. If that batch item answers a pending serverrequest, the request keeps waiting, as it did before; that needs a change to
Server's receiveloop and is left for a follow-up.
Types of changes
AI assistance
Drafted with an AI coding agent, then reviewed by me.