mirror of
https://github.com/openai/codex.git
synced 2026-11-22 15:15:18 +00:00
Avoid blocking turns on optional MCP startup (#35742)
## Why A pending optional MCP server can delay the first model request even when the turn does not need that server. ## What changed - Give optional MCP servers a shared one-second startup grace, then omit servers that are still pending from the captured tool catalog. - Continue waiting when the turn explicitly requires a server through a plugin, skill dependency, or `mcp://` mention, and preserve this behavior for input received between sampling steps. - Route direct resource requests to the live connection set when a pending server was omitted from the binding, while keeping all-server resource discovery non-blocking. ## Testing - Cover the shared grace period and resource behavior for pending optional servers. - Verify plugin, plugin-skill, and direct MCP mentions wait for startup. - Verify an Apps-enabled turn proceeds without tools from an unrelated pending optional server. GitOrigin-RevId: b5f895c5a5362fe73f7d33250367662d4a217e4d
This commit is contained in:
1 parent
8e271dc02b
commit
d9e1c9cd55
12 files changed
+519
-58
No files matched your search
@@ -4,6 +4,7 @@ use std::time::Duration;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::anyhow;
|
||||
use rmcp::model::ListResourceTemplatesResult;
|
||||
use rmcp::model::ListResourcesResult;
|
||||
use rmcp::model::PaginatedRequestParams;
|
||||
use rmcp::model::ReadResourceRequestParams;
|
||||
@@ -154,6 +155,19 @@ impl McpConnectionSet {
|
||||
.with_context(|| format!("resources/list failed for `{server}`"))
|
||||
}
|
||||
|
||||
pub async fn list_resource_templates(
|
||||
&self,
|
||||
server: &str,
|
||||
params: Option<PaginatedRequestParams>,
|
||||
) -> Result<ListResourceTemplatesResult> {
|
||||
let (managed, timeout) = self.client_by_name(server).await?;
|
||||
managed
|
||||
.client
|
||||
.list_resource_templates(params, timeout)
|
||||
.await
|
||||
.with_context(|| format!("resources/templates/list failed for `{server}`"))
|
||||
}
|
||||
|
||||
pub async fn read_resource(
|
||||
&self,
|
||||
server: &str,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Context;
|
||||
@@ -30,6 +31,7 @@ use crate::tools::normalize_tools_for_model_with_prefix;
|
||||
const MCP_UI_META_KEY: &str = "ui";
|
||||
const MCP_UI_VISIBILITY_META_KEY: &str = "visibility";
|
||||
const MCP_UI_MODEL_VISIBILITY: &str = "model";
|
||||
const OPTIONAL_MCP_STARTUP_GRACE: Duration = Duration::from_secs(1);
|
||||
|
||||
/// Returns whether a tool may be included in model-facing tool declarations.
|
||||
///
|
||||
@@ -132,10 +134,14 @@ impl McpConnectionSet {
|
||||
self: &Arc<Self>,
|
||||
config: Arc<crate::McpConfig>,
|
||||
plugins_available: bool,
|
||||
required_servers: &[String],
|
||||
) -> McpBinding {
|
||||
let revision = self.tool_catalog_revision.read().await;
|
||||
let mut listed_tools = Vec::new();
|
||||
let mut clients = std::collections::HashMap::new();
|
||||
let optional_startup_deadline = *self
|
||||
.optional_startup_deadline
|
||||
.get_or_init(|| tokio::time::Instant::now() + OPTIONAL_MCP_STARTUP_GRACE);
|
||||
for (server_name, view) in &self.servers {
|
||||
if !view
|
||||
.connection
|
||||
@@ -143,7 +149,15 @@ impl McpConnectionSet {
|
||||
.startup_complete
|
||||
.load(Ordering::Acquire)
|
||||
{
|
||||
if view.connection.client.has_cached_tools() {
|
||||
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
|
||||
|| self.is_selected_plugin_mcp_server(server_name)
|
||||
|| required_servers
|
||||
.iter()
|
||||
.any(|required| required == server_name)
|
||||
|| (server_name == CODEX_APPS_MCP_SERVER_NAME && !has_cached_tools);
|
||||
if !must_wait_for_startup && has_cached_tools {
|
||||
if let Some(server_tools) =
|
||||
view.listed_tools(&self.tool_plugin_provenance).await
|
||||
{
|
||||
@@ -156,6 +170,17 @@ impl McpConnectionSet {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !must_wait_for_startup
|
||||
&& tokio::time::timeout_at(
|
||||
optional_startup_deadline,
|
||||
view.connection.client.client(),
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
trace!(server_name = %server_name, "omitting pending optional MCP server");
|
||||
continue;
|
||||
}
|
||||
let _ = view.connection.client.client().await;
|
||||
}
|
||||
view.connection.client.reconnect_failed_startup().await;
|
||||
|
||||
Reference in new issue
Block a user