//! A chat's access to an initialized MCP client and its tool catalog. //! A pooled connection honors this chat's lease and routes input requests back to it. use std::future::Future; use std::sync::Arc; use std::time::Duration; use crate::client_tool_catalog::ClientToolCatalog; use crate::connection_pool::ConnectionSharing; use crate::tools::ToolInfo; use anyhow::Result; use anyhow::anyhow; use akode_protocol::mcp::McpServerInfo; use akode_rmcp_client::RmcpClient; use akode_rmcp_client::SendElicitation; use rmcp::model::ListResourceTemplatesResult; use rmcp::model::ListResourcesResult; use rmcp::model::PaginatedRequestParams; use rmcp::model::ServerCapabilities; use serde_json::Value; use tokio_util::task::AbortOnDropHandle; #[derive(Clone)] pub(crate) struct ManagedClient { pub(crate) connection_sharing: ConnectionSharing, /// The calling chat's input handler, separate from the shared transport's startup handler. pub(crate) shared_elicitation: Option>, pub(crate) _auth_change_notifications: Option>>, pub(crate) client: Arc, pub(crate) server_info: McpServerInfo, pub(crate) tool_catalog: Arc, pub(crate) tool_timeout: Option, pub(crate) server_instructions: Option, pub(crate) server_supports_sandbox_state_meta_capability: bool, pub(crate) client_supports_network_policy_meta_capability: bool, } impl ManagedClient { pub(crate) async fn with_active_shared_lease( &self, operation: impl Future, ) -> Result { match &self.connection_sharing { ConnectionSharing::Shared(lease) => tokio::select! { biased; () = lease.cancellation().cancelled() => { Err(anyhow!("MCP connection lease has been released")) } result = operation => Ok(result), }, ConnectionSharing::Private | ConnectionSharing::Negotiated | ConnectionSharing::Owned(_) => Ok(operation.await), } } pub(crate) async fn with_lease_request( &self, operation: impl Future>, ) -> Result { // Keep one copy of the request future as it passes through both lease scopes. let operation = std::pin::pin!(operation); self.with_active_shared_lease(async { match self.connection_sharing.server_lease() { Some(lease) => lease.requests.scope(operation).await, None => operation.await, } }) .await? } pub(crate) async fn call_tool( &self, name: String, arguments: Option, meta_for_session: impl Fn(&ServerCapabilities) -> Result> + Send + Sync, timeout: Option, ) -> Result { let meta_for_session = |caps: &ServerCapabilities| { let mut meta = meta_for_session(caps)?; if let Some(lease) = self.connection_sharing.server_lease() { meta.get_or_insert_with(|| serde_json::json!({})) .as_object_mut() .ok_or_else(|| anyhow!("MCP tool metadata must be an object"))? .insert( crate::connection_pool::LEASE_META_KEY.to_owned(), serde_json::json!(lease.id), ); } Ok(meta) }; self.with_lease_request(async { if let Some(send_elicitation) = &self.shared_elicitation { self.client .call_tool_with_elicitation( name, arguments, meta_for_session, timeout, Arc::clone(send_elicitation), ) .await } else { self.client .call_tool(name, arguments, meta_for_session, timeout) .await } }) .await } /// Lists resources within this chat's lease lifetime. pub(crate) async fn list_resources( &self, params: Option, timeout: Option, ) -> Result { self.with_active_shared_lease(self.client.list_resources(params, timeout)) .await? } /// Lists resource templates within this chat's lease lifetime. pub(crate) async fn list_resource_templates( &self, params: Option, timeout: Option, ) -> Result { self.with_active_shared_lease(self.client.list_resource_templates(params, timeout)) .await? } /// Reads a resource using this chat's input authority and lease lifetime. pub(crate) async fn read_resource( &self, mut params: rmcp::model::ReadResourceRequestParams, timeout: Option, ) -> Result { if let Some(lease) = self.connection_sharing.server_lease() { params.meta.get_or_insert_default().insert( crate::connection_pool::LEASE_META_KEY.to_owned(), serde_json::json!(lease.id), ); } self.with_lease_request(async { if let Some(send_elicitation) = &self.shared_elicitation { self.client .read_resource_with_elicitation(params, timeout, Arc::clone(send_elicitation)) .await } else { self.client.read_resource(params, timeout).await } }) .await } pub(crate) async fn listed_catalog(&self) -> (Vec, Option>) { self.tool_catalog .read(|catalog| (catalog.tools.to_vec(), catalog.meta)) .await } }