Skip to content

feat(mcp): log the API error code, platform and param on failed tool calls - #140

Merged
Ooscaar merged 2 commits into
developfrom
mcp-watch/telemetry-error-code
Nov 27, 2026
Merged

Ooscaar merged 2 commits into
developfrom
mcp-watch/telemetry-error-code

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 27, 2026

Copy link
Copy Markdown
Member

Why

The mcp_tool_call telemetry (Axiom dataset zernio-mcp) recorded error_type and status_code for failed calls, but not the API's machine-readable error code:

  • Today's 4x spike of messages_send_inbox_message | LateAPIError | 400 (452 errors in 24 h) could only be explained from the API's own logs. The cause was one agent looping 418 times on an Instagram conversation that Meta kept rejecting.
  • Looking into the metrics_query_meta_ads 400s, we couldn't see which parameter the API rejected, so we couldn't count causes directly.

With these fields, both would have been answerable from zernio-mcp alone.

What

When a call fails with a LateAPIError (or a subclass), its mcp_tool_call line gains up to three fields, read from the top level of the API's error body (exc.details):

Field From Example
error_code code recipient_unavailable
error_param param (the value LateAPIError.__str__ prints as field:) metrics.0
error_platform platform instagram

Sanitising: a value is logged only if it is a str made entirely of [A-Za-z0-9_.:-]. It is then cut to 64 characters. Anything else is dropped, including non-strings, empty strings and text with spaces.

  • No message text, ids or user data are logged. A value that looks like a sentence fails the character rule and is dropped.
  • Tool results and callers are unchanged. Only the log line gets the new fields.
  • The Axiom sink forwards the whole event and there is no field allowlist, so the fields reach Axiom with no other change.

Docs

  • docs/HTTP_DEPLOYMENT.md: the three fields are added to the mcp_tool_call field list.
  • CHANGELOG.md: new entry under Unreleased → Added.

Tests

In tests/test_mcp_telemetry.py:

  • End to end: an error with code, param and platform logs all three fields, and its message text appears nowhere in the line.
  • No details: an error without details gets no new fields.
  • Rate limit: a LateRateLimitError logs its platform quota code and platform.
  • Param: a present param is logged; with no param, no field is added.
  • Bad values: 42, None, a list, a dict, "" and a sentence holding a phone number are dropped for all three fields.
  • Truncation: a 10,000-character code and a 100-character param are cut to 64.

After rebasing on develop at #139: full suite 3940 passed, 44 skipped; mypy src clean; ruff check src tests clean.

Open points

  • Bracketed param paths: a path like platforms[0].accountId is dropped by the character rule, while dotted paths like metrics.0 get through. It's worth checking a real metrics_query_meta_ads 400 body; if brackets show up, [ and ] can be allowed.
  • No Sentry tag: the fields go to the log line (and so Axiom) only. Adding an mcp.error_code tag on the Sentry transaction, next to mcp.error_type, would take about two lines.

🤖 Generated with Claude Code

Ooscaar and others added 2 commits November 27, 2026 17:41
mcp_tool_call recorded error_type and status_code but not the API's
machine-readable error code, so a spike of LateAPIError 400s on
messages_send_inbox_message (one agent looping 418 times on an Instagram
conversation Meta kept rejecting) could only be explained from the API's
own logs. A failed LateAPIError call now also logs error_code and
error_platform from the error envelope's `code` and `platform`. Only
identifier-like strings ([A-Za-z0-9_.:-], at most 64 chars) are kept, so
message text never reaches the logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A failed LateAPIError call's mcp_tool_call line now also logs
error_param from the error envelope's `param`, the field
LateAPIError.__str__ already shows, so 400 causes such as those on
metrics_query_meta_ads can be counted per parameter. Same rules as
error_code: identifier-like strings only, at most 64 chars.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit 79213eb into develop Nov 27, 2026
4 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