Repository navigation
feat(core): Enrich MCP SDK spans instead of duplicating them - #26309
sentry-junior[bot] wants to merge 14 commits into
Conversation
size-limit report 📦Compared: e99e87c (PR test merge using develop at 452f91e) Against: 452f91e (develop) · base run #66586 ✅ Baseline matches: this diff contains only this PR's changes.
|
|
One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check. |
Optional E2E testsSome optional checks were not successful. They do not block the merge. 1 not successful, 63 successful. See the run.
Successful checks
|
@modelcontextprotocol/server starts its own OpenTelemetry span for every MCP request and notification once a tracer provider is registered. With the Sentry tracer provider (e.g. @sentry/nextjs, @sentry/sveltekit, @sentry/deno, or enableOpenTelemetrySetup: true) every request ended up with two server spans. The MCP server instrumentation now adds its attributes to the span the MCP SDK starts and only starts its own span when the SDK records nothing. Refs #26078 Co-Authored-By: Francesco Gringl-Novy <francesco.novy@sentry.io> Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Co-Authored-By: Francesco Gringl-Novy <francesco.novy@sentry.io> Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
…n config Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Moves the pnpm exotic-subdeps setting into the e2e app's pnpm-workspace.yaml. Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
c63a8ef to
b4160a2
Compare
…e OpenTelemetry global Whether the MCP SDK's span reaches Sentry is now decided from the span it starts, so the hand-off no longer reads the private OpenTelemetry API registry. When it does not reach Sentry, our own span starts as soon as the SDK opens its scope, so requests the SDK rejects without running a handler still get a span.
Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
The MCP SDK activates its span through the OpenTelemetry context, which does not reach Sentry's scope on runtimes without an OpenTelemetry context manager (e.g. Deno), so handler spans and errors were not nested.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dbcbed0. Configure here.
Without an OpenTelemetry context manager (e.g. Deno), the SDK's span was not active in transport.send, so outgoing notifications still got a second span.
isaacs
left a comment
There was a problem hiding this comment.
With the hand-off, the MCP span is a child of http.server (or of the remote parent from _meta) and not its own segment. Without the hand-off, it is still a forced transaction. So span shape depends on whether a tracer provider is registered, and the same app can change shape after an SDK upgrade. Under span streaming (the default), the MCP insights views query spans by op, so I expect them to still work. I did not verify the product side. The static (transaction) lifecycle is the riskier case, because MCP data then lives inside the http.server transaction.
Suggestion: get a yes from the team that owns the MCP insights views before landing, and say in the CHANGELOG or PR body that MCP spans are no longer segments when the MCP SDK's OTel spans reach Sentry. The PR can't fix this, because forceTransaction can't apply after the SDK starts the span.
The design is sound and as small as it can be.

Why: The next
@modelcontextprotocol/serverrelease starts its own OpenTelemetry span for every MCP request and notification (modelcontextprotocol/typescript-sdk#3115). When the Sentry tracer provider is registered, that span reaches Sentry, so every request gets two nested server spans with the same name. That happens by default in nextjs, sveltekit, deno and vercel-edge, and anywhereenableOpenTelemetrySetup: trueis set. This change adds our attributes to the span the MCP SDK starts instead of starting a second one. Upstream's docs tell Sentry users to remove theMcpServerintegration instead, but that loses tool arguments and results, which upstream deliberately doesn't record.Non-obvious decisions:
Protocol._instrumentation. That field is package-internal, but the integration already reads_registeredToolsand similar. Without the field (v1, v2 ≤ 2.4,openTelemetry: false), nothing changes.spanStart. If it is a Sentry span, we enrich it. If it isn't (a non-Sentry tracer provider, or none at all), we start our own span. We don't inspect the OpenTelemetry API registry.transport.send. Response attributes (tool/prompt results,initializeserver info) are therefore applied from the SDK's end callback, not from send-time correlation.How to review: Start with
upstreamInstrumentation.ts, then the hand-off branches intransport.ts. The changes inspans.tsandcorrelation.tsonly separate building attributes from starting or ending the span, so both paths can use them.Unsure about:
http.serverspan instead of its own segment, becauseforceTransactioncan't be applied after the span is started. Is that fine for the MCP insights views?node-express-mcp-v2 (MCP SDK OpenTelemetry)installs the unreleased SDK from pkg.pr.new, pinned to typescript-sdk919bb8e. The app'spnpm-workspace.yamlsetsblockExoticSubdeps: falsebecause the previews depend on each other by URL. Once upstream releases, the variant should switch to npm and that setting can go.Refs #26078
via Francesco Gringl-Novy, @mydea.
--
View Session [Sentry]