Repository navigation
fix(server): answer a known method with invalid params with -32602 #1375
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
Open
talhayme
wants to merge
2
commits into
modelcontextprotocol:main
Choose a base branch
from
talhayme:fix/1347-invalid-params-for-known-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think this is identifying a valid problem but this seems like not the right approach. Having to maintain the method lists and mapping in multiple places is fragile and likely to drift over time. Fixing this however requires some rework up to the deserialization step where the distinction between unknown methods and incorrect params is lost.
Instead I would suggest reworking deserialization a little bit into something like this (rough example) where the method is identified in order to deserialize (or fail on) the specific request params
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.
Alternative design idea from Gemini, leaning on macros:
Every typed request is already parameterized by
M: ConstString(Request<M, P>,RequestOptionalParam<M, P>,RequestNoParam<M>). Add a trait:ts_union!, dispatchDeserializeby"method"instead of#[serde(untagged)]:"method"in the buffered map/value."method"against$(<$V as RequestMethod>::METHOD => ...)*:$V(which is also$V::deserializefails, capture the serde error right there (plus_metaintoextensions) into a non-public variant on the#[non_exhaustive]enum (e.g.,InvalidParams { method: &'static str, error: String, extensions: Extensions }) instead of falling through toCustomRequestand re-parsing later."method"does not match any<$V as RequestMethod>::METHODdoes it deserialize asCustomRequest.handle_requestinto a single pre-check onrequest.method()(not duplicated in both the match arms and anot_servedslice):Before
match request, run onevalidate_method_served(self, request.method(), legacy_request, &context)?check that handles:!legacy_request: rejectinitialize,ping,logging/setLevel,resources/subscribe,resources/unsubscribewith-32601(fixing the bug whereSetLevelRequestandInitializeRequestwith valid params currently bypass the!legacy_requestcheck).legacy_request: rejectsubscriptions/listenwith-32601.tasks/get|tasks/update|tasks/cancel: runvalidate_tasks_capability.Then in
match request, the individual arms don't needif !legacy_request/validate_tasks_capabilityat all, andClientRequest::InvalidParams { method, error, .. }simply returnsErr(McpError::invalid_params(format!("invalid params for {method}: {error}"), None)).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.
Thanks, agreed — the second list was the weak part. I'll rework it along those lines in this PR:
RequestMethodtrait so each request type carries its method name;DeserializeforClientRequestthat dispatches onmethod, deserializes only that variant, and keeps its error in a#[doc(hidden)]InvalidParamsvariant (the enum is already#[non_exhaustive]), withCustomRequestonly for unknown methods;validate_method_servedpre-check, which also closes the bypass you spotted forinitializeandlogging/setLevelwith valid params on 2026-07-28.Should
ServerRequest(sampling and elicitation on the client side) get the same treatment here, or in a follow-up?AI disclosure: drafted with Claude Code; I reviewed it.