Skip to content

fix(server): Mark calls to unregistered tools as failed spans - #1952

Merged
dcramer merged 2 commits into
mainfrom
fix/mcp-unknown-tool-spans
Nov 4, 2026
Merged

dcramer merged 2 commits into
mainfrom
fix/mcp-unknown-tool-spans

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Since #1868 turned on EXPOSE_ALL_TOOLS, search_sentry_tools and execute_sentry_tool are no longer registered, but clients still call them (~200/hour each on the hosted server). The MCP SDK rejects these calls with JSON-RPC -32602 Tool … not found. Sentry's MCP instrumentation treats -32602 as a client error and leaves the tools/call span status unset, so these failures were recorded as ok 0ms spans and didn't show up in tool failure rates. They also lacked app.transport, because we set it only inside tool handlers, and those never run for unknown names.

This wraps the SDK's tools/call handler. When a call names a tool this server did not register, the wrapper marks the active span as failed (error.type: unknown_tool, status not_found) and adds the usual request attributes (app.transport, app.client.family, constraints, skills). The removed gateway tools get a clear message:

Tool execute_sentry_tool was removed from the Sentry MCP server. Call Sentry tools directly by name. If they are missing from your tool list, reconnect the Sentry MCP server to refresh it.

Other unknown names keep the SDK's error. The response stays a -32602 JSON-RPC error, which the MCP spec prescribes for unknown tools. No Sentry issue is created: this is a client mistake, and neither our code nor the SDK captures -32602.

The SDK has no hook for unknown tool names, so the wrapper uses the Protocol's private _requestHandlers map. If an SDK upgrade changes that map, the wrapper does nothing (it checks instanceof Map and that a handler exists) and the new span test fails.

Evidence

sentry/mcp-server spans, 2026-11-03T20:15Z–2026-11-04T17:15Z:

mcp.tool.name span.status rpc.response.status_code app.transport count
execute_sentry_tool ok -32602 – 4,853
search_sentry_tools ok -32602 – 4,047

All of them have network.transport: tcp and an HTTP mcp.transport (WebStandardStreamableHTTPServerTransport or PerRequestHTTPServerTransport), and their parent http.server POST /mcp/… spans carry app.transport: http. They come from hosted HTTP clients across many client families, not from stdio. The missing app.transport only means our handler never ran. The cli project has no mcp.* spans in the last 30 days. The same gap also hid other unknown names, mostly the deprecated search_events (~1.2k calls in the same window).

Local repro with @sentry/node 11.6.0 and the real wrapMcpServerWithSentry:

  • Before: the tools/call execute_sentry_tool span is ok with rpc.response.status_code: -32602.
  • After: it is error with error.type: unknown_tool and app.transport: http, and no error events are sent.

telem/unknown-tool.test.ts covers the same path. It uses a real ServerRuntimeClient and the real MCP instrumentation under an AsyncLocalStorage context strategy, and asserts the finished span's status and error.type. It fails without the server.ts change.

Deploy note

This adds roughly 400–500 failed tools/call spans per hour, which were previously recorded as ok. Most of them (~420/hour) are the removed gateway names, and the rest are other unknown names such as search_events. Tool failure-rate alerts and dashboards for mcp-server may jump after deploy. These are failures that were already happening and are now visible, not a regression.

SDK follow-up

The root behavior is in getsentry/sentry-javascript. completeSpanWithResults (packages/core/src/integrations/mcp-server/correlation.ts) sets an error status only when isJsonRpcServerError (errorCapture.ts) is true, and that check excludes -32602. Not capturing an exception for client errors makes sense, but the span should probably still get an error status, and error.type should be the JSON-RPC code, so tools/call failure rates include protocol-level client errors.

Out of scope

  • Spans for tool calls that fail SDK input validation (an SDK isError result) also lack app.transport, for the same reason: our handler never runs.
  • Whether deprecated names like search_events should get a replacement hint is a separate product decision.

Review

Independent review: ready, no must-fix.

  • The reviewer confirmed the root cause and verified it end to end with a real ServerRuntimeClient, the real wrapMcpServerWithSentry and an async context. Main records the call as ok without app.transport. This branch records it as error with error.type=unknown_tool and app.transport=http, and sends 0 error events. Registered tools are unaffected.
  • All transports dispatch through _requestHandlers.
  • Applied optional items 1–4:
    • The span test now uses real instrumentation instead of a mocked getActiveSpan.
    • The wrapper checks instanceof Map and that a handler exists before wrapping.
    • The not-found test runs regardless of EXPOSE_ALL_TOOLS.
    • The span attributes are computed once.

pnpm run tsc, pnpm run lint, and the mcp-core (2,044 tests) and mcp-cloudflare (447 tests) suites pass locally. In the sandbox, some packages/cli filesystem-permission tests fail on clean main too.

via @dcramer.

--

View Session [Sentry]

sentry-junior Bot and others added 2 commits November 4, 2026 18:24
Since EXPOSE_ALL_TOOLS stopped registering search_sentry_tools and
execute_sentry_tool, clients that still call them get a -32602
"Tool not found" JSON-RPC error. Sentry's MCP instrumentation treats
-32602 as a client error and leaves the tools/call span status unset,
and our request attributes are only set inside tool handlers, so these
calls showed up as successful 0ms spans without app.transport.

Wrap the SDK's tools/call handler so a call to a name this server did
not register sets error.type: unknown_tool, an error span status, and
the request attributes before the SDK rejects it. The removed gateway
tools get a message that tells the client to call tools directly. No
Sentry issue is created for these client mistakes.

Co-Authored-By: David Cramer <[email protected]>
Replace the mocked getActiveSpan assertions with a test that runs the real
wrapMcpServerWithSentry under an AsyncLocalStorage context strategy and
checks the finished span's status and error.type. Keep the not-found test
outside the EXPOSE_ALL_TOOLS block, guard the private handler map with
instanceof Map, and compute the tool-call span attributes once.

Co-Authored-By: David Cramer <[email protected]>
@vercel

vercel Bot commented Nov 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 4, 2026 6:48pm UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 4, 2026

This branch was previously deployed

1 inactive deployment
Preview — dc6b50e9 Deployed Nov 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant