Skip to content

feat(http): trace tool calls to Sentry - #246

Merged
Gerardo Sabetta (gerardosabetta) merged 4 commits into
mainfrom
surf-2269-sentry-mcp-tracing
Nov 30, 2026
Merged

Gerardo Sabetta (gerardosabetta) merged 4 commits into
mainfrom
surf-2269-sentry-mcp-tracing

Conversation

@gerardosabetta

@gerardosabetta Gerardo Sabetta (gerardosabetta) commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Sentry's MCP dashboard is empty because socket-mcp sends only errors (#243). Each tools/call now runs in a root transaction that the dashboard reads. Tracing is off unless SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE is set above 0 along with the DSN.

What changed

  • error-reporting-sentry.mts: setCurrentClient, tracesSampleRate from the new env var (default 0), and beforeSendTransaction, which rebuilds each transaction from an allowlist the same way beforeSend rebuilds events. Inputs, outputs, headers, user data, tags and child spans are dropped.
  • server.mts: the tools/call handler runs inside startSpan({ name: 'tools/call <tool>', op: 'mcp.server', forceTransaction: true, ... }) with mcp.method.name, mcp.tool.name, mcp.transport (the server's real transport), network.transport, mcp.client.name/version and mcp.tool.result.is_error. The handler body is unchanged apart from its indentation.
  • Client names: a client named only by its User-Agent now gets the hosted proxy's client family (claude-code, cursor, curl, …, else other), always without a prefix.

Heads-up for reviewers

  • wrapMcpServerWithSentry can't be used here, because the server uses the low-level Server with setRequestHandler. The span is manual and follows the Sentry MCP cookbook.
  • Each call is its own trace (startNewTrace, parentSpan: null). Core's default scope stack isn't async-aware, so concurrent calls would otherwise parent each other.
  • mcp.client tags change for clients named only by their User-Agent: unlisted products are now other, with no version.
  • The hosted proxy still prefixes User-Agent families with ua:. Dropping that prefix is a depscan follow-up, listed below.

Testing

  • pnpm test: 74 files, 692 tests pass. New cases: the span attributes over the real HTTP stack (success, upstream 502, unknown tool), no transactions when the sample rate is unset, a separate trace per call, a canary leak check, and sanitizeTransaction allowlist unit tests.
  • pnpm run check passes (fmt, lint, typecheck, build, versions, scan). The e2e test test/e2e/sentry.test.mts passes.

Follow-ups (out of scope)

  • Hosted proxy (depscan) tracing stays in Tempo. Dropping the proxy's ua: prefix so both layers send bare client names.
  • Sentry alert rules on the MCP transactions.
  • Source maps upload.
Overlap check

No worktree or branch named surf-2269-mcp-metrics exists locally (git worktree list, git branch -a), so overlap with that work couldn't be checked.

Slides

cover

gap

flow

attributes

privacy

headsup

verify

followups

🤖 Generated with Claude Code

Sentry's MCP dashboard stays empty because the server sends only
errors. Each tools/call now runs in a root transaction named
`tools/call <tool>` with op `mcp.server`, carrying the tool, client,
transport and whether the result was an error. Tracing is off unless
SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE is set above 0 with the DSN.

Transactions are rebuilt from an allowlist before they leave the
process, like error events: no child spans, arguments, results,
headers, user data or tags.

A client named only by its User-Agent is now tagged with the same
client family the hosted proxy uses (`claude-code`, `cursor`, `curl`,
or `other`), so both layers name a client the same way.
Gerardo Sabetta (gerardosabetta) added a commit that referenced this pull request Nov 30, 2026
@gerardosabetta
Gerardo Sabetta (gerardosabetta) marked this pull request as ready for review November 30, 2026 14:02
@cursor

cursor Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes observability and client identification on every HTTP tools/call when tracing is enabled; default sample rate 0 preserves prior errors-only behavior.

Overview
Adds opt-in Sentry performance tracing so each sampled tools/call becomes a root transaction (tools/call <tool>) with MCP dashboard attributes. Tracing stays off unless SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE is set between 0 and 1 alongside SOCKET_MCP_SENTRY_DSN.

The tools/call handler is wrapped in traceToolCall; the Sentry client gains beforeSendTransaction / sanitizeTransaction with the same allowlist-style redaction as error events (no args, headers, child spans, or tokens). Each call uses its own trace so concurrent requests do not nest.

Client tagging when clientInfo is missing now maps the User-Agent product to known families (claude-code, cursor, curl, …) or other without a version, aligning error and trace metadata with the hosted proxy.

Reviewed by Cursor Bugbot for commit 523c96f. Configure here.

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread lib/error-reporting-sentry.mts Resolved
Tools report most failures as an error result rather than throwing, so
those transactions ended with an ok status and Sentry's failure rate
counted them as successes. A call whose outcome is not success now
gets error.type tool_error and the internal_error status, as Sentry's
own MCP integration does.
With SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE unset or 0, the reporter no
longer installs Sentry's global client and tool calls run without a
span wrapper, so errors-only deployments behave as before tracing.
@vasco-santos

Copy link
Copy Markdown
Contributor

bugbot review

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[agent] The failed-call status and rate-zero changes address the earlier feedback. The remaining test gap is overlapping HTTP requests, where Sentry’s shared scope matters.

Comment thread docs/service.md Resolved

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 523c96f. Configure here.

The trace test only covered back-to-back calls and passed if one trace
id was missing. It now checks both are valid root traces, and a new
case holds one call open upstream while another finishes. The tracing
docs now split setup from what Sentry shows.
@gerardosabetta
Gerardo Sabetta (gerardosabetta) merged commit 03f98ae into main Nov 30, 2026
12 checks passed
@gerardosabetta
Gerardo Sabetta (gerardosabetta) deleted the surf-2269-sentry-mcp-tracing branch November 30, 2026 15:37
@cursor cursor Bot mentioned this pull request Nov 30, 2026
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.

3 participants