|
1 | 1 | #![cfg(not(feature = "local"))] |
2 | | -use rmcp::transport::streamable_http_server::{ |
3 | | - StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager, |
| 2 | +use std::time::Duration; |
| 3 | + |
| 4 | +use futures::future::BoxFuture; |
| 5 | +use rmcp::{ |
| 6 | + ServerHandler, ServiceExt, |
| 7 | + handler::server::{ |
| 8 | + router::tool::ToolRoute, |
| 9 | + tool::{ToolCallContext, ToolRouter, schema_for_type}, |
| 10 | + }, |
| 11 | + model::{ |
| 12 | + CallToolRequestParams, CallToolResult, Content, ProgressNotificationParam, |
| 13 | + ServerCapabilities, ServerInfo, Tool, |
| 14 | + }, |
| 15 | + tool_handler, |
| 16 | + transport::{ |
| 17 | + StreamableHttpClientTransport, |
| 18 | + streamable_http_client::StreamableHttpClientTransportConfig, |
| 19 | + streamable_http_server::{ |
| 20 | + StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager, |
| 21 | + }, |
| 22 | + }, |
4 | 23 | }; |
5 | 24 | use tokio_util::sync::CancellationToken; |
6 | 25 |
|
@@ -76,6 +95,114 @@ async fn stateless_json_response_returns_application_json() -> anyhow::Result<() |
76 | 95 | Ok(()) |
77 | 96 | } |
78 | 97 |
|
| 98 | +#[derive(Debug, Default, serde::Deserialize, schemars::JsonSchema)] |
| 99 | +struct EmptyArgs {} |
| 100 | + |
| 101 | +#[derive(Debug, Clone)] |
| 102 | +struct ProgressToolServer { |
| 103 | + tool_router: ToolRouter<Self>, |
| 104 | +} |
| 105 | + |
| 106 | +impl ProgressToolServer { |
| 107 | + fn new() -> Self { |
| 108 | + Self { |
| 109 | + tool_router: ToolRouter::new().with_route(ToolRoute::new_dyn( |
| 110 | + Tool::new( |
| 111 | + "progress_then_result", |
| 112 | + "Emit a progress notification before returning", |
| 113 | + schema_for_type::<EmptyArgs>(), |
| 114 | + ), |
| 115 | + |context: ToolCallContext<'_, Self>| -> BoxFuture<'_, _> { |
| 116 | + Box::pin(async move { |
| 117 | + let Some(progress_token) = |
| 118 | + context.request_context.meta.get_progress_token() |
| 119 | + else { |
| 120 | + return Err(rmcp::ErrorData::invalid_params( |
| 121 | + "missing progress token", |
| 122 | + None, |
| 123 | + )); |
| 124 | + }; |
| 125 | + |
| 126 | + context |
| 127 | + .request_context |
| 128 | + .peer |
| 129 | + .notify_progress(ProgressNotificationParam::new(progress_token, 1.0)) |
| 130 | + .await |
| 131 | + .map_err(|err| { |
| 132 | + rmcp::ErrorData::internal_error( |
| 133 | + format!("failed to send progress notification: {err}"), |
| 134 | + None, |
| 135 | + ) |
| 136 | + })?; |
| 137 | + |
| 138 | + Ok(CallToolResult::success(vec![Content::text("done")])) |
| 139 | + }) |
| 140 | + }, |
| 141 | + )), |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[tool_handler] |
| 147 | +impl ServerHandler for ProgressToolServer { |
| 148 | + fn get_info(&self) -> ServerInfo { |
| 149 | + ServerInfo::new(ServerCapabilities::builder().enable_tools().build()) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#[tokio::test] |
| 154 | +async fn stateless_json_response_waits_for_terminal_tool_response() -> anyhow::Result<()> { |
| 155 | + let ct = CancellationToken::new(); |
| 156 | + let service: StreamableHttpService<ProgressToolServer, LocalSessionManager> = |
| 157 | + StreamableHttpService::new( |
| 158 | + || Ok(ProgressToolServer::new()), |
| 159 | + Default::default(), |
| 160 | + StreamableHttpServerConfig { |
| 161 | + stateful_mode: false, |
| 162 | + json_response: true, |
| 163 | + sse_keep_alive: None, |
| 164 | + cancellation_token: ct.child_token(), |
| 165 | + ..Default::default() |
| 166 | + }, |
| 167 | + ); |
| 168 | + |
| 169 | + let router = axum::Router::new().nest_service("/mcp", service); |
| 170 | + let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?; |
| 171 | + let addr = tcp_listener.local_addr()?; |
| 172 | + |
| 173 | + let handle = tokio::spawn({ |
| 174 | + let ct = ct.clone(); |
| 175 | + async move { |
| 176 | + let _ = axum::serve(tcp_listener, router) |
| 177 | + .with_graceful_shutdown(async move { ct.cancelled_owned().await }) |
| 178 | + .await; |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + let transport = StreamableHttpClientTransport::from_config( |
| 183 | + StreamableHttpClientTransportConfig::with_uri(format!("http://{addr}/mcp")), |
| 184 | + ); |
| 185 | + let client = ().serve(transport).await?; |
| 186 | + |
| 187 | + let result = tokio::time::timeout( |
| 188 | + Duration::from_secs(3), |
| 189 | + client.call_tool(CallToolRequestParams::new("progress_then_result")), |
| 190 | + ) |
| 191 | + .await??; |
| 192 | + |
| 193 | + let text = result |
| 194 | + .content |
| 195 | + .first() |
| 196 | + .and_then(|content| content.raw.as_text()) |
| 197 | + .map(|text| text.text.as_str()); |
| 198 | + assert_eq!(text, Some("done")); |
| 199 | + |
| 200 | + let _ = client.cancel().await; |
| 201 | + ct.cancel(); |
| 202 | + handle.await?; |
| 203 | + Ok(()) |
| 204 | +} |
| 205 | + |
79 | 206 | #[tokio::test] |
80 | 207 | async fn stateless_sse_mode_default_unchanged() -> anyhow::Result<()> { |
81 | 208 | let ct = CancellationToken::new(); |
|
0 commit comments