use futures::future::BoxFuture; use mcp_core::protocol::{JsonRpcMessage, JsonRpcRequest}; use std::collections::HashMap; use std::sync::Arc; use std::task::{Context, Poll}; use tokio::sync::{oneshot, RwLock}; use tower::{timeout::Timeout, Service, ServiceBuilder}; use crate::transport::{Error, TransportHandle}; /// A wrapper service that implements Tower's Service trait for MCP transport #[derive(Clone)] pub struct McpService { inner: Arc, pending_requests: Arc, } impl McpService { pub fn new(transport: T) -> Self { Self { inner: Arc::new(transport), pending_requests: Arc::new(PendingRequests::default()), } } pub async fn respond(&self, id: &str, response: Result) { self.pending_requests.respond(id, response).await } pub async fn hangup(&self, error: Error) { self.pending_requests.broadcast_close(error).await } } impl Service for McpService where T: TransportHandle + Send + Sync + 'static, { type Response = JsonRpcMessage; type Error = Error; type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { // Most transports are always ready, but this could be customized if needed Poll::Ready(Ok(())) } fn call(&mut self, request: JsonRpcMessage) -> Self::Future { let transport = self.inner.clone(); let pending_requests = self.pending_requests.clone(); Box::pin(async move { match request { JsonRpcMessage::Request(JsonRpcRequest { id: Some(id), .. }) => { // Create a channel to receive the response let (sender, receiver) = oneshot::channel(); pending_requests.insert(id.to_string(), sender).await; transport.send(request).await?; receiver.await.map_err(|_| Error::ChannelClosed)? } JsonRpcMessage::Request(_) => { // Handle notifications without waiting for a response transport.send(request).await?; Ok(JsonRpcMessage::Nil) } JsonRpcMessage::Notification(_) => { // Handle notifications without waiting for a response transport.send(request).await?; Ok(JsonRpcMessage::Nil) } _ => Err(Error::UnsupportedMessage), } }) } } // Add a convenience constructor for creating a service with timeout impl McpService where T: TransportHandle, { pub fn with_timeout(transport: T, timeout: std::time::Duration) -> Timeout> { ServiceBuilder::new() .timeout(timeout) .service(McpService::new(transport)) } } // A data structure to store pending requests and their response channels pub struct PendingRequests { requests: RwLock>>>, } impl Default for PendingRequests { fn default() -> Self { Self::new() } } impl PendingRequests { pub fn new() -> Self { Self { requests: RwLock::new(HashMap::new()), } } pub async fn insert(&self, id: String, sender: oneshot::Sender>) { self.requests.write().await.insert(id, sender); } pub async fn respond(&self, id: &str, response: Result) { if let Some(tx) = self.requests.write().await.remove(id) { let _ = tx.send(response); } } pub async fn broadcast_close(&self, error: Error) { for (_, tx) in self.requests.write().await.drain() { let err = match &error { Error::StdioProcessError(s) => Error::StdioProcessError(s.clone()), _ => Error::ChannelClosed, }; let _ = tx.send(Err(err)); } } pub async fn clear(&self) { self.requests.write().await.clear(); } pub async fn len(&self) -> usize { self.requests.read().await.len() } pub async fn is_empty(&self) -> bool { self.len().await == 0 } }