Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Prepare MCP calls directly from advertised tool identities (#47981)

Henry Levy75e0e0aad97a
diff --git a/codex-rs/codex-mcp/src/binding.rs b/codex-rs/codex-mcp/src/binding.rs
index ade3747729..f0a95b95c6 100644
--- a/codex-rs/codex-mcp/src/binding.rs
+++ b/codex-rs/codex-mcp/src/binding.rs
@@ -166,8 +166,7 @@ impl fmt::Debug for McpBinding {
}
}
-/// A call bound to the exact client, tool, timeout, and server metadata seen by
-/// one [`McpBinding`].
+/// A call bound to an exact client, catalog revision, timeout, and permission authority.
#[derive(Clone)]
pub struct PreparedMcpCall {
connections: Arc<McpConnectionSet>,
diff --git a/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs b/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs
index bce7aeb3cf..8d8a9f84b5 100644
--- a/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs
+++ b/codex-rs/codex-mcp/src/connection_manager/tool_catalog.rs
@@ -447,6 +447,52 @@ impl McpConnectionSet {
)
}
+ #[instrument(level = "trace", skip_all)]
+ pub(crate) async fn prepare_call_for_tool(
+ self: &Arc<Self>,
+ config: Arc<crate::McpConfig>,
+ advertised_tool: &ToolInfo,
+ ) -> Option<PreparedMcpCall> {
+ let server_name = &advertised_tool.server_name;
+ let view = self.servers.get(server_name)?;
+ if !view.tool_filter.allows(&advertised_tool.tool.name) {
+ return None;
+ }
+ let mut client = view.connection.client().await.ok()?;
+ client.tool_timeout = view.tool_timeout;
+ let snapshot = client.tool_catalog.read(Arc::new).await;
+ let current_tool = snapshot.tools.iter().find(|tool| {
+ tool.server_name == *server_name
+ && tool.tool.name == advertised_tool.tool.name
+ && tool.connector_id == advertised_tool.connector_id
+ })?;
+ let mut tool_info = if server_name == CODEX_APPS_MCP_SERVER_NAME {
+ prepare_codex_apps_tools_for_model(
+ vec![current_tool.clone()],
+ &self.tool_plugin_context,
+ )
+ } else {
+ crate::rmcp_client::prepare_regular_mcp_tools_for_model(
+ vec![current_tool.clone()],
+ &self.tool_plugin_context,
+ )
+ }
+ .pop()?;
+ if !tool_is_model_visible(&tool_info) {
+ return None;
+ }
+ tool_info = Self::with_server_metadata(tool_info, &view.metadata);
+ // Preserve the globally normalized identity advertised to the model, while
+ // taking schema, annotations, and approval metadata from the current catalog.
+ tool_info
+ .callable_namespace
+ .clone_from(&advertised_tool.callable_namespace);
+ tool_info
+ .callable_name
+ .clone_from(&advertised_tool.callable_name);
+ self.prepare_call(&tool_info, Arc::new(client), config, snapshot)
+ }
+
fn prepare_call(
self: &Arc<Self>,
tool_info: &ToolInfo,
diff --git a/codex-rs/codex-mcp/src/runtime.rs b/codex-rs/codex-mcp/src/runtime.rs
index 4234906260..c0f670ef09 100644
--- a/codex-rs/codex-mcp/src/runtime.rs
+++ b/codex-rs/codex-mcp/src/runtime.rs
@@ -49,6 +49,7 @@ use tokio_util::sync::CancellationToken;
use crate::McpConfig;
use crate::binding::McpBinding;
+use crate::binding::PreparedMcpCall;
use crate::client_tool_catalog::CodexAppsToolSnapshot;
use crate::connection_manager::BindingCatalogRevision;
use crate::connection_manager::McpConnectionSet;
@@ -535,6 +536,16 @@ impl McpRuntime {
.await;
}
+ /// Prepares one advertised tool against this publication's current client and policy.
+ /// Its model-visible name stays fixed while execution metadata comes from the live catalog.
+ pub async fn prepare_call(&self, advertised_tool: &ToolInfo) -> Option<PreparedMcpCall> {
+ let current = self.current.load_full();
+ current
+ .connections
+ .prepare_call_for_tool(Arc::clone(current.config.as_ref()?), advertised_tool)
+ .await
+ }
+
/// Captures the current runtime after its selected server has finished startup.
pub async fn current_binding_for_call(&self, server: &str) -> Option<Arc<McpBinding>> {
let current = self.current.load_full();
diff --git a/codex-rs/core/src/session/mcp_runtime.rs b/codex-rs/core/src/session/mcp_runtime.rs
index 2bbee2d5ed..e40ba8c6dd 100644
--- a/codex-rs/core/src/session/mcp_runtime.rs
+++ b/codex-rs/core/src/session/mcp_runtime.rs
@@ -15,6 +15,7 @@ use codex_mcp::McpServerRegistration;
use codex_mcp::McpServerSource;
use codex_mcp::McpStartupPolicy;
use codex_mcp::PreparedMcpCall;
+use codex_mcp::ToolInfo;
use codex_protocol::capabilities::SelectedCapabilityRoot;
use std::collections::HashSet;
@@ -55,15 +56,13 @@ impl Session {
/// Captures this session's current MCP client and catalog for one tool call.
pub(crate) async fn prepare_mcp_call(
self: &Arc<Self>,
- server: &str,
- tool: &str,
+ advertised_tool: &ToolInfo,
) -> Option<PreparedMcpCall> {
self.refresh_mcp_if_dirty().await;
self.services
.mcp_runtime
- .current_binding_for_call(server)
- .await?
- .prepare_call(server, tool)
+ .prepare_call(advertised_tool)
+ .await
}
pub(super) async fn latest_mcp_desired_state(
diff --git a/codex-rs/core/src/tools/handlers/mcp.rs b/codex-rs/core/src/tools/handlers/mcp.rs
index e664da954f..78e2a3fda4 100644
--- a/codex-rs/core/src/tools/handlers/mcp.rs
+++ b/codex-rs/core/src/tools/handlers/mcp.rs
@@ -197,13 +197,7 @@ impl McpHandler {
&self,
invocation: ToolInvocation,
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
- let prepared_mcp_call = invocation
- .session
- .prepare_mcp_call(
- &self.tool_info.server_name,
- self.tool_info.tool.name.as_ref(),
- )
- .await;
+ let prepared_mcp_call = invocation.session.prepare_mcp_call(&self.tool_info).await;
// Use the executed call's binding; a later catalog refresh must not change eligibility.
let result_metadata_capture_allowed = invocation
.session