From 27b0218b94f3a44fe005a62f5e0ec882f333d80f Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Thu, 25 Dec 2025 00:00:59 +0700 Subject: [PATCH] Fix codex mcp tool call mapping --- .../server/agent/providers/codex-mcp-agent.ts | 84 +++++++++++++++++-- plan.md | 3 +- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/packages/server/src/server/agent/providers/codex-mcp-agent.ts b/packages/server/src/server/agent/providers/codex-mcp-agent.ts index 98e9ff413..d43ffbddf 100644 --- a/packages/server/src/server/agent/providers/codex-mcp-agent.ts +++ b/packages/server/src/server/agent/providers/codex-mcp-agent.ts @@ -221,6 +221,73 @@ function normalizePatchFilesFromChanges(changes: unknown): PatchFileChange[] { return []; } +function extractMcpToolIdentifiers( + item: Record +): { server?: string; tool?: string } { + const toolRecord = + item.tool && typeof item.tool === "object" + ? (item.tool as Record) + : undefined; + const serverRecord = + item.server && typeof item.server === "object" + ? (item.server as Record) + : undefined; + const server = + asString(item.server) ?? + asString(serverRecord?.name) ?? + asString(serverRecord?.id) ?? + asString(item.server_name) ?? + asString(item.serverId) ?? + asString(item.server_id) ?? + asString(item.mcp_server); + const tool = + asString(item.tool) ?? + asString(toolRecord?.name) ?? + asString(toolRecord?.tool) ?? + asString(item.tool_name) ?? + asString(item.toolId) ?? + asString(item.tool_id) ?? + asString(item.name); + if (!server && tool && tool.includes(".")) { + const [serverName, ...toolParts] = tool.split("."); + const toolName = toolParts.join("."); + return { + server: serverName || undefined, + tool: toolName || tool, + }; + } + return { server: server ?? undefined, tool: tool ?? undefined }; +} + +function extractMcpToolPayload( + item: Record +): { input?: unknown; output?: unknown; status?: string } { + const input = + item.input ?? + item.arguments ?? + item.args ?? + item.params ?? + item.request ?? + (item.tool && typeof item.tool === "object" + ? (item.tool as Record).input + : undefined); + const output = + item.output ?? + item.result ?? + item.response ?? + item.return ?? + item.returns ?? + item.result_content ?? + item.content ?? + item.structuredContent ?? + item.structured_content; + const status = + asString(item.status) ?? + asString(item.state) ?? + asString(item.outcome); + return { input, output, status }; +} + function normalizePatchFilesFromFiles(files: unknown): PatchFileChange[] { if (!Array.isArray(files)) { return []; @@ -1651,17 +1718,20 @@ class CodexMcpAgentSession implements AgentSession { output: content ?? output ?? outputValue, }); } - case "mcp_tool_call": + case "mcp_tool_call": { + const { server, tool } = extractMcpToolIdentifiers(item); + const { input, output, status } = extractMcpToolPayload(item); return createToolCallTimelineItem({ - server: item.server as string, - tool: item.tool as string, - status: item.status as string | undefined, + server: server ?? "mcp", + tool: tool ?? "tool", + status: status ?? (item.status as string | undefined), callId, - displayName: `${item.server}.${item.tool}`, + displayName: `${server ?? "mcp"}.${tool ?? "tool"}`, kind: "tool", - input: item.input, - output: item.output, + input, + output, }); + } case "web_search": return createToolCallTimelineItem({ server: "web_search", diff --git a/plan.md b/plan.md index fd53c7708..84a6bb3c0 100644 --- a/plan.md +++ b/plan.md @@ -414,9 +414,10 @@ Build a new Codex MCP provider side‑by‑side with the existing Codex SDK prov - Ensure the E2E coverage test can find `tool: "read_file"` with content. - **Done (2025-12-24 23:52)**: WHAT: normalized top-level `read_file`/`file_read` events into thread items and mapped read file tool calls with path/content input/output in `packages/server/src/server/agent/providers/codex-mcp-agent.ts:1139` and `packages/server/src/server/agent/providers/codex-mcp-agent.ts:1618`. RESULT: read_file tool calls now emit timeline items with tool name, file path input, and content output for Codex MCP. EVIDENCE: Not run (not requested). -- [ ] **Fix**: Codex MCP should emit external MCP tool calls (mcp_tool_call) with input/output in timeline items. +- [x] **Fix**: Codex MCP should emit external MCP tool calls (mcp_tool_call) with input/output in timeline items. - Ensure MCP server tool calls surface `server`, `tool`, `input`, and `output` fields. + - **Done (2025-12-25 00:00)**: WHAT: added MCP tool identifier/payload extraction helpers and used them for `mcp_tool_call` timeline mapping in `packages/server/src/server/agent/providers/codex-mcp-agent.ts:224` and `packages/server/src/server/agent/providers/codex-mcp-agent.ts:1721`. RESULT: MCP tool call timeline items now normalize server/tool/input/output fields from multiple event shapes. EVIDENCE: Not run (not requested). - [ ] **Fix**: Codex MCP web_search timeline items should include query input and results output.