Skip to content

feat(mcp): Migrate to span first#6131

Open
ericapisani wants to merge 6 commits intomasterfrom
ep/py-2340-mcp-span-first-84l
Open

feat(mcp): Migrate to span first#6131
ericapisani wants to merge 6 commits intomasterfrom
ep/py-2340-mcp-span-first-84l

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented Apr 23, 2026

Add span streaming support to the MCP integration.

Refs PY-2340
Fixes #6038

Add span streaming support to the MCP integration. When span streaming
is enabled, start spans via sentry_sdk.traces.start_span and set data
via set_attribute; otherwise fall back to the existing transaction-based
flow and set_data.

Introduce _set_span_data_attribute in sentry_sdk.ai.utils to branch on
StreamedSpan vs Span so handlers can write attributes uniformly in both
modes.

Refs PY-2340
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Apr 23, 2026

@ericapisani ericapisani marked this pull request as ready for review April 23, 2026 19:06
@ericapisani ericapisani requested a review from a team as a code owner April 23, 2026 19:06
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 23, 2026

Codecov Results 📊

5 passed | Total: 5 | Pass Rate: 100% | Execution Time: 1.09s

All tests are passing successfully.

❌ Patch coverage is 12.50%. Project has 17018 uncovered lines.

Files with missing lines (1)
File Patch % Lines
mcp.py 5.86% ⚠️ 257 Missing

Generated by Codecov Action

Comment thread sentry_sdk/ai/utils.py
Comment thread tests/integrations/mcp/test_mcp.py


@pytest.mark.asyncio
async def test_stdio_transport_detection(sentry_init, capture_events, stdio):
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.

Parts of this diff look too red 😅
What happened to tests such as test_stdio_transport_detection()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was folded into test_tool_handler_stdio. There were a few assertions that were unique to this test that now appear there (taken from the red below):

assert span["data"][SPANDATA.MCP_TRANSPORT] == "stdio"
assert span["data"][SPANDATA.NETWORK_TRANSPORT] == "pipe"
# No session ID for stdio transport
assert SPANDATA.MCP_SESSION_ID not in span["data"]

You can see them in the other test here

ericapisani and others added 2 commits April 24, 2026 09:49
The non-streaming branches of test_prompt_handler_with_error and
test_resource_handler_with_error only verified error-event fields,
leaving span status and transaction shape unchecked. Mirror the
streaming-branch assertions and the tool-handler test pattern so
regressions in classic-mode error handling for prompts and resources
are caught.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

Migrate mcp to span first

2 participants