Skip to content

Commit f3c86c4

Browse files
committed
mcp: select 2026-07-28 from any MCP-Protocol-Version copy and move header cases to datadriven
1 parent d619973 commit f3c86c4

2 files changed

Lines changed: 107 additions & 85 deletions

File tree

‎src/environmentd/src/http/mcp.rs‎

Lines changed: 53 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,21 @@ impl ProtocolVersion {
8585
/// Anything else falls back to 2025-11-25 on purpose, never a 400. A 400
8686
/// with a 2026-07-28 error tells dual-era clients the server speaks that
8787
/// revision, so they stop falling back to `initialize`.
88-
fn select(header: Option<&HeaderValue>, modern_enabled: bool) -> Self {
89-
match header.and_then(|value| value.to_str().ok()) {
90-
Some("2026-07-28") if modern_enabled => Self::V2026_07_28,
91-
_ => Self::V2025_11_25,
88+
///
89+
/// Any copy naming 2026-07-28, also inside a comma-joined list, selects it.
90+
/// Serving 2025-11-25 there would skip the header check while a proxy that
91+
/// reads that copy trusts `Mcp-Name`. `McpHeaders::check_against` then
92+
/// refuses the repetition.
93+
fn select(values: &[HeaderValue], modern_enabled: bool) -> Self {
94+
// Bytes, since a copy with a non-ASCII byte elsewhere still names it.
95+
let names_2026_07_28 = values
96+
.iter()
97+
.flat_map(|value| value.as_bytes().split(|byte| *byte == b','))
98+
.any(|version| version.trim_ascii() == b"2026-07-28");
99+
if names_2026_07_28 && modern_enabled {
100+
Self::V2026_07_28
101+
} else {
102+
Self::V2025_11_25
92103
}
93104
}
94105

@@ -119,7 +130,12 @@ impl McpHeaders {
119130
/// Rejects a request whose mirrored headers are missing or disagree with
120131
/// the body, so a proxy routing on the headers sees what we execute.
121132
fn check_against(&self, method: &McpMethod) -> Result<(), McpRequestError> {
122-
header_text(&self.protocol_version, "MCP-Protocol-Version")?;
133+
let version = header_text(&self.protocol_version, "MCP-Protocol-Version")?;
134+
if version != "2026-07-28" {
135+
return Err(McpRequestError::HeaderMismatch(format!(
136+
"MCP-Protocol-Version header value '{version}' is not '2026-07-28'"
137+
)));
138+
}
123139
let header_method = header_text(&self.method, "Mcp-Method")?;
124140
// An unknown method's name is not kept after parsing, and it executes
125141
// nothing, so only the header's presence is checked.
@@ -745,7 +761,7 @@ async fn handle_mcp_request(
745761
let read_data_product_tool_enabled = ENABLE_MCP_AGENT_READ_DATA_PRODUCT_TOOL.get(dyncfgs);
746762
let max_response_size = MCP_MAX_RESPONSE_SIZE.get(dyncfgs);
747763
let protocol = ProtocolVersion::select(
748-
mcp_headers.protocol_version.first(),
764+
&mcp_headers.protocol_version,
749765
ENABLE_MCP_PROTOCOL_2026_07_28.get(dyncfgs),
750766
);
751767
let request_timeout = MCP_REQUEST_TIMEOUT.get(dyncfgs);
@@ -2554,24 +2570,32 @@ mod tests {
25542570
fn test_protocol_version_select() {
25552571
use ProtocolVersion::*;
25562572
for (header, enabled, want) in [
2557-
(None, true, V2025_11_25),
2558-
(Some("2025-11-25"), true, V2025_11_25),
2559-
(Some("2025-06-18"), true, V2025_11_25),
2560-
(Some("2026-07-28"), true, V2026_07_28),
2561-
(Some("2026-07-28"), false, V2025_11_25),
2562-
(Some("not-a-version"), true, V2025_11_25),
2563-
(Some(" 2026-07-28"), true, V2025_11_25),
2564-
(Some("2026-07-28X"), true, V2025_11_25),
2573+
(&[][..], true, V2025_11_25),
2574+
(&["2025-11-25"], true, V2025_11_25),
2575+
(&["2025-06-18"], true, V2025_11_25),
2576+
(&["2026-07-28"], true, V2026_07_28),
2577+
(&["2026-07-28"], false, V2025_11_25),
2578+
(&["not-a-version"], true, V2025_11_25),
2579+
(&["2026-07-28X"], true, V2025_11_25),
2580+
(&["2025-11-25", "2026-07-28"], true, V2026_07_28),
2581+
(&["2025-11-25, 2026-07-28"], true, V2026_07_28),
2582+
(&["2025-11-25", "2026-07-28"], false, V2025_11_25),
25652583
] {
2566-
let value = header.map(HeaderValue::from_static);
2584+
let values: Vec<_> = header.iter().map(|v| HeaderValue::from_static(v)).collect();
25672585
assert_eq!(
2568-
ProtocolVersion::select(value.as_ref(), enabled),
2586+
ProtocolVersion::select(&values, enabled),
25692587
want,
25702588
"for {header:?} with the flag {enabled}"
25712589
);
25722590
}
25732591
let non_ascii = HeaderValue::from_bytes(b"2026-07-28\xff").expect("valid header bytes");
2574-
assert_eq!(ProtocolVersion::select(Some(&non_ascii), true), V2025_11_25);
2592+
assert_eq!(ProtocolVersion::select(&[non_ascii], true), V2025_11_25);
2593+
let non_ascii_list =
2594+
HeaderValue::from_bytes(b"\xff, 2026-07-28").expect("valid header bytes");
2595+
assert_eq!(
2596+
ProtocolVersion::select(&[non_ascii_list], true),
2597+
V2026_07_28
2598+
);
25752599
}
25762600

25772601
fn json_headers() -> HeaderMap {
@@ -2929,76 +2953,20 @@ mod tests {
29292953
);
29302954
}
29312955

2932-
fn headers(method: Option<&'static str>, name: Option<&'static str>) -> McpHeaders {
2933-
McpHeaders {
2934-
protocol_version: vec![HeaderValue::from_static("2026-07-28")],
2935-
method: method.into_iter().map(HeaderValue::from_static).collect(),
2936-
name: name.into_iter().map(HeaderValue::from_static).collect(),
2937-
}
2938-
}
2939-
2940-
fn method(body: &str) -> McpMethod {
2941-
serde_json::from_str::<McpRequest>(body)
2942-
.expect("valid request")
2943-
.method
2944-
}
2945-
29462956
#[mz_ore::test]
2947-
fn test_header_check() {
2948-
let tools_list = method(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#);
2949-
let call = method(
2950-
r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"query_system_catalog","arguments":{"sql_query":"SELECT 1"}}}"#,
2951-
);
2952-
let unknown = method(r#"{"jsonrpc":"2.0","id":1,"method":"server/other"}"#);
2953-
// "query_system_catalog" in the `=?base64?...?=` form.
2954-
let encoded = "=?base64?cXVlcnlfc3lzdGVtX2NhdGFsb2c=?=";
2955-
for (headers, method, ok) in [
2956-
(headers(Some("tools/list"), None), &tools_list, true),
2957-
(headers(None, None), &tools_list, false),
2958-
(headers(Some("tools/call"), None), &tools_list, false),
2959-
(headers(Some("Tools/List"), None), &tools_list, false),
2960-
(
2961-
headers(Some("tools/call"), Some("query_system_catalog")),
2962-
&call,
2963-
true,
2964-
),
2965-
(headers(Some("tools/call"), Some(encoded)), &call, true),
2966-
(headers(Some("tools/call"), None), &call, false),
2967-
(headers(Some("tools/call"), Some("query")), &call, false),
2968-
(
2969-
headers(Some("tools/call"), Some("=?base64?not base64?=")),
2970-
&call,
2971-
false,
2972-
),
2973-
(headers(Some("anything"), None), &unknown, true),
2974-
(headers(None, None), &unknown, false),
2975-
] {
2976-
let result = headers.check_against(method);
2977-
assert_eq!(
2978-
result.is_ok(),
2979-
ok,
2980-
"method {method} with Mcp-Method {:?}, Mcp-Name {:?}: {result:?}",
2981-
headers.method,
2982-
headers.name,
2983-
);
2984-
if let Err(err) = result {
2985-
assert_eq!(err.error_code(), error_codes::HEADER_MISMATCH);
2986-
}
2987-
}
2988-
let non_ascii = McpHeaders {
2957+
fn test_header_check_rejects_non_ascii() {
2958+
let headers = McpHeaders {
2959+
protocol_version: vec![HeaderValue::from_static("2026-07-28")],
29892960
method: vec![HeaderValue::from_bytes(b"tools/list\xff").expect("valid header bytes")],
2990-
..headers(None, None)
2961+
name: vec![],
29912962
};
2992-
assert!(non_ascii.check_against(&tools_list).is_err());
2993-
2994-
// A proxy could act on either copy of a repeated header.
2995-
let mut repeated = headers(Some("tools/list"), None);
2996-
repeated.method.push(HeaderValue::from_static("tools/call"));
2997-
assert!(repeated.check_against(&tools_list).is_err());
2998-
let mut repeated = headers(Some("tools/list"), None);
2999-
repeated
3000-
.protocol_version
3001-
.push(HeaderValue::from_static("2025-11-25"));
3002-
assert!(repeated.check_against(&tools_list).is_err());
2963+
let request = r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#;
2964+
let method = serde_json::from_str::<McpRequest>(request)
2965+
.expect("valid request")
2966+
.method;
2967+
let err = headers
2968+
.check_against(&method)
2969+
.expect_err("non-ASCII Mcp-Method");
2970+
assert_eq!(err.error_code(), error_codes::HEADER_MISMATCH);
30032971
}
30042972
}

‎src/environmentd/tests/testdata/mcp/developer_2026_07_28‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ Mcp-Name: =?base64?cXVlcnlfc3lzdGVtX2NhdGFsb2c=?=
3434
200 OK
3535
{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"[\n [\"materialize\"]\n]"}],"isError":false}}
3636

37+
# Mcp-Name is only required on tools/call.
38+
mcp-developer
39+
MCP-Protocol-Version: 2026-07-28
40+
Mcp-Method: tools/list
41+
{"jsonrpc":"2.0","id":27,"method":"tools/list"}
42+
----
43+
200 OK
44+
{"jsonrpc":"2.0","id":27,"result":{"tools":[{"name":"query_system_catalog","title":"Query System Catalog","description":"Query Materialize system catalog tables for troubleshooting and observability. Only mz_*, pg_catalog, and information_schema tables are accessible. Use the mz_internal.mz_ontology_* tables to discover tables, columns, and join paths before writing queries. Response limit: 1.0 MB.","inputSchema":{"type":"object","properties":{"sql_query":{"type":"string","description":"PostgreSQL-compatible SELECT, SHOW, or EXPLAIN query referencing mz_*, pg_catalog, or information_schema tables"}},"required":["sql_query"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}},{"name":"query","title":"Query","description":"Execute a read-only SQL query (SELECT, SHOW, or EXPLAIN) against any object the role can access, including system catalog and user objects. Requires a cluster, which is what enables EXPLAIN ANALYZE and queries against indexed user objects. For pure system catalog lookups that do not need a cluster, prefer `query_system_catalog`. Response limit: 1.0 MB.","inputSchema":{"type":"object","properties":{"cluster":{"type":"string","description":"Exact cluster name the query should run on. Required: EXPLAIN ANALYZE and queries against indexed user objects need a specific cluster to execute on."},"cluster_replica":{"type":"string","description":"Optional replica name (e.g. 'r1') to target one replica of the cluster. Required for EXPLAIN ANALYZE on clusters with more than one replica. Find replica names in mz_catalog.mz_cluster_replicas."},"sql_query":{"type":"string","description":"PostgreSQL-compatible SELECT, SHOW, or EXPLAIN statement. Multi-statement queries are rejected."}},"required":["cluster","sql_query"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}}]}}
45+
3746
mcp-developer
3847
MCP-Protocol-Version: 2026-07-28
3948
{"jsonrpc":"2.0","id":3,"method":"tools/list"}
@@ -66,6 +75,23 @@ Mcp-Name: query
6675
400 Bad Request
6776
{"jsonrpc":"2.0","id":6,"error":{"code":-32020,"message":"Header mismatch: Mcp-Name header value 'query' does not match body value 'query_system_catalog'","data":{"error_type":"HeaderMismatch"}}}
6877

78+
mcp-developer
79+
MCP-Protocol-Version: 2026-07-28
80+
Mcp-Method: Tools/List
81+
{"jsonrpc":"2.0","id":22,"method":"tools/list"}
82+
----
83+
400 Bad Request
84+
{"jsonrpc":"2.0","id":22,"error":{"code":-32020,"message":"Header mismatch: Mcp-Method header value 'Tools/List' does not match body value 'tools/list'","data":{"error_type":"HeaderMismatch"}}}
85+
86+
mcp-developer
87+
MCP-Protocol-Version: 2026-07-28
88+
Mcp-Method: tools/call
89+
Mcp-Name: =?base64?not base64?=
90+
{"jsonrpc":"2.0","id":23,"method":"tools/call","params":{"name":"query_system_catalog","arguments":{"sql_query":"SELECT 1"}}}
91+
----
92+
400 Bad Request
93+
{"jsonrpc":"2.0","id":23,"error":{"code":-32020,"message":"Header mismatch: Mcp-Name header has invalid Base64","data":{"error_type":"HeaderMismatch"}}}
94+
6995
# The transport leaves header rules for notifications undefined, so a
7096
# notification without Mcp-Method is still accepted.
7197
mcp-developer
@@ -92,6 +118,34 @@ Mcp-Method: tools/call
92118
400 Bad Request
93119
{"jsonrpc":"2.0","id":8,"error":{"code":-32020,"message":"Header mismatch: repeated Mcp-Method header","data":{"error_type":"HeaderMismatch"}}}
94120

121+
# Any copy naming 2026-07-28 selects it, so the repetition is refused instead
122+
# of served as 2025-11-25 without the header check.
123+
mcp-developer
124+
MCP-Protocol-Version: 2025-11-25
125+
MCP-Protocol-Version: 2026-07-28
126+
Mcp-Method: tools/list
127+
{"jsonrpc":"2.0","id":24,"method":"tools/list"}
128+
----
129+
400 Bad Request
130+
{"jsonrpc":"2.0","id":24,"error":{"code":-32020,"message":"Header mismatch: repeated MCP-Protocol-Version header","data":{"error_type":"HeaderMismatch"}}}
131+
132+
mcp-developer
133+
MCP-Protocol-Version: 2026-07-28
134+
MCP-Protocol-Version: 2025-11-25
135+
Mcp-Method: tools/list
136+
{"jsonrpc":"2.0","id":25,"method":"tools/list"}
137+
----
138+
400 Bad Request
139+
{"jsonrpc":"2.0","id":25,"error":{"code":-32020,"message":"Header mismatch: repeated MCP-Protocol-Version header","data":{"error_type":"HeaderMismatch"}}}
140+
141+
mcp-developer
142+
MCP-Protocol-Version: 2025-11-25, 2026-07-28
143+
Mcp-Method: tools/list
144+
{"jsonrpc":"2.0","id":26,"method":"tools/list"}
145+
----
146+
400 Bad Request
147+
{"jsonrpc":"2.0","id":26,"error":{"code":-32020,"message":"Header mismatch: MCP-Protocol-Version header value '2025-11-25, 2026-07-28' is not '2026-07-28'","data":{"error_type":"HeaderMismatch"}}}
148+
95149
# An unknown method only needs Mcp-Method to be present.
96150
mcp-developer
97151
MCP-Protocol-Version: 2026-07-28

0 commit comments

Comments
 (0)