Skip to content

Don't leak tool content or exception messages into span status - #2065

Open
RealAdhi wants to merge 3 commits into
modelcontextprotocol:mainfrom
RealAdhi:fix/span-status-no-content-leak
Open

RealAdhi wants to merge 3 commits into
modelcontextprotocol:mainfrom
RealAdhi:fix/span-status-no-content-leak

Conversation

@RealAdhi

@RealAdhi RealAdhi commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #2060

Problem

On the server, a normal exception reaches the client only as "An error occurred.", but the span was recording the real exception message. So the trace leaked more than the client ever saw.

In McpSessionHandler:

  • AddResponseTags set the span status description to content.ToJsonString() for any result with isError: true, putting tool result content into traces.
  • AddExceptionTags set it to e.Message for every exception.

Change

  • isError results: SetStatus(ActivityStatusCode.Error) with no description. error.type=tool_error already marks the failure.
  • Exceptions: only McpException messages (including McpProtocolException) are kept, since the server sends that message as the JSON-RPC error message the peer already sees, per the semconv guidance that the status description SHOULD match JSONRPCError.message. Other exception messages are dropped. error.type and status-code tags are unchanged.

Since both helpers are shared, client spans for isError results also lose their description. Client-side remote errors are McpProtocolExceptions, so client spans still keep a description for those.

Tests

  • Added Session_ErrorStatusDescription_DoesNotContainToolContentOrExceptionMessage to DiagnosticTests.cs (a tool returning an isError result, a handler that throws InvalidOperationException, and one that throws McpException). It fails before the change (Actual: "[{\"type\":\"text\",\"text\":\"secret tool content"···, then "secret exception message") and passes after.
  • dotnet build: 0 warnings, 0 errors. ModelContextProtocol.Tests passes on net10.0, net9.0, net8.0 and net472. ServerConformanceTests in AspNetCore.Tests fail identically with and without this change on my machine (pre-existing, environment-related).

AI disclosure: I used an AI coding assistant to help write this change; I reviewed every line, ran the suite locally, and can explain it.

Local HTTP transport failures

HTTP client transports throw HttpRequestException with the response body in the message when a POST fails (the SSE transport includes the full body; Streamable HTTP truncates it). That exception reaches AddExceptionTags unwrapped. It is not an McpException, so the span now gets no status description, and error.type is System.Net.Http.HttpRequestException. This path is covered by Session_HttpTransportFailure_DoesNotCopyResponseBodyIntoClientSpanStatus.


Generated by Claude Code

@girishkvs

Copy link
Copy Markdown
Contributor

@RealAdhi On the open question, I think the span should keep the plain McpException message. The MCP conventions say "If the span status is set to ERROR, the status description SHOULD match the JSONRPCError.message if the message is available" (mcp.md L372), and the server sends mcpException.Message as that message (McpSessionHandler.cs L312-L316), so the client already sees it.

@RealAdhi

RealAdhi commented Dec 3, 2026

Copy link
Copy Markdown
Author

@girishkvs Good catch. I'd only looked at McpProtocolException, but you're right that the server's error switch also sends a plain McpException's message as the JSON-RPC error message, so the client sees it anyway. I've switched the check to McpException (which covers the protocol exception too) and added a test case that throws one and checks the span keeps its message. Anything else, like an InvalidOperationException, still gets no description.

@girishkvs girishkvs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@sivaraman-krishnan sivaraman-krishnan 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.

Thanks, @RealAdhi! I checked that the status description was the only place the SDK put these messages on spans: AddExceptionTags doesn't record exception events or exception.* tags, so with this change only McpException messages still reach the span, as intended.

One more client-side effect that might be worth adding to the description: local HTTP transport failures. When a POST fails, the HTTP transports throw an HttpRequestException whose message includes the response body when available. Streamable HTTP does this unless it turns the response into an McpProtocolException, and truncates the body via CreateHttpRequestExceptionWithBodyAsync. SSE does it for any non-success status and includes the full body via CreateHttpRequestException. That exception reaches AddExceptionTags unwrapped through SendRequestAsync, so before this change the response body could end up in the client span's status description. Now there is no description and error.type is System.Net.Http.HttpRequestException. That seems like the right outcome for the same reason. The PR description doesn't mention HTTP transport failures, though, and the test doesn't cover this path.

…t span status

HTTP client transports throw HttpRequestException with the response body in the
message when a POST fails. Add a test that a failing send surfaces as an error
span with no status description and error.type set to the exception type.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AkKv99L5HA4BBrocAW75Qe

This branch has not been deployed

No deployments
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.

Span status contains tool result content and exception messages

4 participants