|
| 1 | +#![cfg(not(feature = "local"))] |
| 2 | +#![cfg(feature = "client")] |
| 3 | + |
| 4 | +use rmcp::{ |
| 5 | + ClientHandler, ServerHandler, ServiceExt, |
| 6 | + handler::server::router::{prompt::PromptRouter, tool::ToolRouter}, |
| 7 | + model::{CacheScope, ClientInfo, ListPromptsResult, ListToolsResult, ProtocolVersion}, |
| 8 | + prompt_handler, tool_handler, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +struct CacheHintServer { |
| 13 | + tool_router: ToolRouter<Self>, |
| 14 | + prompt_router: PromptRouter<Self>, |
| 15 | +} |
| 16 | + |
| 17 | +impl CacheHintServer { |
| 18 | + fn new() -> Self { |
| 19 | + Self { |
| 20 | + tool_router: ToolRouter::new(), |
| 21 | + prompt_router: PromptRouter::new(), |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[tool_handler(router = self.tool_router)] |
| 27 | +#[prompt_handler(router = self.prompt_router)] |
| 28 | +impl ServerHandler for CacheHintServer {} |
| 29 | + |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +struct VersionedClient { |
| 32 | + protocol_version: ProtocolVersion, |
| 33 | +} |
| 34 | + |
| 35 | +impl ClientHandler for VersionedClient { |
| 36 | + fn get_info(&self) -> ClientInfo { |
| 37 | + let mut info = ClientInfo::default(); |
| 38 | + info.protocol_version = self.protocol_version.clone(); |
| 39 | + info |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async fn list_results(protocol_version: ProtocolVersion) -> (ListToolsResult, ListPromptsResult) { |
| 44 | + let (server_transport, client_transport) = tokio::io::duplex(4096); |
| 45 | + |
| 46 | + let server_handle = tokio::spawn(async move { |
| 47 | + CacheHintServer::new() |
| 48 | + .serve(server_transport) |
| 49 | + .await? |
| 50 | + .waiting() |
| 51 | + .await?; |
| 52 | + anyhow::Ok(()) |
| 53 | + }); |
| 54 | + |
| 55 | + let client = VersionedClient { protocol_version } |
| 56 | + .serve(client_transport) |
| 57 | + .await |
| 58 | + .expect("client should connect"); |
| 59 | + let tools = client |
| 60 | + .list_tools(None) |
| 61 | + .await |
| 62 | + .expect("tools/list should succeed"); |
| 63 | + let prompts = client |
| 64 | + .list_prompts(None) |
| 65 | + .await |
| 66 | + .expect("prompts/list should succeed"); |
| 67 | + |
| 68 | + client.cancel().await.expect("client should cancel"); |
| 69 | + server_handle.await.expect("server task").expect("server"); |
| 70 | + (tools, prompts) |
| 71 | +} |
| 72 | + |
| 73 | +#[tokio::test] |
| 74 | +async fn handler_macros_should_emit_required_cache_hints_for_2026_07_28() { |
| 75 | + let (tools, prompts) = list_results(ProtocolVersion::V_2026_07_28).await; |
| 76 | + |
| 77 | + assert_eq!( |
| 78 | + ( |
| 79 | + tools.ttl_ms, |
| 80 | + tools.cache_scope, |
| 81 | + prompts.ttl_ms, |
| 82 | + prompts.cache_scope, |
| 83 | + ), |
| 84 | + ( |
| 85 | + Some(0), |
| 86 | + Some(CacheScope::Public), |
| 87 | + Some(0), |
| 88 | + Some(CacheScope::Public), |
| 89 | + ) |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +#[tokio::test] |
| 94 | +async fn handler_macros_should_omit_cache_hints_for_legacy_versions() { |
| 95 | + let (tools, prompts) = list_results(ProtocolVersion::V_2025_11_25).await; |
| 96 | + |
| 97 | + assert_eq!( |
| 98 | + ( |
| 99 | + tools.ttl_ms, |
| 100 | + tools.cache_scope, |
| 101 | + prompts.ttl_ms, |
| 102 | + prompts.cache_scope, |
| 103 | + ), |
| 104 | + (None, None, None, None) |
| 105 | + ); |
| 106 | +} |
0 commit comments