diff --git a/codex-rs/codex-mcp/src/connection_manager.rs b/codex-rs/codex-mcp/src/connection_manager.rs index 858fc884da..e480251ea8 100644 --- a/codex-rs/codex-mcp/src/connection_manager.rs +++ b/codex-rs/codex-mcp/src/connection_manager.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use std::sync::Mutex; use std::sync::PoisonError; use std::sync::RwLock; +use std::sync::Weak; use std::sync::atomic::Ordering; use std::time::Duration; use std::time::Instant; @@ -116,6 +117,7 @@ pub struct McpConnectionManager { } struct McpConnections { + resource_cache_identity: Arc<()>, clients: HashMap, server_metadata: HashMap, required_servers: Vec, @@ -297,6 +299,7 @@ impl McpConnectionManager { ) -> Self { Self { connections: RwLock::new(Arc::new(McpConnections { + resource_cache_identity: Arc::new(()), clients: HashMap::new(), server_metadata: HashMap::new(), required_servers: Vec::new(), @@ -321,6 +324,10 @@ impl McpConnectionManager { self.connections().clients.contains_key(server_name) } + pub(crate) fn resource_cache_key(&self) -> Weak<()> { + Arc::downgrade(&self.connections().resource_cache_identity) + } + /// Cancels MCP servers that are still starting without shutting down ready clients. pub fn cancel_startup(&self) { self.startup_cancellation_token @@ -975,6 +982,7 @@ async fn start_connections( }); } let connections = McpConnections { + resource_cache_identity: Arc::new(()), clients, server_metadata, required_servers, diff --git a/codex-rs/codex-mcp/src/resource_client.rs b/codex-rs/codex-mcp/src/resource_client.rs index 4406b81a53..c4ef2b1d84 100644 --- a/codex-rs/codex-mcp/src/resource_client.rs +++ b/codex-rs/codex-mcp/src/resource_client.rs @@ -36,9 +36,9 @@ pub struct McpResourceClient { manager: Arc>, } -/// Opaque identity for the manager currently used by an MCP resource client. +/// Opaque identity for the connection snapshot currently used by an MCP resource client. #[derive(Clone)] -pub struct McpResourceClientCacheKey(Weak); +pub struct McpResourceClientCacheKey(Weak<()>); impl PartialEq for McpResourceClientCacheKey { fn eq(&self, other: &Self) -> bool { @@ -62,9 +62,9 @@ impl McpResourceClient { Self { manager } } - /// Returns an identity that changes whenever the published manager changes. + /// Returns an identity that changes whenever the manager's connections change. pub fn cache_key(&self) -> McpResourceClientCacheKey { - McpResourceClientCacheKey(Arc::downgrade(&self.manager.load_full())) + McpResourceClientCacheKey(self.manager.load_full().resource_cache_key()) } /// Returns whether the current manager contains the named server.