Allow cached catalogs to satisfy MCP startup readiness (#47935)

## Why

Required MCP servers can block startup while establishing a live connection even when a usable tool catalog is already cached.

## What changed

- Add per-server `startup_readiness = "catalog"` to expose cached tools while the connection starts, retaining `"connection"` as the default.
- Allow an eligible cached catalog to satisfy required-server startup validation and model binding capture. Explicit server or plugin requirements still wait for a live connection, and tool execution still requires the current connection.
- Check tool filters and model visibility when accepting cached catalogs, and preserve cached catalog revisions for binding reuse.
- Serialize the new setting, expose it in the configuration schema, and trace startup readiness at validation and model binding gates.

GitOrigin-RevId: a1cbdf1757ebb5824cffe2cc56408be49711c422
This commit is contained in:
Henry Levy
2026-09-24 20:38:50 +00:00
committed by copyberry
parent af09e926d6
commit b62fcd3c4b
34 changed files with 257 additions and 34 deletions
@@ -1,3 +1,5 @@
use std::collections::HashSet;
use anyhow::Result;
use anyhow::anyhow;
use codex_protocol::protocol::McpStartupFailure;
@@ -8,12 +10,13 @@ use super::McpConnectionSet;
use crate::rmcp_client::StartupOutcomeError;
impl McpConnectionSet {
/// Waits for every required server and reports their startup failures together.
/// Waits for required servers unless their readiness policy accepts a cached catalog.
///
/// The manager must already be reachable through [`crate::McpRuntime`] so
/// startup-time elicitation can resolve while validation waits.
pub(crate) async fn validate_required_servers(&self) -> Result<()> {
let failures = async {
self.record_startup_readiness("required_startup", &[], &HashSet::new());
let mut failures = Vec::new();
for server_name in &self.required_servers {
let Some(view) = self.servers.get(server_name) else {
@@ -23,7 +26,8 @@ impl McpConnectionSet {
});
continue;
};
if view.connection.startup_is_dormant() && view.connection.client.has_cached_tools()
if view.allows_cached_startup()
&& view.cached_startup_tools(/*fallback*/ None).is_some()
{
continue;
}
@@ -7,6 +7,7 @@ use std::time::Instant;
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use codex_config::McpStartupReadiness;
use codex_connectors::ConnectorRuntimeFetchSource;
use futures::future::join_all;
use tracing::Instrument;
@@ -59,14 +60,58 @@ pub fn tool_is_model_visible(tool: &ToolInfo) -> bool {
.any(|target| target.as_str() == Some(MCP_UI_MODEL_VISIBILITY))
}
/// Catalog identity within one published connection set, including dormant servers.
/// Catalog identity within one published connection set, including cached declarations.
#[derive(PartialEq, Eq)]
pub(crate) enum BindingCatalogRevision {
Ready(ClientToolCatalogRevision),
Dormant(u64),
Cached(u64),
}
impl McpConnectionSet {
/// Observes opted-in servers at startup gates without waiting or cloning catalogs.
pub(crate) fn record_startup_readiness(
&self,
phase: &'static str,
required_servers: &[String],
required_plugins: &HashSet<String>,
) {
if !tracing::enabled!(tracing::Level::INFO) {
return;
}
for (server_name, view) in &self.servers {
if view.startup_readiness != McpStartupReadiness::Catalog {
continue;
}
let client = &view.connection.client;
let startup_pending = !client.startup_complete.load(Ordering::Acquire);
let cache_eligible = startup_pending
&& client
.tool_catalog_cache_context
.as_ref()
.is_some_and(|cache| {
cache
.current_revision_if(|tools| view.accepts_cached_tools(tools))
.is_some()
});
let explicitly_required = required_servers.contains(server_name)
|| (self.is_selected_plugin_mcp_server(server_name)
&& self
.plugin_id_for_mcp_server_name(server_name)
.is_some_and(|plugin_id| required_plugins.contains(plugin_id)));
tracing::info!(
event.name = "mcp.startup_readiness",
server_name = %server_name,
phase,
startup_pending,
startup_readiness = "catalog",
cache_eligible,
required = self.required_servers.binary_search(server_name).is_ok(),
explicitly_required,
"MCP startup readiness observed"
);
}
}
pub(crate) async fn stable_catalog_revisions(
&self,
required_servers: &[String],
@@ -74,15 +119,17 @@ impl McpConnectionSet {
) -> Option<HashMap<String, BindingCatalogRevision>> {
let mut revisions = HashMap::new();
for (server_name, view) in &self.servers {
if !view
let startup_complete = view
.connection
.client
.startup_complete
.load(Ordering::Acquire)
.load(Ordering::Acquire);
if !startup_complete
|| (view.startup_readiness == McpStartupReadiness::Catalog
&& view.connection.client.ready_client().is_none())
{
// A cached catalog can remain stable without starting its server.
// Explicit requirements must still start the server during binding capture.
if !view.connection.startup_is_dormant()
// Explicit requirements still wait for a live connection during capture.
if !view.allows_cached_startup()
|| view.connection.client.cancel_token.is_cancelled()
|| required_servers
.iter()
@@ -99,10 +146,10 @@ impl McpConnectionSet {
.client
.tool_catalog_cache_context
.as_ref()?
.current_revision()?;
.current_revision_if(|tools| view.accepts_cached_tools(tools))?;
revisions.insert(
server_name.clone(),
BindingCatalogRevision::Dormant(revision),
BindingCatalogRevision::Cached(revision),
);
continue;
}
@@ -222,12 +269,10 @@ impl McpConnectionSet {
{
let required = self.required_servers.binary_search(server_name).is_ok();
// Keep the catalog that lets us skip startup even if it expires during the wait.
let cached_tools = view.connection.client.cached_tools().filter(|tools| {
view.connection.client.is_codex_apps_mcp_server || !tools.is_empty()
});
let cached_tools = view.cached_startup_tools(/*fallback*/ None);
let has_cached_tools = cached_tools.is_some();
let must_wait_for_startup = (required
&& (!view.connection.startup_is_dormant() || !has_cached_tools))
&& (!view.allows_cached_startup() || !has_cached_tools))
|| required_servers
.iter()
.any(|required| required == server_name)
@@ -237,6 +282,7 @@ impl McpConnectionSet {
.is_some_and(|plugin_id| required_plugins.contains(plugin_id)))
|| (server_name == CODEX_APPS_MCP_SERVER_NAME && !has_cached_tools);
if !must_wait_for_startup && has_cached_tools {
trace!(server_name = %server_name, "using cached MCP catalog without waiting for startup");
return (server_name, view, cached_tools);
}
if !must_wait_for_startup && optional_mcp_startup_grace.is_zero() {
@@ -283,14 +329,30 @@ impl McpConnectionSet {
}))
.await;
let server_results = join_all(server_snapshots.into_iter().map(|(server_name, view, cached_tools)| async move {
let (client, server_tools) = if !view
let startup_pending = !view
.connection
.client
.startup_complete
.load(Ordering::Acquire)
.load(Ordering::Acquire);
let accepts_cached_catalog = view.startup_readiness == McpStartupReadiness::Catalog;
let cached_tools = if startup_pending
|| (accepts_cached_catalog && view.connection.client.ready_client().is_none())
{
(None, view.connection.client.cached_tools_or(cached_tools)?)
view.cached_startup_tools(cached_tools)
} else {
None
};
let (client, server_tools) = if let Some(cached_tools) = cached_tools {
(None, cached_tools)
} else {
// A server may opt out of caching after the first pass. Required catalog
// readiness must then wait for discovery rather than silently omit its tools.
if startup_pending
&& !(accepts_cached_catalog
&& self.required_servers.binary_search(server_name).is_ok())
{
return None;
}
view.connection.client.reconnect_failed_startup().await;
let Ok(mut client) = view.connection.client().await else {
trace!(server_name = %server_name, "omitting MCP server without an exact ready client");