mirror of
https://github.com/openai/codex.git
synced 2026-12-06 01:10:58 +00:00
Overlap remote MCP config reads with capability discovery (#65101)
## Why MCP runtime refreshes waited for capability discovery and host MCP configuration to finish before requesting HTTP MCP servers from the selected remote environment. That serialized two reads that can be in flight together. ## What changed Start the remote environment config read alongside host MCP configuration, including executor capability discovery. Start the read's 10-second timeout after host configuration is ready so a serial executor can queue the read behind discovery without consuming its timeout. Merge the results with host server precedence and requirements still enforced. ## Testing Add V1 and V2 integration cases that hold both executor replies until both requests arrive, then verify host precedence and requirements. Add a paused-clock test that covers a queued read and confirms a stalled read times out 10 seconds after discovery completes. GitOrigin-RevId: d974f0f575559bfe6b756de11aa460399fdabf25
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
use super::mcp_refresh::McpRefreshPermit;
|
||||
use super::mcp_runtime::McpDesiredState;
|
||||
use super::*;
|
||||
use crate::McpRefreshReason;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use crate::environment_selection::combine_selected_capability_roots;
|
||||
use crate::mcp::McpRuntimeProjection;
|
||||
use crate::mcp_tool_call::McpToolApprovalContext;
|
||||
use crate::mcp_tool_call::capture_environment_permissions;
|
||||
use anyhow::Context;
|
||||
@@ -179,7 +181,11 @@ impl Session {
|
||||
)
|
||||
.await;
|
||||
let mcp_config = self
|
||||
.project_selected_environment_mcp_servers(config, environments, mcp_projection)
|
||||
.project_selected_environment_mcp_servers(
|
||||
config,
|
||||
environments,
|
||||
std::future::ready(mcp_projection).boxed(),
|
||||
)
|
||||
.await
|
||||
.config;
|
||||
let runtime_context = self.mcp_runtime_context(environments, &host_fallback_cwd);
|
||||
@@ -284,61 +290,23 @@ impl Session {
|
||||
let auth = self.services.auth_manager.auth().await;
|
||||
// Keep current MCP authority even when reusing the step's executor inventory.
|
||||
let environments = self.services.turn_environments.snapshot().await;
|
||||
let environment_selections = environments.all_selections();
|
||||
let desired = self.latest_mcp_desired_state(auth, environments).await;
|
||||
let selected_capability_roots = self
|
||||
.resolve_selected_capability_roots_for_step(&desired.environments)
|
||||
.await;
|
||||
let ready_selected_capability_roots =
|
||||
Self::ready_selected_capability_roots(&selected_capability_roots);
|
||||
let discovered_v2;
|
||||
let executor_capability_discovery_v2 = match captured_executor_discovery_v2 {
|
||||
Some(captured) => Some(captured),
|
||||
None => {
|
||||
(discovered_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
discovered_v2.as_deref()
|
||||
}
|
||||
};
|
||||
let (executor_capability_discovery, _) = self
|
||||
.executor_capability_discovery_for_step(
|
||||
&desired.config,
|
||||
let projection_future = self
|
||||
.host_mcp_projection(
|
||||
&desired,
|
||||
&selected_capability_roots,
|
||||
&desired.environments,
|
||||
executor_capability_discovery_v2,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let mcp_projection = self
|
||||
.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
&desired.config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
executor_capability_discovery_v2,
|
||||
captured_executor_discovery_v2,
|
||||
)
|
||||
.await;
|
||||
.boxed();
|
||||
self.publish_mcp_runtime(
|
||||
&desired,
|
||||
mcp_projection,
|
||||
projection_future,
|
||||
&ready_selected_capability_roots,
|
||||
Some(self.mcp_elicitation_reviewer()),
|
||||
)
|
||||
@@ -347,6 +315,60 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
async fn host_mcp_projection(
|
||||
&self,
|
||||
desired: &McpDesiredState,
|
||||
selected_capability_roots: &[ResolvedSelectedCapabilityRoot],
|
||||
ready_selected_capability_roots: &[SelectedCapabilityRoot],
|
||||
captured_executor_discovery_v2: Option<&[ExecutorCapabilityDiscoveryV2Snapshot]>,
|
||||
) -> McpRuntimeProjection {
|
||||
let environment_selections = desired.environments.all_selections();
|
||||
let discovered_v2;
|
||||
let executor_capability_discovery_v2 = match captured_executor_discovery_v2 {
|
||||
Some(captured) => Some(captured),
|
||||
None => {
|
||||
(discovered_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
discovered_v2.as_deref()
|
||||
}
|
||||
};
|
||||
let (executor_capability_discovery, _) = self
|
||||
.executor_capability_discovery_for_step(
|
||||
&desired.config,
|
||||
selected_capability_roots,
|
||||
&desired.environments,
|
||||
executor_capability_discovery_v2,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
self.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
&desired.config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
executor_capability_discovery_v2,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Refreshes Apps tools on the published thread runtime and returns that client's snapshot.
|
||||
#[tracing::instrument(name = "mcp.runtime.apps_catalog_refresh", skip_all)]
|
||||
pub(crate) async fn refresh_codex_apps_tools(
|
||||
@@ -411,56 +433,25 @@ impl Session {
|
||||
.claim_forced(self.thread_id, McpRefreshReason::HardRefreshCodexAppsTools);
|
||||
let auth = self.services.auth_manager.auth().await;
|
||||
let environments = self.services.turn_environments.snapshot().await;
|
||||
let environment_selections = environments.all_selections();
|
||||
let desired = self.latest_mcp_desired_state(auth, environments).await;
|
||||
let selected_capability_roots = self
|
||||
.resolve_selected_capability_roots_for_step(&desired.environments)
|
||||
.await;
|
||||
let ready_selected_capability_roots =
|
||||
Self::ready_selected_capability_roots(&selected_capability_roots);
|
||||
let (executor_capability_discovery_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let (executor_capability_discovery, _) = self
|
||||
.executor_capability_discovery_for_step(
|
||||
&desired.config,
|
||||
&selected_capability_roots,
|
||||
&desired.environments,
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let mcp_projection = self
|
||||
.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
&desired.config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
.host_mcp_projection(
|
||||
&desired,
|
||||
&selected_capability_roots,
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
/*captured_executor_discovery_v2*/ None,
|
||||
)
|
||||
.await;
|
||||
let mcp_projection = self
|
||||
.project_selected_environment_mcp_servers(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
mcp_projection,
|
||||
std::future::ready(mcp_projection).boxed(),
|
||||
)
|
||||
.await;
|
||||
let selected_plugins = mcp_projection.selected_plugins.clone();
|
||||
@@ -944,49 +935,18 @@ impl Session {
|
||||
&captured_environments,
|
||||
)
|
||||
.await;
|
||||
let environment_selections = environments.all_selections();
|
||||
let desired = self.latest_mcp_desired_state(auth, environments).await;
|
||||
let (executor_capability_discovery_v2, _) = self
|
||||
.executor_capability_discovery_for_step_v2(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let (executor_capability_discovery, _) = self
|
||||
.executor_capability_discovery_for_step(
|
||||
&desired.config,
|
||||
&selected_capability_roots,
|
||||
&desired.environments,
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
/*skill_turn*/ None,
|
||||
)
|
||||
.await;
|
||||
let mcp_projection = self
|
||||
.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
&desired.config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
.host_mcp_projection(
|
||||
&desired,
|
||||
&selected_capability_roots,
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
executor_capability_discovery_v2.as_deref(),
|
||||
/*captured_executor_discovery_v2*/ None,
|
||||
)
|
||||
.await;
|
||||
self.publish_mcp_runtime(
|
||||
&desired,
|
||||
mcp_projection,
|
||||
std::future::ready(mcp_projection).boxed(),
|
||||
&ready_selected_capability_roots,
|
||||
elicitation_reviewer,
|
||||
)
|
||||
|
||||
@@ -123,7 +123,7 @@ impl Session {
|
||||
};
|
||||
self.publish_mcp_runtime(
|
||||
&desired,
|
||||
mcp_projection,
|
||||
std::future::ready(mcp_projection).boxed(),
|
||||
/*ready_selected_capability_roots*/ &[],
|
||||
Some(self.mcp_elicitation_reviewer()),
|
||||
)
|
||||
@@ -141,53 +141,73 @@ impl Session {
|
||||
&'a self,
|
||||
config: &'a Config,
|
||||
environments: &'a TurnEnvironmentSnapshot,
|
||||
mut projection: McpRuntimeProjection,
|
||||
projection_future: BoxFuture<'a, McpRuntimeProjection>,
|
||||
) -> BoxFuture<'a, McpRuntimeProjection> {
|
||||
if self.isolation == codex_extension_api::SessionIsolation::Isolated
|
||||
|| self
|
||||
.services
|
||||
.mcp_thread_init
|
||||
.get::<crate::mcp::McpServersDisabled>()
|
||||
.is_some()
|
||||
|| !environments
|
||||
.turn_environments()
|
||||
.any(|selected| selected.environment().is_remote())
|
||||
{
|
||||
return projection_future;
|
||||
}
|
||||
|
||||
Box::pin(async move {
|
||||
if self.isolation == codex_extension_api::SessionIsolation::Isolated
|
||||
|| self
|
||||
.services
|
||||
.mcp_thread_init
|
||||
.get::<crate::mcp::McpServersDisabled>()
|
||||
.is_some()
|
||||
{
|
||||
return projection;
|
||||
}
|
||||
|
||||
let mut catalog = None;
|
||||
for selected in environments.turn_environments() {
|
||||
let environment = selected.environment();
|
||||
if !environment.is_remote() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let environment_id = &selected.selection.environment_id;
|
||||
let discovery = environment
|
||||
.discover_http_mcp_servers(selected.cwd().clone())
|
||||
.await;
|
||||
let outcome = if discovery.is_ok() {
|
||||
"success"
|
||||
} else {
|
||||
"error"
|
||||
};
|
||||
// Count completed discovery attempts, including refreshes, before host policy
|
||||
// or MCP startup determines whether the server's tools become available.
|
||||
self.services.session_telemetry.counter(
|
||||
"codex.mcp.executor_discovery",
|
||||
/*inc*/ 1,
|
||||
&[("outcome", outcome)],
|
||||
);
|
||||
let servers = match discovery {
|
||||
Ok(servers) => servers,
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
environment_id,
|
||||
%error,
|
||||
"failed to discover executor-local MCP servers"
|
||||
);
|
||||
// A serial executor can queue config behind root discovery. Each read keeps its
|
||||
// full timeout after host projection, which also establishes catalog precedence.
|
||||
let projection_done = tokio::sync::Notify::new();
|
||||
let timeout_start_future = projection_done.notified().shared();
|
||||
let host_projection_future =
|
||||
projection_future.inspect(|_| projection_done.notify_one());
|
||||
let executor_servers_future = async {
|
||||
let mut discovered = Vec::new();
|
||||
for selected in environments.turn_environments() {
|
||||
let environment = selected.environment();
|
||||
if !environment.is_remote() {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let environment_id = &selected.selection.environment_id;
|
||||
let discovery = environment
|
||||
.discover_http_mcp_servers(
|
||||
selected.cwd().clone(),
|
||||
timeout_start_future.clone(),
|
||||
)
|
||||
.await;
|
||||
let outcome = if discovery.is_ok() {
|
||||
"success"
|
||||
} else {
|
||||
"error"
|
||||
};
|
||||
// Count completed discovery attempts, including refreshes, before host policy
|
||||
// or MCP startup determines whether the server's tools become available.
|
||||
self.services.session_telemetry.counter(
|
||||
"codex.mcp.executor_discovery",
|
||||
/*inc*/ 1,
|
||||
&[("outcome", outcome)],
|
||||
);
|
||||
let servers = match discovery {
|
||||
Ok(servers) => servers,
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
environment_id,
|
||||
%error,
|
||||
"failed to discover executor-local MCP servers"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
discovered.push((environment_id, servers));
|
||||
}
|
||||
discovered
|
||||
};
|
||||
let (mut projection, discovered) =
|
||||
tokio::join!(host_projection_future, executor_servers_future);
|
||||
let mut catalog = None;
|
||||
for (environment_id, servers) in discovered {
|
||||
for (name, mut server) in servers {
|
||||
let outcome = if server.enabled {
|
||||
"found"
|
||||
@@ -261,7 +281,7 @@ impl Session {
|
||||
pub(super) async fn publish_mcp_runtime(
|
||||
&self,
|
||||
desired: &McpDesiredState,
|
||||
mcp_projection: McpRuntimeProjection,
|
||||
projection_future: BoxFuture<'_, McpRuntimeProjection>,
|
||||
ready_selected_capability_roots: &[SelectedCapabilityRoot],
|
||||
elicitation_reviewer: Option<ElicitationReviewerHandle>,
|
||||
) {
|
||||
@@ -269,7 +289,7 @@ impl Session {
|
||||
.project_selected_environment_mcp_servers(
|
||||
&desired.config,
|
||||
&desired.environments,
|
||||
mcp_projection,
|
||||
projection_future,
|
||||
)
|
||||
.await;
|
||||
let selected_plugins = mcp_projection.selected_plugins.clone();
|
||||
|
||||
Reference in New Issue
Block a user