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

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
This commit is contained in:
Resham Joshi 2026-06-18 22:32:40 +02:00 • committed by GitHub
parent 9e997ddb15
commit 80e3de3bab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 1 deletions

View file

@ -6,7 +6,9 @@ import { homedir } from 'os'
import type { ParsedProviderCall } from './providers/types.js'
const CODEX_CACHE_VERSION = 3
// v4: attribute MCP calls emitted as event_msg/mcp_tool_call_end (issue #478).
// Recent Codex sessions cached under v3 dropped these, so force a re-parse.
const CODEX_CACHE_VERSION = 4
const CACHE_FILE = 'codex-results.json'
type FileFingerprint = { mtimeMs: number; sizeBytes: number }

View file

@ -405,6 +405,22 @@ function createParser(source: SessionSource, seenKeys: Set<string>): SessionPars
continue
}
// Recent Codex emits MCP calls as `event_msg`/`mcp_tool_call_end`
// instead of a `function_call` response_item, so the call was never
// attributed. Rebuild the canonical `mcp__<server>__<tool>` name the
// classifier recognizes.
if (entry.type === 'event_msg' && entry.payload?.type === 'mcp_tool_call_end') {
const inv = (entry.payload as Record<string, unknown>)['invocation'] as Record<string, unknown> | undefined
const server = typeof inv?.['server'] === 'string' ? inv['server'] as string : ''
const tool = typeof inv?.['tool'] === 'string' ? inv['tool'] as string : ''
if (server && tool) {
const name = `mcp__${server}__${tool}`
pendingTools.push(name)
pendingToolSequence.push([{ tool: name }])
}
continue
}
if (entry.type === 'response_item' && entry.payload?.type === 'message' && entry.payload?.role === 'user') {
const texts = normalizeContentBlocks(entry.payload.content)
.filter(c => c.type === 'input_text')

View file

@ -70,6 +70,20 @@ function functionCall(name: string, timestamp?: string) {
})
}
function mcpToolCallEnd(server: string, tool: string, timestamp?: string) {
return JSON.stringify({
type: 'event_msg',
timestamp: timestamp ?? '2026-04-14T10:00:30Z',
payload: {
type: 'mcp_tool_call_end',
call_id: 'call-1',
invocation: { server, tool, arguments: {} },
duration: '1.2s',
result: { Ok: { content: [] } },
},
})
}
function userMessage(text: string, timestamp?: string) {
return JSON.stringify({
type: 'response_item',
@ -279,6 +293,30 @@ describe('codex provider - JSONL parsing', () => {
expect(call.deduplicationKey).toContain('codex:')
})
it('attributes MCP calls emitted as event_msg/mcp_tool_call_end', async () => {
const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-mcp.jsonl', [
sessionMeta({ session_id: 'sess-mcp', model: 'gpt-5.5' }),
userMessage('look up the issue'),
mcpToolCallEnd('github', 'get_issue'),
tokenCount({
timestamp: '2026-04-14T10:01:00Z',
last: { input: 300, output: 100 },
total: { total: 400 },
}),
])
const provider = createCodexProvider(tmpDir)
const source = { path: filePath, project: 'test', provider: 'codex' }
const parser = provider.createSessionParser(source, new Set())
const calls: ParsedProviderCall[] = []
for await (const call of parser.parse()) {
calls.push(call)
}
expect(calls).toHaveLength(1)
expect(calls[0]!.tools).toEqual(['mcp__github__get_issue'])
})
it('normalizes Codex subagent tool calls to Agent', async () => {
const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-agent.jsonl', [
sessionMeta({ session_id: 'sess-agent', model: 'gpt-5.5' }),