fix(mcp-server): attribute mcp_metrics session via client-identity fallback#546
Merged
NagyVikt merged 1 commit intoMay 14, 2026
Conversation
…llback `colony gain` was bucketing ~9,000 calls/day into a single `<unknown>` session row because high-volume read-only tools (task_plan_list, get_observations, search, task_timeline, list_sessions, examples_list, …) have no `session_id` in their schema — `metricContextOf` had nothing to attribute the call to and recorded `session_id: null`, which the sessions aggregate SQL collapses into `<unknown>`. The metrics wrapper now reuses the same `detectMcpClientIdentity` heuristic the heartbeat wrapper already runs per call: env-derived identity (CODEX_SESSION_ID, CLAUDECODE_SESSION_ID, COLONY_CLIENT_SESSION_ID) or the stable `mcp-<ppid>` fallback when no signal is available. Explicit `args.session_id` / `args.current_session_id` still win — the fallback only fires when both are absent. Effect: per-client session rows replace the giant `<unknown>` bucket in `colony gain` so the next regression investigation can see WHICH agent is driving the bulk of the load — the gap PR #531's compact-mode fix left open. Tests (apps/mcp-server/test/metrics-wrapper.test.ts): * explicit args.session_id still wins (regression guard) * sessionless tool + CODEX_SESSION_ID env → codex session bucket * sessionless tool + no env signal → stable `mcp-<ppid>` bucket (two calls collapse into one row, not 2 `<unknown>` rows) mcp-server suite: 276/276 PASS. Lint clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Automated by gx branch finish (PR flow).