Route hook MCP calls through current connections (#39331)

## What changed

- Execute hook-triggered MCP calls through the runtime's latest connection set instead of preparing a separate catalog-bound call.
- Keep hook calls from waiting for server startup or reconnecting, and reject disconnected servers immediately.
- Apply the shorter of the hook-requested timeout and the server's configured tool timeout.

## Testing

- Update connection manager tests to cover non-waiting calls and disabled tool filters.

GitOrigin-RevId: 3166b2d1e26d9ae2b8b3a648b4d0897cc260eb93
This commit is contained in:
Abhinav
2026-08-19 01:15:10 +00:00
committed by copyberry
parent 67ed4e717a
commit d35e5495f9
8 changed files with 84 additions and 269 deletions
@@ -325,82 +325,6 @@ impl McpConnectionSet {
)
}
/// Captures an exact live client and filtered tool without starting or reconnecting a server.
///
/// The prepared call retains the current catalog revision so subsequent execution cannot
/// silently drift to a different connection, tool catalog, or managed configuration.
pub(crate) async fn prepare_connected_call(
self: &Arc<Self>,
config: Arc<crate::McpConfig>,
server: &str,
tool: &str,
) -> Option<PreparedMcpCall> {
// Only consider servers and tools allowed by the current configuration.
let view = self.servers.get(server)?;
if !view.tool_filter.allows(tool) {
return None;
}
// Reuse an existing connection without starting or reconnecting the server.
let mut client = view.connection.client.ready_client()?;
if client.client.is_closed().await {
return None;
}
client.tool_timeout = view.tool_timeout;
// Snapshot the revision before reading tools so concurrent refreshes invalidate the call.
let revision = *self.tool_catalog_revision.read().await;
// Codex Apps catalogs can refresh independently of their MCP connection.
let server_tools = if server == CODEX_APPS_MCP_SERVER_NAME {
self.codex_apps_tools_override
.read()
.await
.clone()
.unwrap_or_else(|| client.tools.clone())
} else {
client.tools.clone()
};
// Filter and annotate tools using the same rules as the rest of the MCP runtime.
let server_tools = filter_tools(server_tools, &view.tool_filter);
let server_tools = if server == CODEX_APPS_MCP_SERVER_NAME {
prepare_codex_apps_tools_for_model(server_tools, &self.tool_plugin_provenance)
} else {
crate::rmcp_client::prepare_regular_mcp_tools_for_model(
server_tools,
&self.tool_plugin_provenance,
)
};
// Look up the requested tool and attach its server's policy metadata.
let tool_info = server_tools
.into_iter()
.find(|tool_info| tool_info.tool.name.as_ref() == tool)
.map(|tool_info| Self::with_server_metadata(tool_info, &view.metadata))?;
// Reject tools from connectors that an administrator disabled in managed requirements.
if server == CODEX_APPS_MCP_SERVER_NAME
&& tool_info
.connector_id
.as_deref()
.and_then(|connector_id| {
config
.config_layer_stack
.requirements_toml()
.apps
.as_ref()?
.apps
.get(connector_id)
})
.is_some_and(|requirements| requirements.enabled == Some(false))
{
return None;
}
self.prepare_call(&tool_info, Arc::new(client), config, revision)
}
fn prepare_call(
self: &Arc<Self>,
tool_info: &ToolInfo,