//! The production adapters consume actual trait results and own their pending work. use super::*; #[tokio::test] async fn rmcp_negotiation_handler_qa_discover_uses_requested_revision_capabilities() { use crate::protocol::{ CAPABILITY_DESCRIPTORS, CapabilityId, CapabilityRegistry, METHOD_DESCRIPTORS, }; use std::sync::Arc; let mut outcomes = Vec::new(); for revisions in [&["2026-07-28"][..], &["2025-11-25"][..]] { let mut descriptors = CAPABILITY_DESCRIPTORS.to_vec(); descriptors .iter_mut() .find(|d| d.id == CapabilityId::Tools) .unwrap() .revisions = revisions; let registration = Arc::new(CapabilityRegistry::new(&descriptors, METHOD_DESCRIPTORS).unwrap()); crate::app::dispatch_observer::observe_with_registration(registration, async { let expected = advertised_server_capabilities("2026-07-28"); let actual = discover("2026-07-28").await.unwrap(); outcomes.push(( expected["tools"].clone(), actual["capabilities"]["tools"].clone(), )); }) .await; } assert_eq!( outcomes, vec![ (json!({"listChanged":true}), json!({"listChanged":true})), (Value::Null, Value::Null) ], "modern-only tools must be advertised; legacy-only tools must not leak into modern discovery" ); } use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst}; #[derive(Default)] struct Control { calls: AtomicUsize, dropped: AtomicBool, finished: AtomicBool, fail: bool, pending: bool, } struct Witness<'a>(&'a AtomicBool); impl Drop for Witness<'_> { fn drop(&mut self) { self.0.store(true, SeqCst); } } fn error() -> ErrorData { ErrorData::new( ErrorCode(-32042), "typed negotiation failure", Some(json!({"proof":42})), ) } impl Control { async fn enter(&self, context: &RequestContext) -> Result<(), ErrorData> { assert!( context.peer.is_transport_closed(), "bootstrap closes before handler" ); self.calls.fetch_add(1, SeqCst); let _witness = Witness(&self.dropped); if self.pending { std::future::pending::<()>().await; } self.finished.store(true, SeqCst); if self.fail { Err(error()) } else { Ok(()) } } } impl ServerHandler for Control { async fn initialize( &self, request: InitializeRequestParams, context: RequestContext, ) -> Result { self.enter(&context).await?; let mut result = ServerHandler::initialize(&NegotiationHandler, request, context).await?; result.server_info = Implementation::new("typed-initialize", "42"); result.capabilities.tools = None; Ok(result) } async fn discover( &self, context: RequestContext, ) -> Result { self.enter(&context).await?; let mut result = ServerHandler::discover(&NegotiationHandler, context).await?; result = result.with_server_info(Implementation::new("typed-discover", "42")); result.capabilities.tools = None; Ok(result) } async fn ping(&self, context: RequestContext) -> Result<(), ErrorData> { self.enter(&context).await } } #[tokio::test] async fn rmcp_negotiation_handler_mutated_typed_success_is_consumed() { let h = Control::default(); let init = initialize_with(&h, "2025-11-25").await.unwrap(); assert_eq!(init["serverInfo"]["name"], "typed-initialize"); assert!(init["capabilities"].get("tools").is_none()); let discover = discover_with(&h, "2026-07-28").await.unwrap(); assert_eq!( discover["_meta"]["io.modelcontextprotocol/serverInfo"]["name"], "typed-discover" ); assert!(discover["capabilities"].get("tools").is_none()); ping_with(&h).await.unwrap(); assert_eq!(h.calls.load(SeqCst), 3, "no bootstrap invocation"); } #[tokio::test] async fn rmcp_negotiation_handler_typed_errors_are_not_success() { let h = Control { fail: true, ..Default::default() }; for actual in [ initialize_with(&h, "2025-11-25").await.unwrap_err(), discover_with(&h, "2026-07-28").await.unwrap_err(), ping_with(&h).await.unwrap_err(), ] { assert_eq!(actual, error()); for mode in [ crate::transport::ResponseMode::Json, crate::transport::ResponseMode::Sse, ] { let response = super::super::dispatch::sdk_error_response( &json!({"id":"typed-error"}), mode, "2025-11-25", actual.clone(), ); assert_eq!(response.status(), 200); assert!(response.headers().get("mcp-session-id").is_none()); let bytes = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap(); let raw = std::str::from_utf8(&bytes).unwrap(); let body: Value = match mode { crate::transport::ResponseMode::Json => serde_json::from_str(raw).unwrap(), crate::transport::ResponseMode::Sse => serde_json::from_str( raw.lines() .find_map(|l| l.strip_prefix("data:")) .unwrap() .trim(), ) .unwrap(), }; assert_eq!( body, json!({"jsonrpc":"2.0","id":"typed-error","error":{"code":-32042,"message":"typed negotiation failure","data":{"proof":42}}}) ); } } assert_eq!(h.calls.load(SeqCst), 3); assert!(initialize("not-supported").await.is_err()); assert_eq!( NegotiationHandler .supported_protocol_versions() .iter() .map(|v| v.as_str()) .collect::>(), SUPPORTED_PROTOCOL_VERSIONS ); } #[tokio::test] async fn rmcp_negotiation_handler_drop_cancels_request_owned_work() { for method in 0..3 { let h = Control { pending: true, ..Default::default() }; let mut future = Box::pin(async { match method { 0 => { initialize_with(&h, "2025-11-25").await?; } 1 => { discover_with(&h, "2026-07-28").await?; } _ => { ping_with(&h).await?; } } Ok::<_, ErrorData>(()) }); tokio::select! { result = &mut future => panic!("pending handler completed: {result:?}"), _ = async { while h.calls.load(SeqCst) == 0 { tokio::task::yield_now().await; } } => {} } assert!(!h.dropped.load(SeqCst)); drop(future); assert!(h.dropped.load(SeqCst)); tokio::task::yield_now().await; assert!(!h.finished.load(SeqCst)); assert_eq!(h.calls.load(SeqCst), 1); } }