Repository navigation
fix: reject unadvertised capability methods by default #1446
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
xixi-box
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
xixi-box:fix/issue-1445-f852ec0-6401d18d
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,126 @@ | ||
| #![cfg(all(feature = "client", feature = "server"))] | ||
|
|
||
| use rmcp::{ | ||
| ClientHandler, RoleClient, RoleServer, ServerHandler, | ||
| model::{ | ||
| ArgumentInfo, ClientConfig, CompleteRequestParams, ErrorCode, InitializeResult, | ||
| ProtocolVersion, Reference, ServerCapabilities, ServerConfig, | ||
| }, | ||
| service::{ServiceError, serve_directly}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct CapabilityServer(ServerCapabilities); | ||
|
|
||
| impl ServerHandler for CapabilityServer { | ||
| fn get_info(&self) -> ServerConfig { | ||
| ServerConfig::new(self.0.clone()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct ProbeClient; | ||
|
|
||
| impl ClientHandler for ProbeClient { | ||
| fn get_info(&self) -> ClientConfig { | ||
| let mut info = ClientConfig::default(); | ||
| info.protocol_version = ProtocolVersion::V_2026_07_28; | ||
| info | ||
| } | ||
| } | ||
|
|
||
| fn expect_result<T: std::fmt::Debug>( | ||
| method: &str, | ||
| result: Result<T, ServiceError>, | ||
| advertised: bool, | ||
| ) -> Option<T> { | ||
| if advertised { | ||
| Some(result.unwrap_or_else(|error| panic!("{method} should succeed: {error:?}"))) | ||
| } else { | ||
| match result { | ||
| Err(ServiceError::McpError(error)) => { | ||
| assert_eq!(error.code, ErrorCode::METHOD_NOT_FOUND, "{method}"); | ||
| } | ||
| other => panic!("{method} should return method not found: {other:?}"), | ||
| } | ||
| None | ||
| } | ||
| } | ||
|
|
||
| async fn check_capabilities(capabilities: ServerCapabilities) { | ||
| let prompts = capabilities.prompts.is_some(); | ||
| let completions = capabilities.completions.is_some(); | ||
| let resources = capabilities.resources.is_some(); | ||
| let tools = capabilities.tools.is_some(); | ||
|
|
||
| let (server_transport, client_transport) = tokio::io::duplex(4096); | ||
| let server = serve_directly::<RoleServer, _, _, _, _>( | ||
| CapabilityServer(capabilities), | ||
| server_transport, | ||
| Some(ProbeClient.get_info()), | ||
| ); | ||
| let mut peer_info = InitializeResult::default(); | ||
| peer_info.protocol_version = ProtocolVersion::V_2026_07_28; | ||
| let client = serve_directly::<RoleClient, _, _, _, _>( | ||
| ProbeClient, | ||
| client_transport, | ||
| Some(peer_info.into()), | ||
| ); | ||
|
|
||
| if let Some(result) = expect_result("prompts/list", client.list_prompts(None).await, prompts) { | ||
| assert!(result.prompts.is_empty()); | ||
| } | ||
| if let Some(result) = expect_result( | ||
| "completion/complete", | ||
| client | ||
| .complete(CompleteRequestParams::new( | ||
| Reference::for_prompt("unused"), | ||
| ArgumentInfo::new("argument", "value"), | ||
| )) | ||
| .await, | ||
| completions, | ||
| ) { | ||
| assert!(result.completion.values.is_empty()); | ||
| } | ||
| if let Some(result) = expect_result( | ||
| "resources/list", | ||
| client.list_resources(None).await, | ||
| resources, | ||
| ) { | ||
| assert!(result.resources.is_empty()); | ||
| } | ||
| if let Some(result) = expect_result( | ||
| "resources/templates/list", | ||
| client.list_resource_templates(None).await, | ||
| resources, | ||
| ) { | ||
| assert!(result.resource_templates.is_empty()); | ||
| } | ||
| if let Some(result) = expect_result("tools/list", client.list_tools(None).await, tools) { | ||
| assert!(result.tools.is_empty()); | ||
| } | ||
|
|
||
| client.cancel().await.expect("stop client"); | ||
| server.cancel().await.expect("stop server"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn default_list_methods_follow_advertised_capabilities() { | ||
| // LocalSet also lets this test run with --all-features, where rmcp's | ||
| // `local` feature uses spawn_local for its service tasks. | ||
| tokio::task::LocalSet::new() | ||
| .run_until(async { | ||
| check_capabilities(ServerCapabilities::default()).await; | ||
| check_capabilities(ServerCapabilities::builder().enable_tools().build()).await; | ||
| check_capabilities( | ||
| ServerCapabilities::builder() | ||
| .enable_tools() | ||
| .enable_resources() | ||
| .enable_prompts() | ||
| .enable_completions() | ||
| .build(), | ||
| ) | ||
| .await; | ||
| }) | ||
| .await; | ||
| } |
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.
In the non-
localtest configuration,tests/test_stateless_server_requests.rs:24still definesStatelessServerwith an emptyServerHandlerimplementation, but the assertions at lines 100–103 and 245–248 require its validtools/listrequests to succeed. This branch now returns-32601instead, so both tests fail. The default-info handlers intest_discover_http_client_startup.rshave the same problem, and the resource gate likewise breaks Calculator-backed session-store/stale-session tests that still calllist_all_resources()despite Calculator advertising only tools. Update the remaining success-path fixtures to declare the capabilities they exercise, or change their requests to supported methods.Useful? React with 👍 / 👎.
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.
Addressed in
2efd7c0. Thanks for catching the remaining fixtures.tools; the sharedTestServerfixture does too.tools/listmethod, including the raw inline-protocol request.test_server_initializationpassed (22 tests).The full non-
localCI run is being checked on this commit.