-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fail fast on server-to-client requests in JSON-response mode instead of hanging #3195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ffcc057
Fail fast on request-scoped server requests in JSON-response mode
maxisbey e20be4d
Drop request-scoped messages a JSON response cannot carry at the router
maxisbey c8a4bb1
Document that connect() drivers must pass the transport's context bui…
maxisbey 08638b8
Move the JSON-mode back-channel verdict onto the message metadata
maxisbey 8d76959
Narrow the transport's back-channel verdict to JSON-response mode
maxisbey ca1a848
Stamp the JSON-mode verdict in one metadata factory
maxisbey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 The new
json_response=Truebullet's parenthetical "(ctx.elicit(), sampling)" over-claims for sampling: the default sampling API (ctx.session.create_message()) sends withrelated_request_id=None, so it rides the connection's standalone channel — which never consults thecan_send_requestflag this PR wires up — and still routes to the GET stream (or silently hangs if none is attached, the out-of-scope case the PR description acknowledges) rather than raisingNoBackChannelError. Only request-scoped calls (ctx.elicit(), Resolve-driven sampling, or an explicitrelated_request_id) get the new fail-fast; consider dropping "sampling" or qualifying it as request-scoped sampling.Extended reasoning...
What the doc claims vs. what the code does
The new bullet in
docs/run/index.mdsays that underjson_response=True"a tool that calls back into the client mid-request (ctx.elicit(), sampling) raisesNoBackChannelErroron this leg". That is accurate forctx.elicit()/ctx.elicit_url()and for Resolve-driven sampling, because those stamprelated_request_id=ctx.request_idand therefore travel the request-scopedDispatchContext, whosecan_send_requestthis PR sets toFalsein JSON-response mode. It is not accurate for the default sampling API.The code path
ServerSession.create_message()defaultsrelated_request_id=None, andServerSession.send_request()selects the channel withchannel = self._request_outbound if related is not None else self._connection.outbound(src/mcp/server/session.py:88-89). With no related id, sampling rides the connection's standalone channel. For a stateful loop session,Connection.for_loopinstalls theJSONRPCDispatcheritself as that outbound, and itssend_raw_requestnever consultscan_send_request— only the per-messageDispatchContextdoes (src/mcp/shared/jsonrpc_dispatcher.py:167). The mcpserverContexthas no sampling helper that stamps a related id (onlyelicit/elicit_urldo,src/mcp/server/mcpserver/context.py:185,218), so the ordinary way a tool samples —ctx.session.create_message(...)— goes standalone by default.Why the new guards don't fire on this path
Both new mechanisms in this PR are keyed to the request-scoped leg. The
transport_context_forfail-fast is only checked inDispatchContext.send_raw_request, which the standalone path bypasses. And the newmessage_routerdrop branch insrc/mcp/server/streamable_http.pyrequiresrelated_request_id is not Nonebefore it fires; a sampling request with no related id falls through toGET_STREAM_KEYas before.Step-by-step proof
json_response=Trueand a tool that callsawait ctx.session.create_message([...], max_tokens=10)mid-request.2025-11-25) client that has not opened the standalone GET stream, and call the tool.create_messagebuildsServerMessageMetadata(related_request_id=None)(session.py:266) →send_requestpicksself._connection.outbound→ the dispatcher writes the request and parks a waiter, with nocan_send_requestcheck anywhere on that path.message_routersees aJSONRPCRequestwith no related id →target_request_idstaysNone→ routes toGET_STREAM_KEY. No GET stream is registered, so the message is logged and dropped.NoBackChannelError.(If the client has a GET stream attached, the request is delivered there instead — also not the documented
NoBackChannelError.)Why this is a nit
Nothing in the code is wrong — the PR's actual behavior change is correct, tested, and matches its description. The inaccuracy is one word in one new doc bullet; the neighboring
legacy-clients.mdnote andtroubleshooting.mdentries are correctly scoped to the request-scoped channel. All verifiers who traced this agreed.Suggested fix
Drop "sampling" from the parenthetical, or qualify it, e.g.: "a tool that calls back into the client on the request-scoped channel (
ctx.elicit(), sampling with a related request id)".