Skip to content

feat(mcp): unsampled per-tool Prometheus metrics for hosted tools/call - #25514

Merged
Git-on-my-level merged 6 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/mcp-metrics
Nov 16, 2026
Merged

Git-on-my-level merged 6 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/mcp-metrics

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Why

To make the hosted MCP server work well for ChatGPT, Claude and other agents, we need to see how it behaves on every call. Today the only per-tool signal is PostHog's MCP Tool Call, which is sampled per user (25% by default) and has no response size. It cannot say which tools fail, how slow they are, how big their answers are, or how often reads are cut short and need another page. The only MCP Prometheus metric is the OAuth token counter.

What changes

Every hosted tools/call that reaches dispatch now updates four unsampled series (backend/utils/mcp_server/tool_metrics.py):

Series Labels Meaning
omi_mcp_tool_calls_total tool, client, outcome, error_category Each call and how it ended
omi_mcp_tool_duration_seconds tool Wall time from admission to result
omi_mcp_tool_response_chars tool Compact JSON characters in a successful result, measured from the text the payload already serialized
omi_mcp_tool_truncated_results_total tool Successful results with truncated=true

Labels are bounded:

  • tool is allowlisted to the registry through mcp_tool_enum, and anything else is unknown;
  • client is the existing analytics client family (chatgpt, claude, other_registered, api_key, unknown), exposed as mcp_client_family;
  • outcome and error_category are closed sets.

No uid, query, id or payload is a label. Worst case is about 30 tools × 5 clients × 6 outcome/category pairs (success, and five error categories: authorization_denied, validation, unknown_tool, internal, and the metrics-only capped for the daily cap or usage budget), and real traffic uses far fewer.

Recording happens in transport._tool_call_analytics, which every dispatch path already calls (success, model-visible error, unknown tool, scope denial, internal error), and in the daily-cap early return and the quota-store outage 503 (counted as internal), which the sampled PostHog event has never covered. Result size and truncation are recorded in the success path after the payload is built. Calls rejected before dispatch (malformed arguments) are counted but add no latency sample. Duration buckets reach 120 seconds. Reloading a collector with a different shape raises an error. It fails open: a broken metric never changes a tool result. REST twins are not counted.

backend/docs/mcp-analytics.md lists the series with starter PromQL (error rate by tool, p95 latency, truncated-read share).

Tests

New backend/tests/unit/test_mcp_tool_metrics.py, 10 tests through transport.handle_mcp_message:

  • success counts the call, its duration, size and truncation;
  • model-visible errors are counted by category;
  • unknown tools collapse to one label;
  • internal failures are counted and still answer;
  • a broken metric never breaks the call;
  • capped calls, quota-store outages and scope denials are counted;
  • client-family labels map to the bounded set;
  • a shape mismatch on reload fails loudly.

An omi-panel review (luna, deepseek, glm-flash) found 7 issues; 6 were fixed here. The seventh, that the histogram test sums cumulative buckets, was rejected: prometheus_client stores per-label buckets non-cumulatively.

All 50 backend/tests/unit/test_mcp_*.py files pass through test.sh, each in its own process.

Release

Default-on. These are additive metrics, with no change to any response. Rollback is reverting this PR.

Not verified: series in the production Prometheus scrape, which needs a deploy.

Failure-Class: none

🤖 Generated with Claude Code

PostHog's MCP Tool Call event is sampled per user (25%) and carries no
response size, so there was no complete answer to which tools fail, how slow
they are, how big their answers are, or how often reads are cut short.

- omi_mcp_tool_calls_total{tool,client,outcome,error_category}
- omi_mcp_tool_duration_seconds{tool}
- omi_mcp_tool_response_chars{tool} (compact JSON text already serialized)
- omi_mcp_tool_truncated_results_total{tool} (results with truncated=true)

Labels are bounded: tool is allowlisted to the registry, client is the
analytics client family. Recording fails open and never changes a result.
backend/docs/mcp-analytics.md lists the series and starter PromQL.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T19:34:37.778816Z 756f8f0 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 516f21db34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread backend/utils/mcp_server/transport.py Resolved
Git-on-my-level and others added 3 commits November 17, 2026 01:12
…y, guard metric shapes

Review follow-ups on the per-tool metrics:
- the daily-cap / usage-budget early return now increments
  omi_mcp_tool_calls_total with error_category=capped (metrics only);
- calls rejected before dispatch add no duration sample, and duration
  buckets extend to 120s;
- reusing a registered collector checks its type and labels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fb057ce70

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Git-on-my-level
Git-on-my-level merged commit eca2d8c into BasedHardware:main Nov 16, 2026
52 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.

1 participant