diff --git a/codex-rs/codex-mcp/src/connection_manager.rs b/codex-rs/codex-mcp/src/connection_manager.rs index 3f832a173c..fd12768715 100644 --- a/codex-rs/codex-mcp/src/connection_manager.rs +++ b/codex-rs/codex-mcp/src/connection_manager.rs @@ -493,7 +493,6 @@ impl McpConnectionSet { catalog_item_limit, ); let defer_startup = allow_deferred_startup - && !configured_config.required && !tool_plugin_provenance.is_selected_plugin_mcp_server(&server_name) && async_managed_client .tool_catalog_cache_context diff --git a/codex-rs/codex-mcp/src/connection_manager/required.rs b/codex-rs/codex-mcp/src/connection_manager/required.rs index 4ba65c53ba..2ba8cb371d 100644 --- a/codex-rs/codex-mcp/src/connection_manager/required.rs +++ b/codex-rs/codex-mcp/src/connection_manager/required.rs @@ -23,6 +23,10 @@ impl McpConnectionSet { }); continue; }; + if view.connection.startup_is_dormant() && view.connection.client.has_cached_tools() + { + continue; + } match view.connection.client().await { Ok(_) => {} diff --git a/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs b/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs index d9ac4e46d2..9e41137701 100644 --- a/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs +++ b/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs @@ -186,7 +186,8 @@ impl McpConnectionSet { { let required = self.required_servers.binary_search(server_name).is_ok(); let has_cached_tools = view.connection.client.has_cached_tools(); - let must_wait_for_startup = required + let must_wait_for_startup = (required + && (!view.connection.startup_is_dormant() || !has_cached_tools)) || self.is_selected_plugin_mcp_server(server_name) || required_servers .iter() diff --git a/codex-rs/codex-mcp/src/runtime.rs b/codex-rs/codex-mcp/src/runtime.rs index dec1858b8a..816dd2be85 100644 --- a/codex-rs/codex-mcp/src/runtime.rs +++ b/codex-rs/codex-mcp/src/runtime.rs @@ -48,12 +48,12 @@ use crate::server::EffectiveMcpServer; use crate::tool_catalog_cache::McpToolCatalogCache; use crate::tools::ToolInfo; -/// Controls when one task starts its optional MCP servers. +/// Controls when one task starts its eligible MCP servers. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum McpStartupPolicy { /// Start configured servers when their task's MCP runtime is published. Eager, - /// Start optional servers with cached tool definitions on first use. + /// Start servers with cached tool definitions on first use. LazyWhenCached, } diff --git a/codex-rs/core/tests/suite/mcp_tool_cache.rs b/codex-rs/core/tests/suite/mcp_tool_cache.rs index cf7fa6b28a..d1de57d2a9 100644 --- a/codex-rs/core/tests/suite/mcp_tool_cache.rs +++ b/codex-rs/core/tests/suite/mcp_tool_cache.rs @@ -30,6 +30,7 @@ use core_test_support::wait_for_mcp_server; use pretty_assertions::assert_eq; use serde_json::Value; use serde_json::json; +use test_case::test_case; use super::rmcp_client::remote_aware_environment_id; use super::rmcp_client::remote_aware_stdio_server_bin; @@ -247,7 +248,9 @@ async fn mcp_calls_stay_bound_to_each_thread() -> anyhow::Result<()> { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn cached_http_mcp_starts_lazily_for_subagents() -> anyhow::Result<()> { +#[test_case(false; "optional server")] +#[test_case(true; "required server")] +async fn cached_http_mcp_starts_lazily_for_subagents(required: bool) -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let responses_server = responses::start_mock_server().await; @@ -269,6 +272,7 @@ async fn cached_http_mcp_starts_lazily_for_subagents() -> anyhow::Result<()> { "url": server_url, "http_headers": { "Authorization": "Bearer cached-http-test-token" }, "enabled_tools": ["calendar_create_event"], + "required": required, "startup_timeout_sec": 10, })) .expect("HTTP MCP server configuration"),