Centralize tool registration and protect host tools (#36127)

## Why

External MCP, extension, and dynamic tools can collide with host-owned tool
names. In particular, a dynamic `tool_search` definition must not replace the
host implementation used to discover and route deferred tools.

## What changed

- Build each turn's tools through a single ordered `ToolRegistry` that retains
  the first runtime registered for a name.
- Treat duplicate external tools as skippable collisions while preserving
  strict duplicate checks for trusted host tools.
- Reserve host-owned code-mode and `tool_search` runtimes before producing the
  model-visible tool list.
- Base hosted web-search fallback behavior on the extension runtime that
  actually won registration.

## Testing

- Cover ordered registry mutation and duplicate handling.
- Verify a client-provided `tool_search` cannot shadow the host tool.
- Verify hosted web-search fallback follows the registered browser runtime.

GitOrigin-RevId: acebe360de8ec69ad7da2f684cc2542cbf18a984
This commit is contained in:
jif authored and copyberry committed 2026-07-30 10:07:04 +00:00
1 parent 5decb399ae
commit 89a0eed93c
10 files changed
+443 -356

No files matched your search

+58 -71
View File
@@ -17,93 +17,80 @@ use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::override_tool_exposure;
#[instrument(level = "trace", skip_all)]
pub(crate) fn build_mcp_tool_runtimes(
all_mcp_tools: &[McpToolInfo],
connectors: Option<&[connectors::AppInfo]>,
config: &Config,
pub(crate) fn build_mcp_tool_runtimes<'a>(
all_mcp_tools: &'a [McpToolInfo],
connectors: Option<&'a [connectors::AppInfo]>,
config: &'a Config,
search_tool_enabled: bool,
) -> Vec<Arc<dyn CoreToolRuntime>> {
let mut exposed_tools = filter_non_codex_apps_mcp_tools_only(all_mcp_tools);
if let Some(connectors) = connectors {
exposed_tools.extend(filter_codex_apps_mcp_tools(
all_mcp_tools,
connectors,
config,
));
}
) -> impl Iterator<Item = Arc<dyn CoreToolRuntime>> + 'a {
// Keep regular MCP tools first; Apps tools also require connector and policy checks.
let non_app_tools = filter_non_codex_apps_mcp_tools_only(all_mcp_tools);
let app_tools = connectors
.into_iter()
.flat_map(move |connectors| filter_codex_apps_mcp_tools(all_mcp_tools, connectors, config));
let exposure = if search_tool_enabled {
ToolExposure::Deferred
} else {
ToolExposure::Direct
};
exposed_tools
.into_iter()
.filter_map(|tool| {
let tool_name = tool.canonical_tool_name();
match McpHandler::new(tool) {
Ok(handler) => {
let handler: Arc<dyn CoreToolRuntime> = Arc::new(handler);
Some(override_tool_exposure(handler, exposure))
}
Err(err) => {
warn!("Skipping MCP tool `{tool_name}`: failed to build tool spec: {err}");
None
}
non_app_tools.chain(app_tools).filter_map(move |tool| {
let tool_name = tool.canonical_tool_name();
match McpHandler::new(tool.clone()) {
Ok(handler) => {
let handler: Arc<dyn CoreToolRuntime> = Arc::new(handler);
Some(override_tool_exposure(handler, exposure))
}
})
.collect()
Err(err) => {
warn!("Skipping MCP tool `{tool_name}`: failed to build tool spec: {err}");
None
}
}
})
}
fn filter_non_codex_apps_mcp_tools_only(mcp_tools: &[McpToolInfo]) -> Vec<McpToolInfo> {
mcp_tools
.iter()
.filter(|tool| {
tool.server_name != CODEX_APPS_MCP_SERVER_NAME && tool_is_model_visible(tool)
})
.cloned()
.collect()
}
fn filter_codex_apps_mcp_tools(
fn filter_non_codex_apps_mcp_tools_only(
mcp_tools: &[McpToolInfo],
connectors: &[connectors::AppInfo],
config: &Config,
) -> Vec<McpToolInfo> {
) -> impl Iterator<Item = &McpToolInfo> + '_ {
mcp_tools.iter().filter(|tool| {
tool.server_name != CODEX_APPS_MCP_SERVER_NAME && tool_is_model_visible(tool)
})
}
fn filter_codex_apps_mcp_tools<'a>(
mcp_tools: &'a [McpToolInfo],
connectors: &'a [connectors::AppInfo],
config: &'a Config,
) -> impl Iterator<Item = &'a McpToolInfo> + 'a {
let allowed: HashSet<&str> = connectors
.iter()
.map(|connector| connector.id.as_str())
.collect();
let app_tool_policy = AppToolPolicyEvaluator::new(&config.config_layer_stack);
mcp_tools
.iter()
.filter(|tool| {
if tool.server_name != CODEX_APPS_MCP_SERVER_NAME {
return false;
}
if !tool_is_model_visible(tool) {
return false;
}
let Some(connector_id) = tool.connector_id.as_deref() else {
return false;
};
let annotations = tool.tool.annotations.as_ref();
allowed.contains(connector_id)
&& app_tool_policy
.policy(AppToolPolicyInput {
connector_id: Some(connector_id),
tool_name: &tool.tool.name,
tool_title: tool.tool.title.as_deref(),
destructive_hint: annotations
.and_then(|annotations| annotations.destructive_hint),
open_world_hint: annotations
.and_then(|annotations| annotations.open_world_hint),
})
.enabled
})
.cloned()
.collect()
mcp_tools.iter().filter(move |tool| {
if tool.server_name != CODEX_APPS_MCP_SERVER_NAME {
return false;
}
if !tool_is_model_visible(tool) {
return false;
}
let Some(connector_id) = tool.connector_id.as_deref() else {
return false;
};
let annotations = tool.tool.annotations.as_ref();
allowed.contains(connector_id)
&& app_tool_policy
.policy(AppToolPolicyInput {
connector_id: Some(connector_id),
tool_name: &tool.tool.name,
tool_title: tool.tool.title.as_deref(),
destructive_hint: annotations
.and_then(|annotations| annotations.destructive_hint),
open_world_hint: annotations
.and_then(|annotations| annotations.open_world_hint),
})
.enabled
})
}
#[cfg(test)]