Skip to content

fix(codex): attribute MCP calls emitted as event_msg/mcp_tool_call_end (#478)#513

Merged
iamtoruk merged 1 commit into
mainfrom
fix/codex-mcp-tool-call-end
Jun 18, 2026
Merged

fix(codex): attribute MCP calls emitted as event_msg/mcp_tool_call_end (#478)#513
iamtoruk merged 1 commit into
mainfrom
fix/codex-mcp-tool-call-end

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Problem

Codex MCP usage shows up in long lookback windows but not in recent ones (today, week, month). Reported in #478.

Recent Codex sessions report a completed MCP tool call as an event_msg with type: "mcp_tool_call_end" instead of a function_call response_item. The provider only built its tool list (pendingTools) from function_call and patch_apply_end events, and MCP is recognized purely by the mcp__<server>__<tool> naming convention reaching that list (classifier.ts:45, parser.ts:965). So these calls were never attributed: token usage was still counted, but the MCP tool itself was dropped. Older sessions emitted MCP as function_call items carrying the mcp__ prefix, which is why long windows still showed it.

Fix

  • Add a handler for event_msg / mcp_tool_call_end that reads invocation.server and invocation.tool and rebuilds the canonical mcp__<server>__<tool> name. Verified against the Codex source event shape (McpToolCallEndEvent -> { type, call_id, invocation: { server, tool, arguments }, ... }). Uses _end so each call is counted once after it completes; empty server/tool are skipped so a malformed event can't produce an invalid name.
  • Bump the Codex result cache to v4. The cache is keyed on file fingerprint, not parser version, so without this bump existing users would keep serving the stale (pre-fix) attribution until session files changed. v4 forces a one-time re-parse.

Validation on real local data

parsed calls MCP attributed
before 1383 none
after 1383 mcp__node_repl__js x5

Matches ground truth: the raw JSONL holds exactly 5 genuine mcp_tool_call_end events across two recent sessions, all dropped before and all attributed after. Total call count unchanged (no double-counting; token totals untouched).

New test attributes MCP calls emitted as event_msg/mcp_tool_call_end asserts the reconstructed name. Full codex suite 18/18, tsc --noEmit clean.

Fixes #478

Recent Codex sessions report completed MCP tool calls as an event_msg with
type mcp_tool_call_end instead of a function_call response_item. The parser
only built its tool list from function_call and patch_apply_end events, so
these MCP calls were never attributed: token usage was still counted, but the
MCP tool itself was missing from recent lookback windows (today/week/month)
while older history still showed it.

Add a handler that reads invocation.server and invocation.tool from the
event and rebuilds the canonical mcp__<server>__<tool> name the classifier
recognizes. Bump the Codex result cache to v4 so sessions cached under v3
re-parse and pick up the previously dropped MCP calls.

Validated on real local data: 5 mcp_tool_call_end events that were dropped
before are now attributed as mcp__node_repl__js, with no change to the total
call count.

Fixes #478
@iamtoruk iamtoruk merged commit 80e3de3 into main Jun 18, 2026
3 checks passed
@iamtoruk iamtoruk deleted the fix/codex-mcp-tool-call-end branch June 18, 2026 20:32
iamtoruk added a commit that referenced this pull request Jun 18, 2026
The export schema emitted tools and shell-commands but no MCP section, and
the tools list is built from extractCoreTools which strips mcp__ names, so
MCP usage never appeared in codeburn export output even when sessions had
MCP activity (issue #496).

Add an mcp section to the JSON export and an mcp.csv to the CSV export,
sourced from session.mcpBreakdown (server -> calls, with share). Mirrors the
existing tools section.

The underlying parse fix (Codex mcp_tool_call_end attribution) landed in
#513; this makes that data visible in exports. Validated on real local data:
the JSON export now reports mcp: [{Server: node_repl, Calls: 5, ...}].

Fixes #496
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex MCP usage is missing from recent lookback windows

1 participant