-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(core,server): expose the per-request envelope on the request handler context #2231
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6aaa838
feat(core): expose the governing protocol version on the request hand…
felixweinberger 0966d2b
feat(server): expose per-request client capabilities and info on the …
felixweinberger 97cf203
test(e2e): cover the per-request envelope on the handler context
felixweinberger dbab53e
docs: document the per-request envelope on the handler context
felixweinberger e047648
Add changeset for the per-request envelope context fields
felixweinberger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@modelcontextprotocol/core': patch | ||
| '@modelcontextprotocol/server': patch | ||
| --- | ||
|
|
||
| Request handlers can now read the protocol version governing the current request from their context (`ctx.mcpReq.protocolVersion`, both client and server handlers), and server handlers can read the calling client's declared capabilities and implementation info | ||
| (`ctx.client.capabilities`, `ctx.client.info`). `getNegotiatedProtocolVersion()` is now declared on `Protocol`, so both roles expose it. |
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
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
registerTool_requestContextexample (and its synced copy in docs/server.md) gates the form-modeelicitInputcall onctx.client.capabilities.elicitationbeing truthy, butServer.elicitInput()withmode: 'form'requires the mode-specificelicitation.formcapability and throwsCapabilityNotSupportedotherwise — so a url-only client (elicitation: { url: {} }) passes the example's guard yet the tool call returnsisError, the opposite of the documented "never ask a client to do something it cannot" intent. Change the guard toctx.client.capabilities.elicitation?.formto match the predicate the SDK actually enforces.Extended reasoning...
What the bug is. The new "Reading request context" section exists specifically to teach capability-aware gating using the new
ctx.client.capabilitiesfield: "Checkctx.client.capabilitiesbefore sending a server-initiated request so you never ask a client to do something it cannot." The example then guards a form-mode elicitation with the coarse checkif (ctx.client.capabilities.elicitation)(examples/server/src/serverGuide.examples.ts~line 441, synced intodocs/server.md). However, the SDK's own predicate for form elicitation is stricter:Server.elicitInput()withmode: 'form'throwsSdkError(CapabilityNotSupported, 'Client does not support form elicitation.')unlessthis._clientCapabilities?.elicitation?.formis set (packages/server/src/server/server.ts:579-580).The code path that triggers it. A spec-valid client may declare url-only elicitation:
capabilities: { elicitation: { url: {} } }.ElicitationCapabilitySchemaonly normalizes an emptyelicitation: {}object to{ form: {} }(packages/core/src/types/schemas.ts:318-332); a non-empty, form-less declaration like{ url: {} }is preserved as-is. The client-side helpergetSupportedElicitationModes()inpackages/client/src/client/client.tsagrees: it returnssupportsFormMode: falsefor a url-only declaration, so such a client will also reject an incoming form request. Both ends therefore treat "has elicitation" and "supports form elicitation" as different things — but the example conflates them.Step-by-step proof.
capabilities: { elicitation: { url: {} } }. After initialize,ctx.client.capabilities.elicitationis{ url: {} }— truthy.delete-recordstool.if (ctx.client.capabilities.elicitation)→ truthy object → enters the branch.ctx.mcpReq.elicitInput({ mode: 'form', ... })callsServer.elicitInput(), which checksthis._clientCapabilities?.elicitation?.form→undefined→ throwsSdkError(CapabilityNotSupported, 'Client does not support form elicitation.').McpServer.registerToolwrapper catches the throw and converts it into anisError: truetool result. The records are never deleted and the user never sees a confirmation prompt — the tool just fails, which is exactly the "asking a client to do something it cannot" failure the section says this pattern prevents.Why nothing else prevents it. The example never reaches the elicitation request handler on the client (the SDK throws server-side first), and there is no fallback path in the example: the throw escapes the handler before the "delete and attribute to caller" branch. The e2e scenarios added in this PR only cover
{ sampling, roots }and bare-capability clients, so the url-only-elicitation shape is not exercised.Impact. This is a documentation/example correctness issue only — no SDK runtime code is wrong. But because the example is the canonical illustration of the new
ctx.client.capabilitiesfield, it teaches readers a check that does not match what the SDK enforces, and copy-pasted handlers will fail for the (narrow but legitimate) class of url-only elicitation clients. Note that the bareelicitation: {}case is fine thanks to the schema preprocess normalization, so the impact is limited to form-less, non-empty elicitation declarations.How to fix. Change the guard in
examples/server/src/serverGuide.examples.ts(and re-run the snippet sync sodocs/server.mdfollows) to the mode-specific check:This matches the predicate
Server.elicitInput()enforces for form mode and the semantics ofgetSupportedElicitationModes(). Optionally, the surrounding prose ("only elicit input when the client declared theelicitationcapability") could mention that form vs. url elicitation are gated by their mode-specific sub-capabilities.