Skip to content

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

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

RealAdhi wants to merge 1 commit 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 McpProtocolException messages are kept (the JSON-RPC error message the peer already sees); 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.

Open question for reviewers: a plain McpException still sends its message over the wire, but this change drops it from the span to match the issue's wording. Happy to keep it if you'd prefer.

Tests

  • Added Session_ErrorStatusDescription_DoesNotContainToolContentOrExceptionMessage to DiagnosticTests.cs (a tool returning an isError result plus a handler that throws InvalidOperationException). 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 assistance: Claude Code drafted the fix and tests; I reviewed every line and ran the suite locally.

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

1 participant