Centralize thread MCP state in McpRuntime (#34930)

## What changed

- Make `McpRuntime` own the published MCP configuration, connections, elicitation routing, and selected capability roots for a thread.
- Capture immutable MCP bindings for model steps and tool calls so in-flight work keeps a consistent connection set and approval authority while refreshed state is published atomically.
- Mark MCP state dirty when relevant configuration, plugins, environments, authentication, or elicitation capabilities change, then rebuild it before the next sampling request or out-of-band MCP operation.
- Separate config reloads from server invalidation: `ReloadMcpConfig` applies resolved MCP inputs, while `RefreshMcpServers` requests reinitialization from the thread's latest state.

## Testing

- Cover refreshed state visibility for existing turns, stable step bindings, current approval authority, cancelled refresh retries, resource-client reconciliation, and Apps recovery between sampling requests.

GitOrigin-RevId: 59eabb1aa8dc083426bd18ef4d3630508f376401
This commit is contained in:
jif
2026-07-23 11:27:34 +00:00
committed by copyberry
parent 39a2438d16
commit e497325a6a
54 changed files with 1792 additions and 2038 deletions
@@ -6,12 +6,12 @@ use codex_connectors::connector_tool_is_synthetic;
use codex_connectors::installed_connector_runtime;
use codex_mcp::CODEX_APPS_MCP_SERVER_NAME;
use codex_mcp::MCP_TOOL_CODEX_APPS_META_KEY;
use codex_mcp::McpConnectionSet;
use codex_mcp::McpRuntime;
use codex_mcp::McpRuntimeInput;
use codex_mcp::ToolInfo;
use codex_mcp::effective_mcp_servers;
use codex_mcp::host_owned_codex_apps_enabled;
use codex_mcp::tool_is_model_visible;
use codex_mcp::tool_plugin_provenance;
use codex_protocol::models::PermissionProfile;
const CONNECTOR_RUNTIME_REFRESH_TIMEOUT: Duration = Duration::from_secs(30);
@@ -52,7 +52,10 @@ impl AppsRequestProcessor {
let runtime_enabled = apps_enabled && workspace_enabled;
let mcp_manager = self.thread_manager.mcp_manager();
let mcp_config = mcp_manager.runtime_config(&config).await;
let mut mcp_config = mcp_manager.runtime_config(&config).await;
// Installed-app discovery has no active turn or reviewer.
mcp_config.permission_profile = PermissionProfile::default();
let mcp_config = Arc::new(mcp_config);
let mut mcp_servers = effective_mcp_servers(&mcp_config, auth.as_ref());
mcp_servers.retain(|name, _| name == CODEX_APPS_MCP_SERVER_NAME);
let cache_key = connector_runtime_context_key(auth.as_ref());
@@ -78,34 +81,31 @@ impl AppsRequestProcessor {
let codex_apps_auth_manager =
host_owned_codex_apps_enabled(&mcp_config, auth.as_ref())
.then(|| Arc::clone(&self.auth_manager));
let connection_manager = McpConnectionSet::new(
&mcp_servers,
config.mcp_oauth_credentials_store_mode,
config.auth_keyring_backend_kind(),
&config.permissions.approval_policy,
APPS_INSTALLED_SUBMIT_ID.to_string(),
/*tx_event*/ None,
cancellation_token.clone(),
PermissionProfile::default(),
let runtime = McpRuntime::new(McpRuntimeInput {
config: Arc::clone(&mcp_config),
plugins_available: false,
ready_selected_capability_roots: Vec::new(),
mcp_servers,
submit_id: APPS_INSTALLED_SUBMIT_ID.to_string(),
tx_event: None,
startup_cancellation_token: cancellation_token.clone(),
runtime_context,
mcp_config.codex_home.clone(),
mcp_manager.codex_apps_tools_cache(),
mcp_manager.tool_catalog_cache(),
cache_key.clone(),
mcp_config.prefix_mcp_tool_names,
mcp_config.client_elicitation_capability.clone(),
/*supports_openai_form_elicitation*/ false,
tool_plugin_provenance(&mcp_config),
auth.as_ref(),
codex_apps_tools_cache: mcp_manager.codex_apps_tools_cache(),
tool_catalog_cache: mcp_manager.tool_catalog_cache(),
codex_apps_tools_cache_key: cache_key.clone(),
supports_openai_form_elicitation: false,
auth: auth.clone(),
codex_apps_auth_manager,
/*elicitation_reviewer*/ None,
/*elicitation_lifecycle*/ None,
codex_mcp::ElicitationRequestRouter::default(),
)
elicitation_reviewer: None,
elicitation_lifecycle: None,
})
.await;
let result = if connection_manager
.wait_for_server_ready(CODEX_APPS_MCP_SERVER_NAME, startup_timeout)
let result = if runtime
.latest_wait_for_server_ready(
CODEX_APPS_MCP_SERVER_NAME,
startup_timeout,
)
.await
{
mcp_manager
@@ -122,7 +122,7 @@ impl AppsRequestProcessor {
))
};
cancellation_token.cancel();
connection_manager.shutdown().await;
runtime.shutdown().await;
result
}
.await;