-
Notifications
You must be signed in to change notification settings - Fork 614
fix(transport): propagate real clientInfo to peer_info in stateless mode #1173
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
github-gregory-bougeard
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
github-gregory-bougeard:fix/stateless-client-info-placeholder
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.
+172
−14
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| //! Regression test for stateless streamable HTTP discarding the real | ||
| //! `clientInfo` from a genuine `initialize` request. | ||
| //! | ||
| //! Before the fix, `peer_info_for_stateless_request` always attached | ||
| //! `Implementation::default()` (i.e. `{"name": "rmcp", "version": "<sdk | ||
| //! version>"}`) to the synthesized `Peer`, regardless of what the client | ||
| //! actually sent in its `initialize` body. That value is what | ||
| //! `serve_inner` logs as "Service initialized as server"/"as client", and | ||
| //! what any handler sees via `context.peer.peer_info()` — so every real | ||
| //! client (regardless of its own identity) appeared identical, and | ||
| //! indistinguishable from rmcp's own placeholder, in stateless mode. | ||
| #![cfg(not(feature = "local"))] | ||
|
|
||
| use rmcp::{ | ||
| ErrorData, RoleServer, ServerHandler, | ||
| model::{ | ||
| Implementation, InitializeRequestParams, InitializeResult, ServerCapabilities, ServerInfo, | ||
| }, | ||
| service::RequestContext, | ||
| transport::streamable_http_server::{ | ||
| StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager, | ||
| }, | ||
| }; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| /// Echoes both the `clientInfo` it received as a typed handler argument and | ||
| /// the `clientInfo` visible via `context.peer.peer_info()` at that same | ||
| /// instant, via `InitializeResult::instructions`, so a black-box HTTP test | ||
| /// can compare the two without any shared in-process state. | ||
| #[derive(Clone, Default)] | ||
| struct EchoingPeerInfo; | ||
|
|
||
| impl ServerHandler for EchoingPeerInfo { | ||
| fn get_info(&self) -> ServerInfo { | ||
| ServerInfo::new(ServerCapabilities::default()) | ||
| } | ||
|
|
||
| async fn initialize( | ||
| &self, | ||
| request: InitializeRequestParams, | ||
| context: RequestContext<RoleServer>, | ||
| ) -> Result<InitializeResult, ErrorData> { | ||
| let peer_client_info: Option<Implementation> = | ||
| context.peer.peer_info().map(|p| p.client_info.clone()); | ||
| let mut info = self.get_info(); | ||
| info.instructions = Some( | ||
| serde_json::json!({ | ||
| "request_client_info": request.client_info, | ||
| "peer_client_info": peer_client_info, | ||
| }) | ||
| .to_string(), | ||
| ); | ||
| Ok(info) | ||
| } | ||
| } | ||
|
|
||
| fn stateless_json_config() -> StreamableHttpServerConfig { | ||
| StreamableHttpServerConfig::default() | ||
| .with_legacy_session_mode(false) | ||
| .with_sse_keep_alive(None) | ||
| .with_cancellation_token(CancellationToken::new()) | ||
| .with_json_response(true) | ||
| } | ||
|
|
||
| async fn spawn_server_of<H: ServerHandler + Default>( | ||
| config: StreamableHttpServerConfig, | ||
| ) -> (reqwest::Client, String, CancellationToken) { | ||
| let ct = config.cancellation_token.clone(); | ||
| let service: StreamableHttpService<H, LocalSessionManager> = | ||
| StreamableHttpService::new(|| Ok(H::default()), Default::default(), config); | ||
|
|
||
| let router = axum::Router::new().nest_service("/mcp", service); | ||
| let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
| let addr = tcp_listener.local_addr().unwrap(); | ||
|
|
||
| tokio::spawn({ | ||
| let ct = ct.clone(); | ||
| async move { | ||
| let _ = axum::serve(tcp_listener, router) | ||
| .with_graceful_shutdown(async move { ct.cancelled_owned().await }) | ||
| .await; | ||
| } | ||
| }); | ||
|
|
||
| (reqwest::Client::new(), format!("http://{addr}/mcp"), ct) | ||
| } | ||
|
|
||
| async fn post_init_with_client_info( | ||
| client: &reqwest::Client, | ||
| url: &str, | ||
| client_name: &str, | ||
| client_version: &str, | ||
| ) -> serde_json::Value { | ||
| let body = serde_json::json!({ | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "method": "initialize", | ||
| "params": { | ||
| "protocolVersion": "2025-06-18", | ||
| "capabilities": {}, | ||
| "clientInfo": {"name": client_name, "version": client_version} | ||
| } | ||
| }); | ||
| let resp = client | ||
| .post(url) | ||
| .header("Content-Type", "application/json") | ||
| .header("Accept", "application/json, text/event-stream") | ||
| .body(body.to_string()) | ||
| .send() | ||
| .await | ||
| .expect("send request"); | ||
| assert!(resp.status().is_success(), "HTTP {}", resp.status()); | ||
| resp.json().await.expect("parse JSON") | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn stateless_init_propagates_real_client_info_to_peer_info() { | ||
| let (client, url, ct) = spawn_server_of::<EchoingPeerInfo>(stateless_json_config()).await; | ||
|
|
||
| let resp = post_init_with_client_info(&client, &url, "totally-real-client", "42.0.0").await; | ||
|
|
||
| let instructions: serde_json::Value = | ||
| serde_json::from_str(resp["result"]["instructions"].as_str().unwrap()) | ||
| .expect("instructions should contain the captured JSON"); | ||
|
|
||
| // Sanity check: the handler's own typed argument really did carry the | ||
| // client's real identity — the request body was parsed correctly. | ||
| assert_eq!( | ||
| instructions["request_client_info"]["name"], "totally-real-client", | ||
| "sanity check failed — the handler never saw the real clientInfo" | ||
| ); | ||
|
|
||
| // The fix: `context.peer.peer_info()` — the exact value `serve_inner` | ||
| // logs as "Service initialized as server" — must reflect the real | ||
| // client identity for a genuine `initialize` request, not | ||
| // `Implementation::default()`. | ||
| assert_eq!( | ||
| instructions["peer_client_info"]["name"], "totally-real-client", | ||
| "Peer::peer_info() should carry the real client_info from the \ | ||
| initialize request, not a placeholder identity" | ||
| ); | ||
| assert_eq!(instructions["peer_client_info"]["version"], "42.0.0"); | ||
|
|
||
| ct.cancel(); | ||
| } |
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.
SEP-2575 requests can carry client context in
_meta.