|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +from logfire.testing import CaptureLogfire |
4 | 5 |
|
5 | | -from mcp.shared._otel import otel_span |
| 6 | +from mcp.client.client import Client |
| 7 | +from mcp.server.mcpserver import MCPServer |
6 | 8 |
|
7 | 9 | pytestmark = pytest.mark.anyio |
8 | 10 |
|
9 | 11 |
|
10 | | -def test_otel_span_creates_span(): |
11 | | - with otel_span("test.span", kind="CLIENT", attributes={"key": "value"}) as span: |
12 | | - assert span is not None |
| 12 | +async def test_client_and_server_spans(capfire: CaptureLogfire): |
| 13 | + """Verify that calling a tool produces client and server spans with correct attributes.""" |
| 14 | + server = MCPServer("test") |
| 15 | + |
| 16 | + @server.tool() |
| 17 | + def greet(name: str) -> str: |
| 18 | + """Greet someone.""" |
| 19 | + return f"Hello, {name}!" |
| 20 | + |
| 21 | + async with Client(server) as client: |
| 22 | + result = await client.call_tool("greet", {"name": "World"}) |
| 23 | + |
| 24 | + assert result.content[0].text == "Hello, World!" # type: ignore[union-attr] |
| 25 | + |
| 26 | + spans = capfire.exporter.exported_spans_as_dict() |
| 27 | + span_names = {s["name"] for s in spans} |
| 28 | + |
| 29 | + assert "MCP tools/call greet" in span_names |
| 30 | + assert "MCP handle tools/call greet" in span_names |
| 31 | + |
| 32 | + client_span = next(s for s in spans if s["name"] == "MCP tools/call greet") |
| 33 | + server_span = next(s for s in spans if s["name"] == "MCP handle tools/call greet") |
| 34 | + |
| 35 | + assert client_span["attributes"]["mcp.method.name"] == "tools/call" |
| 36 | + assert server_span["attributes"]["mcp.method.name"] == "tools/call" |
| 37 | + |
| 38 | + # Server span should be in the same trace as the client span (context propagation). |
| 39 | + assert server_span["context"]["trace_id"] == client_span["context"]["trace_id"] |
0 commit comments