|
| 1 | +#![cfg(not(feature = "local"))] |
| 2 | +//! Regression tests for the `MCP-Protocol-Version` header / initialize body consistency check. |
| 3 | +use rmcp::transport::streamable_http_server::{ |
| 4 | + StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager, |
| 5 | +}; |
| 6 | +use tokio_util::sync::CancellationToken; |
| 7 | + |
| 8 | +mod common; |
| 9 | +use common::calculator::Calculator; |
| 10 | + |
| 11 | +fn init_body(body_version: &str) -> String { |
| 12 | + format!( |
| 13 | + r#"{{"jsonrpc":"2.0","id":1,"method":"initialize","params":{{"protocolVersion":"{body_version}","capabilities":{{}},"clientInfo":{{"name":"test","version":"1.0"}}}}}}"# |
| 14 | + ) |
| 15 | +} |
| 16 | + |
| 17 | +async fn spawn_server( |
| 18 | + config: StreamableHttpServerConfig, |
| 19 | +) -> (reqwest::Client, String, CancellationToken) { |
| 20 | + let ct = config.cancellation_token.clone(); |
| 21 | + let service: StreamableHttpService<Calculator, LocalSessionManager> = |
| 22 | + StreamableHttpService::new(|| Ok(Calculator::new()), Default::default(), config); |
| 23 | + |
| 24 | + let router = axum::Router::new().nest_service("/mcp", service); |
| 25 | + let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 26 | + let addr = tcp_listener.local_addr().unwrap(); |
| 27 | + |
| 28 | + tokio::spawn({ |
| 29 | + let ct = ct.clone(); |
| 30 | + async move { |
| 31 | + let _ = axum::serve(tcp_listener, router) |
| 32 | + .with_graceful_shutdown(async move { ct.cancelled_owned().await }) |
| 33 | + .await; |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + let client = reqwest::Client::new(); |
| 38 | + let base_url = format!("http://{addr}/mcp"); |
| 39 | + (client, base_url, ct) |
| 40 | +} |
| 41 | + |
| 42 | +fn stateless_json_config() -> StreamableHttpServerConfig { |
| 43 | + StreamableHttpServerConfig::default() |
| 44 | + .with_stateful_mode(false) |
| 45 | + .with_json_response(true) |
| 46 | + .with_sse_keep_alive(None) |
| 47 | + .with_cancellation_token(CancellationToken::new()) |
| 48 | +} |
| 49 | + |
| 50 | +fn stateful_config() -> StreamableHttpServerConfig { |
| 51 | + StreamableHttpServerConfig::default() |
| 52 | + .with_stateful_mode(true) |
| 53 | + .with_sse_keep_alive(None) |
| 54 | + .with_cancellation_token(CancellationToken::new()) |
| 55 | +} |
| 56 | + |
| 57 | +async fn post_init( |
| 58 | + client: &reqwest::Client, |
| 59 | + url: &str, |
| 60 | + header: Option<&str>, |
| 61 | + body_version: &str, |
| 62 | +) -> reqwest::Response { |
| 63 | + let mut req = client |
| 64 | + .post(url) |
| 65 | + .header("Content-Type", "application/json") |
| 66 | + .header("Accept", "application/json, text/event-stream") |
| 67 | + .body(init_body(body_version)); |
| 68 | + if let Some(h) = header { |
| 69 | + req = req.header("MCP-Protocol-Version", h); |
| 70 | + } |
| 71 | + req.send().await.expect("send initialize request") |
| 72 | +} |
| 73 | + |
| 74 | +#[tokio::test] |
| 75 | +async fn stateless_init_rejects_when_header_older_than_body() -> anyhow::Result<()> { |
| 76 | + let (client, url, ct) = spawn_server(stateless_json_config()).await; |
| 77 | + |
| 78 | + let response = post_init(&client, &url, Some("2025-03-26"), "2025-11-25").await; |
| 79 | + assert_eq!(response.status(), 400); |
| 80 | + |
| 81 | + let body: serde_json::Value = response.json().await?; |
| 82 | + assert_eq!(body["error"]["code"], -32600); |
| 83 | + assert!( |
| 84 | + body["error"]["message"] |
| 85 | + .as_str() |
| 86 | + .unwrap_or_default() |
| 87 | + .contains("MCP-Protocol-Version"), |
| 88 | + "expected error message to mention the header, got: {body}" |
| 89 | + ); |
| 90 | + |
| 91 | + ct.cancel(); |
| 92 | + Ok(()) |
| 93 | +} |
| 94 | + |
| 95 | +#[tokio::test] |
| 96 | +async fn stateless_init_rejects_when_header_newer_than_body() -> anyhow::Result<()> { |
| 97 | + let (client, url, ct) = spawn_server(stateless_json_config()).await; |
| 98 | + |
| 99 | + let response = post_init(&client, &url, Some("2025-11-25"), "2025-03-26").await; |
| 100 | + assert_eq!(response.status(), 400); |
| 101 | + |
| 102 | + let body: serde_json::Value = response.json().await?; |
| 103 | + assert_eq!(body["error"]["code"], -32600); |
| 104 | + |
| 105 | + ct.cancel(); |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +#[tokio::test] |
| 110 | +async fn stateless_init_accepts_when_header_matches_body() -> anyhow::Result<()> { |
| 111 | + let (client, url, ct) = spawn_server(stateless_json_config()).await; |
| 112 | + |
| 113 | + let response = post_init(&client, &url, Some("2025-11-25"), "2025-11-25").await; |
| 114 | + assert_eq!(response.status(), 200); |
| 115 | + |
| 116 | + let body: serde_json::Value = response.json().await?; |
| 117 | + assert!( |
| 118 | + body["result"].is_object(), |
| 119 | + "expected an InitializeResult, got: {body}" |
| 120 | + ); |
| 121 | + |
| 122 | + ct.cancel(); |
| 123 | + Ok(()) |
| 124 | +} |
| 125 | + |
| 126 | +#[tokio::test] |
| 127 | +async fn stateless_init_accepts_when_header_absent() -> anyhow::Result<()> { |
| 128 | + let (client, url, ct) = spawn_server(stateless_json_config()).await; |
| 129 | + |
| 130 | + let response = post_init(&client, &url, None, "2025-11-25").await; |
| 131 | + assert_eq!(response.status(), 200); |
| 132 | + |
| 133 | + ct.cancel(); |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | +#[tokio::test] |
| 138 | +async fn stateful_init_rejects_when_header_mismatches_body() -> anyhow::Result<()> { |
| 139 | + let (client, url, ct) = spawn_server(stateful_config()).await; |
| 140 | + |
| 141 | + let response = post_init(&client, &url, Some("2024-11-05"), "2025-11-25").await; |
| 142 | + assert_eq!(response.status(), 400); |
| 143 | + |
| 144 | + let body: serde_json::Value = response.json().await?; |
| 145 | + assert_eq!(body["error"]["code"], -32600); |
| 146 | + |
| 147 | + ct.cancel(); |
| 148 | + Ok(()) |
| 149 | +} |
0 commit comments