//! #107: HTTP wire controls are independent of the SDK execution oracle. //! A real committed Tool removal exercises the watcher; existing mcp_signals::scenarios covers //! Resource/Prompt publications, isolation, coalescing, reconnect and authoritative recovery. use super::{CounterSet, support::*}; use serde_json::{Value, json}; use std::{sync::Arc, time::Duration}; #[path = "rmcp_signals_test_support.rs"] mod fixtures; use fixtures::{Endpoint, terminal}; const SUB_ID: &str = "io.modelcontextprotocol/subscriptionId"; const ACK: &str = "notifications/subscriptions/acknowledged"; const TOOLS: &str = "notifications/tools/list_changed"; const INVALID_FILTER: &str = "invalid subscriptions/listen notification filter"; fn counts(c: &CounterSet) -> [usize; 3] { [ c.sdk_subscriptions_listen(), c.sdk_subscriptions_acknowledged(), c.sdk_signal_notification(), ] } fn ack(id: &Value, notifications: Value) -> Value { json!({"jsonrpc":"2.0", "method":ACK, "params":{"_meta":{SUB_ID:id},"notifications":notifications}}) } fn listen_body(id: Value, params: Value) -> Value { json!({"jsonrpc":"2.0","id":id,"method":"subscriptions/listen","params":params}) } async fn catalog_control(revision: &str) -> [usize; 3] { let h = Endpoint::started().await; let session = if revision == LEGACY_REVISION { Some(h.session().await) } else { None }; let admission_before = h.counters.snapshot().admission; let before_list = h.tools(revision, session.as_deref()).await; assert_eq!( h.counters.snapshot().admission - admission_before, 1, "positive control: the HTTP request must carry this observer" ); assert_eq!(before_list["result"]["tools"].as_array().unwrap().len(), 1); assert_eq!(before_list["result"]["tools"][0]["name"], TOOL_NAME); let id = json!(u64::MAX); let before = counts(&h.counters); let mut stream = if revision == MODERN_REVISION { let mut stream = h .listen( id.clone(), json!({ "toolsListChanged":true,"resourcesListChanged":true,"promptsListChanged":true, "resourceSubscriptions":["crank://CANARY-URI"], "unknown":null }), ) .await; // Whole-frame equality proves ack first, exact id, no result/id/URI/payload leakage. assert_eq!( stream.frame().await, ack( &id, json!({ "toolsListChanged":true,"resourcesListChanged":true,"promptsListChanged":true }) ) ); stream } else { h.legacy_stream(session.as_deref().unwrap()).await }; // Enough for the existing one-second watcher to establish its initial committed fingerprint. stream.silent().await; h.draft_without_tools().await; stream.silent().await; assert_eq!( h.tools(revision, session.as_deref()).await["result"]["tools"] .as_array() .unwrap() .len(), 1 ); h.publish_without_tools().await; let expected = if revision == MODERN_REVISION { json!({"jsonrpc":"2.0","method":TOOLS,"params":{"_meta":{SUB_ID:id}}}) } else { json!({"jsonrpc":"2.0","method":TOOLS}) }; assert_eq!( stream.frame().await, expected, "exact committed signal, no catalog payload" ); stream.silent().await; assert_eq!( h.tools(revision, session.as_deref()).await["result"]["tools"], json!([]) ); let after = counts(&h.counters); std::array::from_fn(|i| after[i] - before[i]) } #[tokio::test] async fn modern_wire_control() { catalog_control(MODERN_REVISION).await; } #[tokio::test] async fn legacy_wire_control() { catalog_control(LEGACY_REVISION).await; } #[tokio::test] async fn modern_sdk_runtime_oracle() { // All real HTTP/commit assertions above must pass before missing SDK behavior can be RED. assert_eq!( catalog_control(MODERN_REVISION).await, [1, 1, 1], "real HTTP listen decode / ack serialization / watcher signal serialization SDK entries" ); } #[tokio::test] async fn legacy_sdk_runtime_oracle() { assert_eq!( catalog_control(LEGACY_REVISION).await, [0, 0, 1], "legacy GET must use SDK signal serialization but not modern listen/ack" ); } #[tokio::test] async fn modern_false_absent_and_uri_only_do_not_opt_in() { let h = Endpoint::started().await; let mut absent = h.listen(json!("absent"), json!({})).await; assert_eq!(absent.frame().await, ack(&json!("absent"), json!({}))); let mut disabled = h .listen( json!("disabled"), json!({ "toolsListChanged":false,"resourcesListChanged":false,"promptsListChanged":false, "resourceSubscriptions":["crank://CANARY-URI"] }), ) .await; assert_eq!(disabled.frame().await, ack(&json!("disabled"), json!({}))); // A simultaneous positive stream proves the publication was visible to the running watchers. let mut enabled = h .listen(json!("enabled"), json!({"toolsListChanged":true})) .await; assert_eq!( enabled.frame().await, ack(&json!("enabled"), json!({"toolsListChanged":true})) ); enabled.silent().await; h.draft_without_tools().await; h.publish_without_tools().await; assert_eq!( enabled.frame().await, json!({"jsonrpc":"2.0","method":TOOLS,"params":{"_meta":{SUB_ID:"enabled"}}}) ); absent.silent().await; disabled.silent().await; let body = terminal( h.post( MODERN_REVISION, None, json!({ "jsonrpc":"2.0","id":4,"method":"resources/subscribe", "params":{"_meta":modern_meta(),"uri":"crank://CANARY-URI"} }), ) .await, ) .await; assert_eq!(body["error"]["code"], -32601); assert!(body.get("result").is_none()); assert!(!body.to_string().contains("CANARY")); } #[tokio::test] async fn modern_http_filter_shape_and_bounds_stay_compatible() { let h = Endpoint::started().await; let mut invalid = vec![ json!({}), json!({"notifications":null}), json!({"notifications":[]}), json!({"notifications":{"toolsListChanged":"CANARY"}}), json!({"notifications":{"resourceSubscriptions":[null]}}), json!({"notifications":{"resourceSubscriptions":vec!["CANARY";33]}}), json!({"notifications":{"resourceSubscriptions":["X".repeat(2049)]}}), ]; for key in [ "toolsListChanged", "resourcesListChanged", "promptsListChanged", "resourceSubscriptions", ] { let mut filter = json!({}); filter[key] = Value::Null; invalid.push(json!({"notifications":filter})); } for mut params in invalid { params["_meta"] = modern_meta(); let body = terminal( h.post(MODERN_REVISION, None, listen_body(json!(7), params)) .await, ) .await; assert_eq!(body["id"], 7); assert_eq!(body["error"]["code"], -32602); assert_eq!(body["error"]["message"], INVALID_FILTER); assert!(body.get("result").is_none()); assert!(!body.to_string().contains("CANARY")); assert!(body.to_string().len() < 4096); } // Inclusive bounds, unknown null extension and explicit false are accepted, never acknowledged // as a URI-content subscription or an extra list-change type. let mut stream = h .listen( json!(8), json!({ "resourceSubscriptions":vec!["é".repeat(1024);32], "toolsListChanged":false, "unknown":null }), ) .await; assert_eq!(stream.frame().await, ack(&json!(8), json!({}))); } #[tokio::test] async fn modern_http_ids_are_lossless_and_bounded() { let h = Endpoint::started().await; for id in [ json!("a".repeat(62)), json!("é".repeat(31)), json!(u64::MAX), json!(9_007_199_254_740_993_u64), json!(1.25), json!(1.0), ] { let mut stream = h.listen(id.clone(), json!({})).await; let frame = stream.frame().await; assert_eq!(frame, ack(&id, json!({}))); assert_eq!(frame["params"]["_meta"][SUB_ID].to_string(), id.to_string()); } for id in [json!("a".repeat(63)), json!("é".repeat(32))] { let body = terminal( h.post( MODERN_REVISION, None, listen_body( id.clone(), json!({"_meta":modern_meta(),"notifications":{}}), ), ) .await, ) .await; assert_eq!(body["id"], id); assert_eq!(body["error"]["code"], -32600); assert!(body.get("result").is_none()); } } #[tokio::test] async fn modern_http_meta_admission_precedes_sdk_decode() { let h = Endpoint::started().await; for meta in [ Value::Null, json!([]), json!({META_PROTOCOL_VERSION:MODERN_REVISION, META_CLIENT_CAPABILITIES:null}), ] { let before = counts(&h.counters); let response = h .post( MODERN_REVISION, None, listen_body(json!(9), json!({"_meta":meta,"notifications":{}})), ) .await; // Admission rejections are HTTP 400, not a successful SSE response. assert_eq!(response.status(), 400); let raw = tokio::time::timeout(Duration::from_secs(6), response.text()) .await .unwrap() .unwrap(); let body: Value = serde_json::from_str(&raw).unwrap(); assert_eq!(body["error"]["code"], -32020); assert!(body.get("result").is_none()); assert_eq!( counts(&h.counters), before, "admission must reject before any SDK entry" ); } // clientInfo is optional at HTTP admission; SDK decoding must not newly require it. let mut meta = modern_meta(); meta.as_object_mut().unwrap().remove(META_CLIENT_INFO); let response = h .post( MODERN_REVISION, None, listen_body( json!(10), json!({"_meta":meta,"notifications":{},"extension":null}), ), ) .await; let mut stream = fixtures::Events::open(response); assert_eq!(stream.frame().await, ack(&json!(10), json!({}))); }