Skip to content

fix: Reject an HTTP request that also has a result or an error - #404

Merged
canarik merged 7 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/http-server-method-with-result
Nov 30, 2026
Merged

canarik merged 7 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/http-server-method-with-result

Conversation

@canarik

@canarik canarik commented Nov 29, 2026 •

Copy link
Copy Markdown

Motivation and Context

A POST whose JSON-RPC object has a string method, an id, and also a result or an
error is never answered. JSONRPCMessageKind checks method first and calls it a request,
so StatefulHTTPServerTransport opens an SSE stream and keeps the request's
httpRequestContexts and openRequestIDs entries, and StatelessHTTPServerTransport creates
an exchange and waits. Server tries AnyResponse before AnyRequest, and Response ignores
method, so it reads the body as a response to an unknown request and sends nothing. The POST
hangs, the stateful transport keeps that state until the session ends, and an initialize
with a result opens a session in which a real initialize then gets "Session already
initialized". Introduced in 6112a39 (V0.11.x (#199)).

  • JSONRPCMessageKind classifies such a message as Server and Client read it: as a
    response when it decodes as Response<AnyMethod>, with its method ignored. Otherwise it
    is invalid, with the reason "result and error must not appear in a request", for example
    "error": 5. An id that cannot be read keeps its own reason.
  • Both HTTP server transports accept one that reads as a response with 202 through their
    existing .response path and give it to the server, so an answer to a server-to-client
    request that carries an extra method completes that request. They answer one that does
    not 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.Item uses the same classifier, so a batch item that reads as a response
    answers the server's pending request, and one that does not gets its own -32600.
    HTTPClientTransport uses it too, so a server answer with an extra method ends the wait
    on its stream.

How Has This Been Tested?

Extends RequestIDValidationTests:

  • Stateful transport: a request with a result or an error that does not read as a
    response gets 400 with -32600 and the id and keeps no request context. One that does read
    as a response gets 202 and keeps no context either. That includes "result": null and
    "result": {}, "error": 5.
  • Stateless transport: the same two outcomes, each within 5 seconds, at 2025-11-25 and
    2026-07-28.
  • initialize with a result: opens no session, with and without the default validators.
  • Classifier: response and invalid cases, and unreadable ids.
  • Batches: answer-shaped items get no reply, "error": 5 gets -32600, and a batched answer
    with a method completes the server's pending roots/list.

A pending roots/list answered with an extra method completes and its POST gets 202
(HTTPServerTransportTests). On both transports only a message that reads as a response
reaches the server. Over the stateless transport, the round-trip bridge's
elicitation/create, answered with an extra method, gets 202 and completes the tool call
(RequestLifecycleTests). HTTPClientTransport.answeredRequestID names the request such an
answer is for (HTTPClientReliabilityTests).

Without the fix these fail: the stateless POST gets no answer and the stateful one gets an SSE
stream. swift test passes, the docs build with --warnings-as-errors, and conformance passes
at 2025-11-25 and 2026-07-28. I did not run scripts/run-interop.sh --check, because Go is
not installed on this machine.

Breaking Changes

None in the API. Over HTTP, a request with a result or an error that does not read as a
response now gets 400 instead of a stream or exchange that is never answered. That includes a
request with a malformed error such as "error": 5, which Server used to serve and answer
as 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 method therefore completes that
request. Before, neither transport answered the POST, and the stateless one gave the answer to
Server under another id, so it never matched.

A malformed request whose result reads as a response, with an id that names no pending
request, gets 202 and is ignored, as on stdio.

On the stdio, Network and in-memory transports a single such message is handled by Server as
before. 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

  • Bug fix (non-breaking change which fixes an issue)

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.
@canarik
canarik requested a review from a team as a code owner November 29, 2026 21:45
canarik and others added 2 commits November 30, 2026 02:06
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 jeanmatthieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .response path. On the stateless transport that includes the normalized body and the header checks. readsAsAnswer and the special branches are gone.
  • A batch item of this shape now completes the pending server request instead of getting -32600, because Server.Batch.Item already passes .response items to handleResponse. This covers the follow-up mentioned under Breaking Changes.
  • A message that doesn't read as a response ("error": 5, "error": {}, an error without code) still gets -32600 with its id, over HTTP and in a batch.
  • HTTPClientTransport.answeredRequestID uses the same classifier, so a server answer with an extra method now 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)

@canarik
canarik merged commit bd0022a into modelcontextprotocol:rel/1.0.0 Nov 30, 2026
14 checks passed
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.

2 participants