fix(core): set span.status to error when MCP tool returns JSON-RPC error response#20082
Open
fix(core): set span.status to error when MCP tool returns JSON-RPC error response#20082
Conversation
…ror response When a tool handler threw an error, completeSpanWithResults() was ending the span without setting its status to error. This caused all MCP tool spans to appear with span.status=ok in Sentry, breaking the failure_rate() metric in the MCP insights dashboard. The fix passes hasError=true to completeSpanWithResults() when the outgoing JSON-RPC response contains an error object, setting the span status to internal_error directly on the stored span (bypassing getActiveSpan() which doesn't return the right span at send() time). Co-Authored-By: claude-sonnet-4-6 <noreply@anthropic.com>
Contributor
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Internal Changes 🔧Core
Other
🤖 This preview updates automatically when you update the PR. |
Contributor
size-limit report 📦
|
Contributor
node-overhead report 🧳Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.
|
Adds an `always-error` tool to the MCP e2e test apps and a test step that verifies the resulting transaction has span.status=internal_error when a tool throws. This covers the fix in the span status correlation path end-to-end, complementing the existing unit tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
JPeer264
approved these changes
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an MCP tool returns a JSON-RPC error response (i.e. the response has an
errorfield instead ofresult), the span was being ended withstatus=ok. This meantfailure_rate()read 0% in the MCP insights dashboard even when tools were consistently failing.Root cause
completeSpanWithResultsalways ended the span without checking whether the response was an error. ThecaptureErrorpath usesgetActiveSpan(), which returnsnulloutside awithActiveSpan()context — so errors were never reflected in span status.Fix
Pass
!!message.erroras ahasErrorflag tocompleteSpanWithResults. Whentrue, setSPAN_STATUS_ERRORwithmessage: 'internal_error'directly on the stored span before ending it.Changes
correlation.ts—completeSpanWithResultsaccepts optionalhasErrorparam; sets error status when truetransport.ts— passes!!message.errorwhen completing spans on outgoing sendtransportInstrumentation.test.ts— two new tests: error response sets error status; success response does notCloses #20083