-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(mcp): never let instrumentation errors break the traced MCP call #4464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chengwudi1
wants to merge
2
commits into
traceloop:main
Choose a base branch
from
chengwudi1:fix/mcp-tracing-errors-safe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−24
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
99 changes: 99 additions & 0 deletions
99
packages/opentelemetry-instrumentation-mcp/tests/test_send_request_safety.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Tests for #4463: instrumentation failures must never change the outcome of | ||
| the traced MCP call. BaseSession.send_request returns the RPC result, so a | ||
| wrapper that swallows an instrumentation error and returns None replaces a | ||
| real result (e.g. CallToolResult) with None and crashes the caller.""" | ||
|
|
||
| import pytest | ||
| from opentelemetry.trace.status import StatusCode | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_tool_call_outside_active_span_returns_real_result( | ||
| span_exporter, tracer_provider | ||
| ): | ||
| """ | ||
| #4463: when send_request runs outside any span (first call after | ||
| instrumenting, a detached task, a non-recording context), the | ||
| TraceContextTextMapPropagator writes no traceparent header. The old | ||
| unconditional `carrier["traceparent"]` read then raised KeyError, dont_throw | ||
| swallowed it and returned None, and ClientSession.call_tool crashed with | ||
| AttributeError: 'NoneType' object has no attribute 'isError'. | ||
| """ | ||
| from fastmcp import Client, FastMCP | ||
| from opentelemetry import context | ||
|
|
||
| server = FastMCP("no-active-span") | ||
|
|
||
| @server.tool() | ||
| async def ping() -> str: | ||
| return "pong" | ||
|
|
||
| async with Client(server) as client: | ||
| # Client.__aenter__ makes the session span current. Push an empty | ||
| # context so the tool call happens outside any span, as reported in | ||
| # production for #4463. The meta dict makes the request carry an MCP | ||
| # Meta object, which is what forces the trace-context injection branch | ||
| # in patch_mcp_client to run. | ||
| token = context.attach(context.Context()) | ||
| try: | ||
| result = await client.call_tool("ping", {}, meta={"clientID": "x"}) | ||
| finally: | ||
| context.detach(token) | ||
|
|
||
| assert result.content[0].text == "pong" | ||
|
|
||
| # The call was still traced as a root span, but with no errors. | ||
| ping_spans = [ | ||
| s for s in span_exporter.get_finished_spans() if s.name == "ping.tool" | ||
| ] | ||
| assert ping_spans, "expected a ping.tool span for the traced call" | ||
| assert all( | ||
| s.status.status_code != StatusCode.ERROR for s in ping_spans | ||
| ), "tracing failure was recorded as a span error" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_error_result_with_non_text_content_block_is_returned( | ||
| span_exporter, tracer_provider | ||
| ): | ||
| """ | ||
| #4463: post-call span decoration read result.content[0].text unguarded. | ||
| Error results can carry non-text content blocks (image, audio, embedded | ||
| resource), which have no .text attribute. The AttributeError was raised | ||
| after the RPC had already succeeded, swallowed by dont_throw, and the real | ||
| result was thrown away in favour of None. | ||
| """ | ||
| from mcp.types import CallToolResult, ImageContent | ||
|
|
||
| from opentelemetry.instrumentation.mcp.instrumentation import McpInstrumentor | ||
|
|
||
| instrumentor = McpInstrumentor() | ||
| tracer = tracer_provider.get_tracer("test-send-request-safety") | ||
| span = tracer.start_span("image-error.tool") | ||
|
|
||
| image_error = CallToolResult( | ||
| content=[ | ||
| ImageContent( | ||
| type="image", data="iVBORw0KGgo=", mimeType="image/png" | ||
| ) | ||
| ], | ||
| isError=True, | ||
| ) | ||
|
|
||
| async def fake_wrapped(*args, **kwargs): | ||
| return image_error | ||
|
|
||
| result = await instrumentor._execute_and_handle_result( | ||
| span, "tools/call", [], {}, fake_wrapped, clean_output=True | ||
| ) | ||
| span.end() | ||
|
|
||
| assert result is image_error, "the real RPC result must be returned" | ||
|
|
||
| # The error is still recorded on the span (error.type + ERROR status). | ||
| error_spans = [ | ||
| s for s in span_exporter.get_finished_spans() if s.name == "image-error.tool" | ||
| ] | ||
| assert len(error_spans) == 1 | ||
| assert error_spans[0].status.status_code == StatusCode.ERROR | ||
| assert error_spans[0].attributes.get("error.type") == "tool_error" |
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.
Uh oh!
There was an error while loading. Please reload this page.