Skip to content

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

Open
canarik wants to merge 6 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/http-server-method-with-result
Open

canarik wants to merge 6 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 a message with a string method, a readable id and a
    result or an error as invalid, with the reason "result and error must not appear in a
    request". An id that cannot be read keeps its own reason.
  • Both HTTP server transports answer it with HTTP 400 and -32600 carrying the id, through
    their existing invalid-request path, and open no stream, exchange or session for it.
  • When Server reads such a message as a response, both transports also give it to the
    server, so an answer to a server-to-client request that carries an extra method completes
    that request. A message Server would read as a request is not given to it.

How Has This Been Tested?

Extends RequestIDValidationTests: the stateful transport answers 400 with -32600 and the
id and keeps no request context; the stateless transport answers within 5 seconds at
2025-11-25 and 2026-07-28; an initialize with a result opens no session, with and without
the default validators; the classifier's cases, including "result": null, "error": 5 and
unreadable ids; and a batch item on a message stream. A pending roots/list answered with an
extra method completes (HTTPServerTransportTests), and on both transports only the message
Server reads as a response reaches it. Over the stateless transport, the round-trip bridge's
elicitation/create answered with an extra method completes the tool call
(RequestLifecycleTests). Without the fix these fail: the stateless POST gets no answer and
the stateful one gets an SSE stream. Without the hand-off to Server, the pending requests
never complete. swift test passes, and the docs build with --warnings-as-errors. 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, such a message now gets 400 instead of a stream or exchange that
is never answered. That includes a request with any error member, even a malformed one such
as "error": 5, which Server used to serve and answer as a request. An answer to a
server-to-client request that carries an extra method also gets 400, and Server takes it as
the answer; before, the stateless transport gave it to Server under another id, so it never
matched. On the stdio, Network and in-memory transports a single such message is handled by
Server as before, while the same object in a batch now gets -32600, because
Server.Batch.Item uses the same classifier. If that batch item answers a pending server
request, the request keeps waiting, as it did before; that needs a change to Server's receive
loop and is left for a follow-up.

Types of changes

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

AI assistance

Drafted with an AI coding agent, then reviewed by me.

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
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.

1 participant