Simplify MCP tool handler plumbing (#21595)

## Why
The MCP tool path had accumulated a few core-owned special cases: a
dedicated payload variant, resolver plumbing, a legacy `AfterToolUse`
translation path, and a side channel for parallel-call metadata. That
made `ToolRegistry` and the spec builder know more about MCP than they
needed to.

This change moves MCP-specific execution details back onto `ToolInfo`
and `McpHandler` so `codex-core` can treat MCP calls like normal
function calls while still preserving MCP-specific dispatch and
telemetry behavior where it belongs.

## What changed
- removed `resolve_mcp_tool_info`, `ToolPayload::Mcp`, `ToolKind`, and
the remaining registry-side MCP resolver path
- stored MCP routing metadata directly on `McpHandler` and `ToolInfo`,
including `supports_parallel_tool_calls`
- deleted the legacy `AfterToolUse` consumer in `core`, which removes
the need for handler-specific `after_tool_use_payload` implementations
- switched tool-result telemetry to handler-provided tags and kept
MCP-specific dispatch payload construction inside the handler
- simplified tool spec planning/building by passing `ToolInfo` directly
and dropping the direct/deferred MCP wrapper structs and the
parallel-server side table

## Testing
- `cargo check -p codex-core -p codex-mcp -p codex-otel`
- `cargo test -p codex-core
mcp_parallel_support_uses_exact_payload_server`
- `cargo test -p codex-core
direct_mcp_tools_register_namespaced_handlers`
- `cargo test -p codex-core
search_tool_description_lists_each_mcp_source_once`
- `cargo test -p codex-mcp
list_all_tools_uses_startup_snapshot_while_client_is_pending`
- `just fix -p codex-core -p codex-mcp -p codex-otel`
This commit is contained in:
pakrym-oai
2026-05-12 00:11:31 +00:00
committed by GitHub
parent e16b4e46d4
commit ed5944ba1d
70 changed files with 412 additions and 638 deletions
+12 -8
View File
@@ -65,6 +65,12 @@ const RESPONSES_API_ENGINE_SERVICE_TTFT_FIELD: &str = "engine_service_ttft_total
const RESPONSES_API_ENGINE_IAPI_TBT_FIELD: &str = "engine_iapi_tbt_across_engine_calls_ms";
const RESPONSES_API_ENGINE_SERVICE_TBT_FIELD: &str = "engine_service_tbt_across_engine_calls_ms";
fn trace_field_value<'a>(fields: &'a [(&str, &str)], key: &str) -> Option<&'a str> {
fields
.iter()
.find_map(|(field_key, value)| (*field_key == key).then_some(*value))
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AuthEnvTelemetryMetadata {
pub openai_api_key_env_present: bool,
@@ -943,8 +949,7 @@ impl SessionTelemetry {
call_id: &str,
arguments: &str,
extra_tags: &[(&str, &str)],
mcp_server: Option<&str>,
mcp_server_origin: Option<&str>,
extra_trace_fields: &[(&str, &str)],
f: F,
) -> Result<(String, bool), E>
where
@@ -969,8 +974,7 @@ impl SessionTelemetry {
success,
output.as_ref(),
extra_tags,
mcp_server,
mcp_server_origin,
extra_trace_fields,
);
result
@@ -1010,8 +1014,7 @@ impl SessionTelemetry {
success: bool,
output: &str,
extra_tags: &[(&str, &str)],
mcp_server: Option<&str>,
mcp_server_origin: Option<&str>,
extra_trace_fields: &[(&str, &str)],
) {
let success_str = if success { "true" } else { "false" };
let mut tags = Vec::with_capacity(2 + extra_tags.len());
@@ -1020,8 +1023,9 @@ impl SessionTelemetry {
tags.extend_from_slice(extra_tags);
self.counter(TOOL_CALL_COUNT_METRIC, /*inc*/ 1, &tags);
self.record_duration(TOOL_CALL_DURATION_METRIC, duration, &tags);
let mcp_server = mcp_server.unwrap_or("");
let mcp_server_origin = mcp_server_origin.unwrap_or("");
let mcp_server = trace_field_value(extra_trace_fields, "mcp_server").unwrap_or("");
let mcp_server_origin =
trace_field_value(extra_trace_fields, "mcp_server_origin").unwrap_or("");
log_event!(
self,
event.name = "codex.tool_result",