Skip to content

Attribute CLI-wrapped MCP calls in Codex + re-parse cached sessions (fixes #478)#656

Merged
iamtoruk merged 1 commit into
mainfrom
fix/codex-cli-mcp-attribution
Jul 10, 2026
Merged

Attribute CLI-wrapped MCP calls in Codex + re-parse cached sessions (fixes #478)#656
iamtoruk merged 1 commit into
mainfrom
fix/codex-cli-mcp-attribution

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Fixes #478.

Problem

The reporter runs MCP tools through a CLI wrapper (philschmid/mcp-cli) rather than registering servers natively in Codex. So Codex logs a plain exec_command with no mcp_tool_call_end event, and the MCP usage showed only as a shell command, absent from the MCP breakdown. #513 fixed the native path; this is the CLI-wrapped follow-up, and the reporter confirmed it was still broken for their setup.

Fix

  • Attribute CLI-wrapped calls: recognize mcp-cli [options] call <server> <tool> in the exec command and also emit mcp__<server>__<tool>, so it flows into the MCP breakdown. The exec still counts as Bash (it genuinely is a shell exec, same call, two breakdowns, no token double-count). Flag-tolerant (mcp-cli -c ./cfg call ...), quote/path/bash -lc tolerant, scoped to the mcp-cli binary (rejects foo-mcp-cli), and only the call subcommand matches (info/grep/listing are lookups).
  • Re-parse cached sessions (the load-bearing part): registered codex in PROVIDER_PARSE_VERSIONS. session-cache.json serves unchanged files without invoking the provider parser, so a version bump alone would never re-attribute already-cached sessions, the fix would appear to do nothing for existing users. This also retroactively repairs fix(codex): attribute MCP calls emitted as event_msg/mcp_tool_call_end (#478) #513 for anyone whose files were cached before it shipped.
  • Bumped CODEX_CACHE_VERSION 4→5 for the provider-internal layer.

Verification

Review

Reviewed with a Fable 5 adversarial pass (Codex CLI is temporarily unavailable). It mutation-verified the cache gap and caught that the reporter likely uses mcp-cli -c (flags before the subcommand), which the first-draft regex missed entirely, so the fix would have appeared broken for the exact reporting user. Both are folded in.

Honest scope note

Validated against synthetic sessions matching the documented mcp-cli command shape (the reporter didn't attach their raw session). Scoped to the mcp-cli binary; other CLI-MCP wrappers would need their own pattern. Substring matching means a command that merely mentions mcp-cli ... call x y (a comment, an echo) can false-positive, an accepted tradeoff, documented in the code.

…ached sessions (#478)

Users who run MCP tools through a CLI wrapper (e.g. philschmid/mcp-cli)
instead of registering servers natively don't produce Codex
mcp_tool_call_end events; Codex logs a plain exec_command. So their MCP
usage showed only as a shell command and was absent from the MCP
breakdown, even after #513 fixed the native path.

- Recognize `mcp-cli [options] call <server> <tool>` in the exec command
  and also attribute it as mcp__<server>__<tool>. The exec still counts as
  Bash since it genuinely is a shell exec. Flag-tolerant, quote/path/bash-lc
  tolerant, and scoped to the mcp-cli binary (not foo-mcp-cli); only the
  `call` subcommand (a real execution) matches, not info/grep/listing.
- Register `codex` in PROVIDER_PARSE_VERSIONS. session-cache.json serves
  unchanged session files without invoking the provider parser, so the
  codex-cache version bump alone would never re-attribute already-cached
  sessions. This also retroactively repairs #513's native-MCP fix for
  users whose files were cached before it shipped.
- Bump CODEX_CACHE_VERSION 4->5 for the provider-internal layer.

Tested: parse cases (bash -lc wrapper, flags-before-call, argv array,
plus info/grep/foo-mcp-cli negatives), and a mutation-verified cache
regression test that fails without the PROVIDER_PARSE_VERSIONS entry.
ReDoS-checked. Reviewed with a Fable 5 adversarial pass (Codex is down);
its two must-fixes (the cache gap and the flags-before-call miss that
would have missed the reporter's own sessions) are folded in.

Fixes #478
@iamtoruk iamtoruk merged commit b78e862 into main Jul 10, 2026
3 checks passed
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