//! Story 3.18 acceptance tests: safe MCP server logging (AT-3.18-01..03, FR-19). //! //! Oracles: `docs/acceptance-test-plan.md` AT-3.18-01..03, `epics.md` Story 3.18 and the pinned //! `logging`/`notifications/message`/`LoggingLevel` schemas of both supported revisions //! (`2026-07-28` carries the level per request in `_meta`; `2025-11-25` serves `logging/setLevel`). //! Every request travels through the real HTTP endpoint built by `build_test_app` over a real //! PostgreSQL registry; the level is set through the production request path. //! //! The delivered events are observed on the SSE response stream, which is the only carrier the //! pinned transport gives a server notification: a JSON body has no place for one. Where a pending //! notification exists and the client offered `text/event-stream`, the negotiated mode is the stream. //! //! No external MCP client is exercised here: these are local harness results against the pinned wire //! shapes, and the story handoff states that honestly. use std::time::Duration; use crank_core::PlatformApiKeyScope; use crank_registry::PostgresRegistry; use serde_json::{Value, json}; use super::common::{ agent_mcp_url, build_test_app, create_platform_api_key, initialize_session, publish_agent_with_bindings, spawn_mcp_server, test_registry, }; const MODERN_REVISION: &str = "2026-07-28"; const LEGACY_REVISION: &str = "2025-11-25"; const META_PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion"; const META_LOG_LEVEL: &str = "io.modelcontextprotocol/logLevel"; const META_CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities"; const META_CLIENT_INFO: &str = "io.modelcontextprotocol/clientInfo"; const AGENT_SLUG: &str = "logging-agent"; const OTHER_AGENT_SLUG: &str = "logging-other"; /// The one safe wire detail of a malformed logging level. const INVALID_LEVEL: &str = "invalid logging level"; /// The fixed logger name of a delivered notification. const LOGGER_NAME: &str = "crank-mcp-server"; /// A value that must never appear in a delivered notification. It is distinctive on purpose. const CANARY: &str = "crank-canary-9f7620e5-aa41-4b0d"; struct LoggingEndpoint { client: reqwest::Client, base_url: String, api_key: String, other_api_key: String, registry: PostgresRegistry, } impl LoggingEndpoint { fn url(&self, agent_slug: &str) -> String { agent_mcp_url(&self.base_url, agent_slug) } /// One request whose response is negotiated as the SSE stream, so a pending notification really /// has a carrier. The body is split into its notifications and the final JSON-RPC response. async fn post_stream( &self, agent_slug: &str, api_key: &str, headers: &[(&str, &str)], payload: Value, ) -> StreamReply { let mut request = self .client .post(self.url(agent_slug)) .header(reqwest::header::ACCEPT, "text/event-stream") .header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}")); for (name, value) in headers { request = request.header(*name, *value); } let response = request .json(&payload) .send() .await .expect("the MCP endpoint answers"); let status = response.status(); let content_type = response .headers() .get(reqwest::header::CONTENT_TYPE) .and_then(|value| value.to_str().ok()) .unwrap_or_default() .to_owned(); let body = response.text().await.expect("the stream body is readable"); StreamReply::parse(status, content_type, body) } /// One legacy request inside the initialized session of `agent_slug`. async fn legacy_post( &self, agent_slug: &str, api_key: &str, session_id: &str, payload: Value, ) -> StreamReply { self.post_stream( agent_slug, api_key, &[ ("MCP-Protocol-Version", LEGACY_REVISION), ("MCP-Session-Id", session_id), ], payload, ) .await } /// One `logging/setLevel` request of the legacy era. async fn set_level(&self, session_id: &str, level: Value) -> StreamReply { self.legacy_post( AGENT_SLUG, &self.api_key, session_id, json!({ "jsonrpc": "2.0", "id": 41, "method": "logging/setLevel", "params": {"level": level} }), ) .await } /// One modern request of an arbitrary method with the required per-request identity. async fn modern_stream(&self, method: &str, params: Value) -> StreamReply { let mut params = params; params["_meta"] = json!({ META_PROTOCOL_VERSION: MODERN_REVISION, META_CLIENT_CAPABILITIES: {}, META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"} }); self.modern_stream_with_params(method, params).await } /// The same, with the caller's `params` (including a chosen `_meta`). async fn modern_stream_with_params(&self, method: &str, params: Value) -> StreamReply { self.post_stream( AGENT_SLUG, &self.api_key, &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", method), ], json!({"jsonrpc": "2.0", "id": 51, "method": method, "params": params}), ) .await } async fn legacy_session(&self, agent_slug: &str, api_key: &str) -> String { initialize_session(&self.client, &self.url(agent_slug), api_key).await } } struct StreamReply { status: reqwest::StatusCode, content_type: String, /// The JSON-RPC response, which is the last event of the stream. response: Value, /// The `notifications/message` bodies delivered before it. notifications: Vec, /// The raw body, so a test can search it for a canary value. raw: String, } impl StreamReply { /// Parse a reply that may arrive either as an SSE stream (a pending notification needs the /// stream) or as the plain JSON body of a method this era does not serve. fn parse(status: reqwest::StatusCode, content_type: String, body: String) -> Self { let mut messages = Vec::new(); for block in body.split("\n\n") { for line in block.lines() { let Some(data) = line.strip_prefix("data:") else { continue; }; let data = data.trim_start(); if data.is_empty() { continue; } let value: Value = serde_json::from_str(data) .unwrap_or_else(|error| panic!("every stream event is JSON: {data:?} {error}")); messages.push(value); } } if messages.is_empty() { // An era-correct rejection of an unserved method is answered as a plain JSON body: it // never carries a notification, so there is nothing to stream. let response: Value = serde_json::from_str(&body) .unwrap_or_else(|error| panic!("expected a JSON-RPC body, got {body:?}: {error}")); return Self { status, content_type, response, notifications: Vec::new(), raw: body, }; } let response = messages .pop() .unwrap_or_else(|| panic!("the stream must end with the JSON-RPC response: {body:?}")); Self { status, content_type, response, notifications: messages, raw: body, } } fn result(&self) -> &Value { self.response .get("result") .unwrap_or_else(|| panic!("expected a JSON-RPC result, got {}", self.response)) } fn error(&self) -> (i64, String) { let code = self .response .pointer("/error/code") .and_then(Value::as_i64) .unwrap_or_else(|| panic!("expected a JSON-RPC error, got {}", self.response)); let message = self .response .pointer("/error/message") .and_then(Value::as_str) .unwrap_or_default() .to_owned(); (code, message) } fn assert_stream(&self) { assert_eq!(self.status, reqwest::StatusCode::OK); assert!( self.content_type.starts_with("text/event-stream"), "a streamed answer must carry the SSE content type, got {:?}", self.content_type ); } } /// The exact closed body of one delivered event. Nothing else is allowed to be carried. fn expected_notification(level: &str, event: &str, outcome: &str) -> Value { json!({ "jsonrpc": "2.0", "method": "notifications/message", "params": { "level": level, "logger": LOGGER_NAME, "data": {"event": event, "outcome": outcome} } }) } async fn logging_endpoint() -> LoggingEndpoint { let registry = test_registry().await; publish_agent_with_bindings(®istry, AGENT_SLUG, Vec::new()).await; publish_agent_with_bindings(®istry, OTHER_AGENT_SLUG, Vec::new()).await; let api_key = create_platform_api_key( ®istry, AGENT_SLUG, "mcp-logging", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let other_api_key = create_platform_api_key( ®istry, OTHER_AGENT_SLUG, "mcp-logging-other", &[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], ) .await; let base_url = spawn_mcp_server(build_test_app( registry.clone(), Duration::from_millis(0), Some("https://crank.example.com".to_owned()), )) .await; LoggingEndpoint { client: reqwest::Client::new(), base_url, api_key, other_api_key, registry, } } /// AT-3.18-01/03: the advertisement equals the implemented surface on both revisions. #[tokio::test] async fn advertisement_declares_the_logging_capability_on_both_revisions() { let endpoint = logging_endpoint().await; let discover = endpoint .client .post(endpoint.url(AGENT_SLUG)) .header( reqwest::header::ACCEPT, "application/json, text/event-stream", ) .header( reqwest::header::AUTHORIZATION, format!("Bearer {}", endpoint.api_key), ) .header("MCP-Protocol-Version", MODERN_REVISION) .header("Mcp-Method", "server/discover") .json(&json!({ "jsonrpc": "2.0", "id": 1, "method": "server/discover", "params": {"_meta": { META_PROTOCOL_VERSION: MODERN_REVISION, META_CLIENT_CAPABILITIES: {}, META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"} }} })) .send() .await .expect("the endpoint answers"); let discover: Value = discover.json().await.expect("a JSON body"); assert_eq!( discover["result"]["capabilities"]["logging"], json!({}), "the modern advertisement must declare the implemented logging capability: {discover}" ); let initialize = endpoint .client .post(endpoint.url(AGENT_SLUG)) .header( reqwest::header::ACCEPT, "application/json, text/event-stream", ) .header( reqwest::header::AUTHORIZATION, format!("Bearer {}", endpoint.api_key), ) .json(&json!({ "jsonrpc": "2.0", "id": 2, "method": "initialize", "params": {"protocolVersion": LEGACY_REVISION, "capabilities": {}} })) .send() .await .expect("the endpoint answers"); let initialize: Value = initialize.json().await.expect("a JSON body"); assert_eq!( initialize["result"]["capabilities"]["logging"], json!({}), "the legacy advertisement must declare the implemented logging capability: {initialize}" ); // Control: a capability this story does not implement is still absent, so the assertion above is // not a blanket "advertise everything" change. assert!( initialize["result"]["capabilities"] .get("io.modelcontextprotocol/tasks") .is_none(), "an unimplemented extension must not appear: {initialize}" ); endpoint.registry.ping().await.expect("the registry is up"); } /// AT-3.18-01: setting a valid legacy level is acknowledged with one safe structured event. #[tokio::test] async fn legacy_set_level_acknowledges_with_a_closed_safe_event() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; let reply = endpoint.set_level(&session, json!("debug")).await; reply.assert_stream(); assert_eq!( reply.result(), &json!({}), "the pinned `logging/setLevel` result is empty: {}", reply.response ); assert_eq!( reply.notifications, vec![ expected_notification("notice", "mcp.logging.level_applied", "applied"), expected_notification("info", "mcp.request.served", "ok"), ], "the acknowledgment carries the applied level and the served outcome, nothing else" ); assert!( reply.raw.len() < 4_096, "one notification is a few hundred bytes, never a payload echo" ); } /// AT-3.18-01: after the opt-in, a served request delivers its own safe event only. #[tokio::test] async fn legacy_served_request_delivers_only_the_closed_safe_event() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; endpoint.set_level(&session, json!("debug")).await; let reply = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 61, "method": "tools/list", "params": {}}), ) .await; reply.assert_stream(); assert!( reply.result().get("tools").is_some(), "the business result is unchanged: {}", reply.response ); assert_eq!( reply.notifications, vec![expected_notification("info", "mcp.request.served", "ok")], "a served request reports its outcome and nothing about the request" ); } /// AT-3.18-01 (redaction): a value the request carries can never reach a notification, while the /// positive control proves the same search does find a reflected value elsewhere in the body. #[tokio::test] async fn a_request_value_can_never_reach_a_notification() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; endpoint.set_level(&session, json!("debug")).await; let reply = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({ "jsonrpc": "2.0", "id": 62, "method": "tools/call", "params": { "name": CANARY, "arguments": {"secret": CANARY}, "_meta": {"progressToken": CANARY} } }), ) .await; reply.assert_stream(); // Positive control: the existing product answer echoes the unknown tool name verbatim, so the // canary is provably present in this very body — a "not found" search here would be meaningless. let (code, message) = reply.error(); assert_eq!(code, -32602, "an unknown tool is a safe client error"); assert!( message.contains(CANARY), "the positive control must reflect the canary in the error message, got {message:?}" ); // Positive control on delivery: the event is really delivered and its body is exactly the // closed one for a rejected request, so the canary assertions below cannot pass vacuously. assert_eq!( reply.notifications, vec![expected_notification( "warning", "mcp.request.rejected", "invalid_params" )], "a rejected request must deliver exactly one safe warning event" ); // The delivered notification is exactly the closed safe body: no canary, no argument value. for notification in &reply.notifications { assert_eq!( notification["params"]["data"] .as_object() .map(|data| { let mut keys: Vec<&str> = data.keys().map(String::as_str).collect(); keys.sort_unstable(); keys }) .unwrap_or_default(), vec!["event", "outcome"], "a notification data object carries exactly the two closed members: {notification}" ); assert!( !notification.to_string().contains(CANARY), "a request value must never reach a notification: {notification}" ); assert!( notification["params"].get("id").is_none(), "a notification is not a response: {notification}" ); } } /// AT-3.18-02/03: the modern era opts in per request, and only from the pinned `_meta` key. #[tokio::test] async fn modern_level_is_per_request_and_requires_an_explicit_opt_in() { let endpoint = logging_endpoint().await; let opted_in = endpoint .modern_stream_with_params( "tools/list", json!({"_meta": { META_PROTOCOL_VERSION: MODERN_REVISION, META_CLIENT_CAPABILITIES: {}, META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"}, META_LOG_LEVEL: "debug" }}), ) .await; opted_in.assert_stream(); assert!(opted_in.result().get("tools").is_some()); assert_eq!( opted_in.notifications, vec![expected_notification("info", "mcp.request.served", "ok")], "a request that opted in receives its safe event" ); // The pinned modern schema: without the key the server MUST NOT send any notification. let not_opted_in = endpoint.modern_stream("tools/list", json!({})).await; assert_eq!(not_opted_in.status, reqwest::StatusCode::OK); assert!( not_opted_in.result().get("tools").is_some(), "the business answer is unchanged without an opt-in: {}", not_opted_in.response ); assert!( not_opted_in.notifications.is_empty(), "no notification without an explicit opt-in, got {:?}", not_opted_in.notifications ); // An unknown level is treated as "not opted in" rather than as an error of the business request, // and it never widens the accepted vocabulary. let unknown = endpoint .modern_stream_with_params( "tools/list", json!({"_meta": { META_PROTOCOL_VERSION: MODERN_REVISION, META_CLIENT_CAPABILITIES: {}, META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"}, META_LOG_LEVEL: "verbose" }}), ) .await; assert!(unknown.result().get("tools").is_some()); assert!( unknown.notifications.is_empty(), "an unpinned level must not opt in, got {:?}", unknown.notifications ); } /// AT-3.18-03: the delivered severities follow the ranged level of the legacy session. #[tokio::test] async fn legacy_severity_filter_is_honoured() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; // `error` is more severe than both a served request and a rejected one: neither is delivered. endpoint.set_level(&session, json!("error")).await; let served = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 71, "method": "tools/list", "params": {}}), ) .await; assert!( served.notifications.is_empty(), "a served event is below the `error` level, got {:?}", served.notifications ); let rejected = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 72, "method": "prompts/get", "params": {}}), ) .await; assert_eq!( rejected.error().0, -32602, "a malformed request is a client error" ); assert!( rejected.notifications.is_empty(), "a warning event is below the `error` level, got {:?}", rejected.notifications ); // `warning` admits the rejected event, and still not the served one. endpoint.set_level(&session, json!("warning")).await; let rejected = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 73, "method": "prompts/get", "params": {}}), ) .await; assert_eq!( rejected.notifications, vec![expected_notification( "warning", "mcp.request.rejected", "invalid_params" )], "the rejection is delivered at the `warning` level" ); let served = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 74, "method": "tools/list", "params": {}}), ) .await; assert!( served.notifications.is_empty(), "an info event is still below the `warning` level, got {:?}", served.notifications ); } /// AT-3.18-02: a malformed level is a revision-correct safe error with bounded work, and it never /// changes the level of the client or of anybody else. #[tokio::test] async fn a_malformed_level_is_a_safe_error_without_state_change() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; endpoint.set_level(&session, json!("error")).await; // A value outside the pinned union, a non-string shape, a missing member and an oversized value // all share one safe detail: the client cannot tell them apart and nothing is echoed back. for level in [ json!("verbose"), json!("INFO"), json!(7), json!(null), json!({}), json!("i".repeat(64)), ] { let reply = endpoint.set_level(&session, level.clone()).await; assert_eq!( reply.error(), (-32602, INVALID_LEVEL.to_owned()), "a malformed level is one safe invalid-params detail for {level}" ); if let Some(text) = level.as_str() { assert!( !reply.raw.contains(text), "the offending value must never be echoed back" ); } assert!( reply.notifications.is_empty(), "a rejected level is not an opt-in, got {:?}", reply.notifications ); } // An oversized value is refused by the fixed bound: the answer stays a short safe detail and the // value is not reflected, which is the observable half of "bounded work". let oversized = "i".repeat(65_536); let reply = endpoint.set_level(&session, json!(oversized)).await; assert_eq!(reply.error(), (-32602, INVALID_LEVEL.to_owned())); assert!( !reply.raw.contains(&oversized), "an oversized level must not be reflected" ); assert!( reply.raw.len() < 4_096, "the answer to an oversized level is bounded, got {} bytes", reply.raw.len() ); let missing = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 81, "method": "logging/setLevel", "params": {}}), ) .await; assert_eq!(missing.error(), (-32602, INVALID_LEVEL.to_owned())); // The level of the session is unchanged (`error` was set before the malformed attempts), so a // served request still delivers nothing... let served = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 82, "method": "tools/list", "params": {}}), ) .await; assert!( served.notifications.is_empty(), "a malformed level must not change the active level, got {:?}", served.notifications ); // ...and a valid level still works, which proves the assertions above are not vacuous. endpoint.set_level(&session, json!("debug")).await; let served = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 83, "method": "tools/list", "params": {}}), ) .await; assert_eq!(served.notifications.len(), 1); } /// AT-3.18-02 (era matrix): the modern schema declares no `logging/setLevel`, so the same wire name /// is a method-not-found there and never reaches the legacy service. #[tokio::test] async fn the_modern_era_has_no_set_level_method() { let endpoint = logging_endpoint().await; let reply = endpoint .modern_stream("logging/setLevel", json!({"level": "debug"})) .await; let (code, message) = reply.error(); assert_eq!( code, -32601, "the modern revision has no logging/setLevel: {}", reply.response ); assert!( !message.contains("debug"), "the rejection must not echo the level: {message:?}" ); // And the level was not applied through a wrong-era route: a modern request stays silent. let after = endpoint.modern_stream("tools/list", json!({})).await; assert!(after.notifications.is_empty()); } /// AT-3.18-01/03: a method this era does not implement is a pinned client error, so the safe event /// of an opted-in client describes a client-side rejection, never an internal server failure. #[tokio::test] async fn an_unknown_method_is_a_rejection_not_an_internal_failure() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; endpoint.set_level(&session, json!("info")).await; // `server/discover` is a modern-era method, so the legacy adapter answers it with -32601. let reply = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({ "jsonrpc": "2.0", "id": 71, "method": "server/discover", "params": {} }), ) .await; assert_eq!( reply.error().0, -32601, "an unimplemented method is a pinned client error: {}", reply.response ); assert_eq!( reply.notifications, vec![expected_notification( "warning", "mcp.request.rejected", "invalid_params" )], "a pinned client error must not be reported as an internal failure" ); } /// AT-3.18-01/03 (independence): a level belongs to one client context, so a second session and a /// second Agent keep their own (or no) level. #[tokio::test] async fn a_level_never_leaks_to_another_client() { let endpoint = logging_endpoint().await; let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; endpoint.set_level(&session, json!("debug")).await; // Another initialized session of the same Agent never opted in. let other_session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await; assert_ne!( session, other_session, "each initialize mints its own session" ); let other = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &other_session, json!({"jsonrpc": "2.0", "id": 91, "method": "tools/list", "params": {}}), ) .await; assert!( other.notifications.is_empty(), "a session that never opted in must stay silent, got {:?}", other.notifications ); assert!(other.result().get("tools").is_some()); // The opted-in session still receives its own events... let opted_in = endpoint .legacy_post( AGENT_SLUG, &endpoint.api_key, &session, json!({"jsonrpc": "2.0", "id": 92, "method": "tools/list", "params": {}}), ) .await; assert_eq!(opted_in.notifications.len(), 1); // ...and another Agent's session is unaffected by this Agent's level, both before and after it // sets its own. let other_agent_session = endpoint .legacy_session(OTHER_AGENT_SLUG, &endpoint.other_api_key) .await; let other_agent = endpoint .legacy_post( OTHER_AGENT_SLUG, &endpoint.other_api_key, &other_agent_session, json!({"jsonrpc": "2.0", "id": 93, "method": "tools/list", "params": {}}), ) .await; assert!( other_agent.notifications.is_empty(), "another Agent must not inherit a level, got {:?}", other_agent.notifications ); endpoint .legacy_post( OTHER_AGENT_SLUG, &endpoint.other_api_key, &other_agent_session, json!({"jsonrpc": "2.0", "id": 94, "method": "logging/setLevel", "params": {"level": "error"}}), ) .await; let other_agent_error = endpoint .legacy_post( OTHER_AGENT_SLUG, &endpoint.other_api_key, &other_agent_session, json!({"jsonrpc": "2.0", "id": 95, "method": "tools/list", "params": {}}), ) .await; assert!( other_agent_error.notifications.is_empty(), "the other Agent's own `error` level admits no served event" ); }