Files
crank/apps/mcp-server/tests/integration/revision_matrix/method_name.rs
T
Hermes DevOps 75fe5a123c [skip ci] wip: preserve Hermes crank development checkpoint
Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
2026-10-25 10:00:22 +00:00

289 lines
10 KiB
Rust

//! Required `Mcp-Name`/method identity on modern requests, and the ordering rule that the name is
//! checked before an unsupported-method dispatch.
//!
//! Tests moved verbatim from `revision_matrix.rs` (Story 3.6, kanban t_987bf309); only the module
//! prefix changed.
use super::super::common::*;
use axum::http::header;
use serde_json::{Value, json};
use super::harness::{
MODERN_REVISION, endpoint, json_body, modern_headers, modern_post, modern_request_with_params,
};
/// R7: missing or mismatched `Mcp-Method` is a validation failure.
#[tokio::test]
async fn r7_modern_missing_mcp_method_is_rejected() {
let endpoint = endpoint().await;
let headers = vec![("MCP-Protocol-Version", MODERN_REVISION.to_owned())];
let message = modern_request_with_params(Some(80), "tools/list", json!({}));
let response = modern_post(&endpoint, &headers, message, None).await;
assert_eq!(response.status().as_u16(), 400);
let body = json_body(response).await;
assert_eq!(
body["id"],
json!(80),
"the request id must be preserved, got {body}"
);
assert_eq!(body["error"]["code"], json!(-32020), "got {body}");
assert!(body["result"].is_null(), "got {body}");
}
#[tokio::test]
async fn r7_modern_mismatched_mcp_method_is_rejected() {
let endpoint = endpoint().await;
let headers = modern_headers(MODERN_REVISION, "tools/list");
let message = modern_request_with_params(Some(81), "server/discover", json!({}));
let response = modern_post(&endpoint, &headers, message, None).await;
assert_eq!(response.status().as_u16(), 400);
let body = json_body(response).await;
assert_eq!(
body["id"],
json!(81),
"the request id must be preserved, got {body}"
);
assert_eq!(body["error"]["code"], json!(-32020), "got {body}");
assert!(body["result"].is_null(), "got {body}");
}
/// R9: an unknown RPC on the modern era answers `404` + `-32601` with the request id preserved.
#[tokio::test]
async fn r9_modern_unknown_rpc_is_method_not_found() {
let endpoint = endpoint().await;
let headers = modern_headers(MODERN_REVISION, "tools/unsupported_thing");
let message = modern_request_with_params(Some(90), "tools/unsupported_thing", json!({}));
let response = modern_post(&endpoint, &headers, message, Some("r9-unknown-rpc")).await;
assert_eq!(response.status().as_u16(), 404);
let body = json_body(response).await;
assert_eq!(
body["id"],
json!(90),
"the request id must be preserved, got {body}"
);
assert_eq!(body["error"]["code"], json!(-32601), "got {body}");
assert!(body["result"].is_null(), "got {body}");
}
/// Review pass 6 (P5 / matrix row 6): `Mcp-Name` is a **required** modern header for `tools/call`,
/// `resources/read` and `prompts/get` (`streamable-http.mdx` "Standard Request Headers"), and its
/// validation must happen **before** the business dispatch. Both standard Resource/Prompt requests
/// implement their handler now (stories 3.8 and 3.14), so the missing/mismatched halves assert the
/// `-32020` header diagnostic and a correctly declared name reaches the implemented handler.
async fn modern_standard_method_name_outcome(
method: &'static str,
params: Value,
declared_name: Option<&str>,
correlation: &str,
) -> (u16, String) {
let endpoint = endpoint().await;
let mut headers = modern_headers(MODERN_REVISION, method);
if let Some(name) = declared_name {
headers.push(("Mcp-Name", name.to_owned()));
}
let response = modern_post(
&endpoint,
&headers,
modern_request_with_params(Some(150), method, params),
Some(correlation),
)
.await;
let status = response.status().as_u16();
(status, response.text().await.unwrap())
}
/// The `Mcp-Name` half of the row-6 oracle: a missing or mismatched required name header is a
/// `400` + `-32020` `HeaderMismatch` that keeps the request id, the request correlation and no result.
///
/// Review pass 7 (G2): the correlation assertion was missing entirely here even though the build
/// walkthrough claimed it was present on this half; the complete envelope is now pinned for both
/// standard names.
fn assert_required_name_header_is_a_structured_mismatch(
status: u16,
raw: &str,
correlation: &str,
case: &str,
) {
assert_eq!(
status, 400,
"{case}: row 6 requires a header-validation failure for a missing/mismatched `Mcp-Name`, got {raw}"
);
let body: Value = serde_json::from_str(raw).unwrap_or_else(|error| {
panic!(
"{case}: the `Mcp-Name` diagnostic must be a JSON-RPC envelope, got {raw:?}: {error}"
)
});
assert_eq!(body["jsonrpc"], "2.0", "{case}: got {body}");
assert_eq!(
body["id"],
json!(150),
"{case}: the diagnostic must keep the request id, got {body}"
);
assert_eq!(
body["error"]["code"],
json!(-32020),
"{case}: row 6 returns `-32020` `HeaderMismatch`, got {body}"
);
assert!(
body["error"]["message"]
.as_str()
.is_some_and(|message| !message.trim().is_empty()),
"{case}: the diagnostic must carry a non-empty message, got {body}"
);
assert_eq!(
body["error"]["data"]["request_id"],
json!(correlation),
"{case}: the diagnostic must preserve the request correlation, got {body}"
);
assert!(
body.get("result").is_none(),
"{case}: a rejected request must not carry a result, got {body}"
);
}
/// Review pass 6 (P5): `resources/read` takes its `Mcp-Name` from `params.uri`. Since Story 3.8 the
/// method is implemented, so the name is validated before the Resource handler rather than before the
/// unsupported-method answer.
#[tokio::test]
async fn review6_p5_modern_resources_read_requires_mcp_name_before_resource_dispatch() {
let uri = "crank://operations/crm_create_lead";
let missing = "review6-p5-resources-missing-name";
let (status, raw) =
modern_standard_method_name_outcome("resources/read", json!({ "uri": uri }), None, missing)
.await;
assert_required_name_header_is_a_structured_mismatch(
status,
&raw,
missing,
"resources/read missing name",
);
let mismatched = "review6-p5-resources-mismatched-name";
let (status, raw) = modern_standard_method_name_outcome(
"resources/read",
json!({ "uri": uri }),
Some("crank://operations/other"),
mismatched,
)
.await;
assert_required_name_header_is_a_structured_mismatch(
status,
&raw,
mismatched,
"resources/read mismatched name",
);
let valid = "review6-p5-resources-valid-name";
let (status, raw) = modern_standard_method_name_outcome(
"resources/read",
json!({ "uri": uri }),
Some(uri),
valid,
)
.await;
// Story 3.8: `resources/read` is an implemented capability, so a correctly declared name reaches
// the Resource handler. An unbound URI is answered by one safe, non-disclosing outcome instead of
// the era-correct unsupported-method answer.
assert_eq!(
status, 200,
"a correctly declared `Mcp-Name` must reach the Resource handler, got {raw}"
);
let body: Value = serde_json::from_str(&raw).expect("the safe answer is a JSON-RPC envelope");
assert_eq!(body["jsonrpc"], "2.0", "got {body}");
assert_eq!(
body["id"],
json!(150),
"the answer must keep the request id: {body}"
);
assert_eq!(
body["error"]["code"],
json!(-32602),
"an unbound Resource URI is safe invalid params, got {body}"
);
assert_eq!(
body["error"]["data"]["request_id"],
json!(valid),
"the safe answer must preserve the request correlation, got {body}"
);
assert!(
body.get("result").is_none(),
"a denied Resource read must not carry a result, got {body}"
);
}
/// Review pass 6 (P5): `prompts/get` takes its `Mcp-Name` from `params.name`.
#[tokio::test]
async fn review6_p5_modern_prompts_get_requires_mcp_name_before_unsupported_dispatch() {
let missing = "review6-p5-prompts-missing-name";
let (status, raw) = modern_standard_method_name_outcome(
"prompts/get",
json!({ "name": "crm_create_lead" }),
None,
missing,
)
.await;
assert_required_name_header_is_a_structured_mismatch(
status,
&raw,
missing,
"prompts/get missing name",
);
let mismatched = "review6-p5-prompts-mismatched-name";
let (status, raw) = modern_standard_method_name_outcome(
"prompts/get",
json!({ "name": "crm_create_lead" }),
Some("crm_other_lead"),
mismatched,
)
.await;
assert_required_name_header_is_a_structured_mismatch(
status,
&raw,
mismatched,
"prompts/get mismatched name",
);
let valid = "review6-p5-prompts-valid-name";
let (status, raw) = modern_standard_method_name_outcome(
"prompts/get",
json!({ "name": "crm_create_lead" }),
Some("crm_create_lead"),
valid,
)
.await;
// Story 3.14 implements the standard Prompt requests, so a correctly declared name no longer
// reaches the unsupported-method answer: it is a business answer for a Prompt that this Agent
// does not bind, and the row-6 header diagnostic is still asserted on the two halves above.
assert_eq!(
status, 200,
"a correctly declared `prompts/get` name must reach the implemented handler, got {raw}"
);
let body: Value =
serde_json::from_str(&raw).expect("the business answer is a JSON-RPC envelope");
assert_eq!(body["error"]["code"], json!(-32602), "got {body}");
assert_eq!(
body["error"]["message"],
json!("the requested prompt is not available"),
"an unbound Prompt name stays a safe business answer, got {body}"
);
assert_eq!(
body["error"]["data"]["request_id"],
json!(valid),
"the safe answer must preserve the request correlation, got {body}"
);
assert!(
body.get("result").is_none(),
"an unbound Prompt must not carry a result, got {body}"
);
}