FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Stop flagging snake_case is_error results as tool errors in OTel span by Kludex · Pull Request #2971 · modelcontextprotocol/python-sdk · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion src/mcp/server/_otel.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
Expand Up @@ -59,8 +59,12 @@ async def __call__(self, ctx: ServerRequestContext[Any, Any], call_next: CallNex
span.set_status(StatusCode.ERROR, str(e))
raise
if ctx.method == "tools/call":
# Tool errors are detected pre-serialization, so only shapes that reach the wire as an error
# count: the model, or the camelCase alias (`is_error` is dropped by the alias-only wire
# validation). A raw-dict `isError` is matched as a literal bool only - non-bool coercible
# values (1, "true") would serialize to an error but are rare enough to leave undetected.
match result:
case CallToolResult(is_error=True) | {"isError": True} | {"is_error": True}:
case CallToolResult(is_error=True) | {"isError": True}:

cubic-dev-ai Bot Jun 25, 2026
edited
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

P2: Tool-error span detection now misses coercible isError inputs that serialize to true, so telemetry can still diverge from the actual wire error result.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/mcp/server/_otel.py, line 64:

<comment>Tool-error span detection now misses coercible `isError` inputs that serialize to `true`, so telemetry can still diverge from the actual wire error result.</comment>

<file context>
@@ -59,8 +59,9 @@ async def __call__(self, ctx: ServerRequestContext[Any, Any], call_next: CallNex
+                # Only shapes that survive wire serialization (alias-only) are real tool errors.
                 match result:
-                    case CallToolResult(is_error=True) | {"isError": True} | {"is_error": True}:
+                    case CallToolResult(is_error=True) | {"isError": True}:
                         span.set_attribute("error.type", "tool_error")
                         span.set_status(StatusCode.ERROR)
</file context>

span.set_attribute("error.type", "tool_error")
span.set_status(StatusCode.ERROR)
case _:
Expand Down
11 changes: 7 additions & 4 deletions tests/server/test_otel.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
Expand Up @@ -92,19 +92,22 @@ async def err_tool(ctx: Ctx, params: CallToolRequestParams) -> CallToolResult:


@pytest.mark.anyio
async def test_tool_error_snake_case_dict_result_sets_error_type(server: SrvT, spans: SpanCapture):
async def test_snake_case_dict_result_is_not_a_tool_error(server: SrvT, spans: SpanCapture):
# `is_error` is alias-only on the wire, so serialization drops it; the result reaches the
# client as a success and the span must not contradict that.
async def err_tool(ctx: Ctx, params: CallToolRequestParams) -> dict[str, Any]:
return {"content": [], "is_error": True}

server.add_request_handler("tools/call", CallToolRequestParams, err_tool)
server.middleware.append(OpenTelemetryMiddleware())
async with connected_runner(server) as (client, _):
spans.clear()
await client.send_raw_request("tools/call", {"name": "mytool", "arguments": {}})
result = await client.send_raw_request("tools/call", {"name": "mytool", "arguments": {}})
assert result == {"content": []}
[span] = [s for s in spans.finished() if s.kind == SpanKind.SERVER]
assert span.attributes is not None
assert span.attributes["error.type"] == "tool_error"
assert span.status.status_code == StatusCode.ERROR
assert "error.type" not in span.attributes
assert span.status.status_code == StatusCode.UNSET


@pytest.mark.anyio
Expand Down
Loading

Back | FazBrowse Home | New Git URL