Skip to content

Commit 7795f34

Browse files
committed
Use logfire[testing] for OTel test assertions
1 parent e1ebbc7 commit 7795f34

File tree

3 files changed

+236
-7
lines changed

3 files changed

+236
-7
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dev = [
7272
"coverage[toml]>=7.10.7,<=7.13",
7373
"pillow>=12.0",
7474
"strict-no-cover",
75+
"logfire[testing]",
7576
]
7677
docs = [
7778
"mkdocs>=1.6.1",

tests/shared/test_otel.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
from __future__ import annotations
22

33
import pytest
4+
from logfire.testing import CaptureLogfire
45

5-
from mcp.shared._otel import otel_span
6+
from mcp.client.client import Client
7+
from mcp.server.mcpserver import MCPServer
68

79
pytestmark = pytest.mark.anyio
810

911

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

Comments
 (0)