-
Notifications
You must be signed in to change notification settings - Fork 808
fix(mcp): add mcp.server parent span wrapper for FastMCP tool calls #3382
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
847d784
feat(mcp): add mcp.server parent span wrapper for FastMCP tool calls
nirga 3126550
fix(mcp): add session-level tracing for FastMCP client operations
nirga 3f0c4eb
fix(mcp): add exception_logger support to McpInstrumentor
nirga 208f570
fix instrumentation init
nina-kollman 5646c34
Merge branch 'feature/mcp-server-span-wrapper' of https://github.com/…
nina-kollman 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
packages/opentelemetry-instrumentation-mcp/tests/test_fastmcp_server_span.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,73 @@ | ||
async def test_fastmcp_server_mcp_parent_span(span_exporter, tracer_provider) -> None: | ||
"""Test that FastMCP tool calls have mcp.server as parent span.""" | ||
from fastmcp import FastMCP, Client | ||
|
||
# Create a simple FastMCP server | ||
server = FastMCP("test-server") | ||
|
||
@server.tool() | ||
async def test_tool(x: int) -> int: | ||
"""A simple test tool.""" | ||
return x * 2 | ||
|
||
# Use in-memory client to connect to the server | ||
async with Client(server) as client: | ||
# Test tool calling | ||
result = await client.call_tool("test_tool", {"x": 5}) | ||
assert len(result) == 1 | ||
assert result[0].text == "10" | ||
|
||
# Get the finished spans | ||
spans = span_exporter.get_finished_spans() | ||
|
||
# Debug: Print span details with parent info | ||
print(f"\nTotal spans: {len(spans)}") | ||
for i, span in enumerate(spans): | ||
parent_id = span.parent.span_id if span.parent else "None" | ||
print(f"Span {i}: name='{span.name}', span_id={span.get_span_context().span_id}, " | ||
f"parent_id={parent_id}, trace_id={span.get_span_context().trace_id}") | ||
|
||
# Look specifically for mcp.server and tool spans | ||
server_mcp_spans = [span for span in spans if span.name == 'mcp.server'] | ||
tool_spans = [span for span in spans if span.name.endswith('.tool')] | ||
|
||
print(f"\nMCP Server spans: {len(server_mcp_spans)}") | ||
print(f"Tool spans: {len(tool_spans)}") | ||
|
||
# Check if we have the expected spans | ||
assert len(server_mcp_spans) >= 1, f"Expected at least 1 mcp.server span, found {len(server_mcp_spans)}" | ||
assert len(tool_spans) >= 1, f"Expected at least 1 tool span, found {len(tool_spans)}" | ||
|
||
# Find server-side spans (should be in same trace) | ||
server_side_spans = [] | ||
for server_span in server_mcp_spans: | ||
for tool_span in tool_spans: | ||
if (server_span.get_span_context().trace_id == tool_span.get_span_context().trace_id and | ||
tool_span.parent and | ||
tool_span.parent.span_id == server_span.get_span_context().span_id): | ||
server_side_spans.append((server_span, tool_span)) | ||
break | ||
|
||
print(f"\nFound {len(server_side_spans)} server-side span pairs") | ||
|
||
# Verify we found at least one proper parent-child relationship | ||
assert len(server_side_spans) >= 1, "Expected at least one mcp.server span to be parent of a tool span" | ||
|
||
# Check the specific parent-child relationship | ||
server_span, tool_span = server_side_spans[0] | ||
assert tool_span.parent.span_id == server_span.get_span_context().span_id, \ | ||
"Tool span should be child of mcp.server span" | ||
assert server_span.get_span_context().trace_id == tool_span.get_span_context().trace_id, \ | ||
"Parent and child should be in same trace" | ||
|
||
# Verify MCP server span attributes | ||
assert server_span.attributes.get('traceloop.span.kind') == 'server', \ | ||
"Server span should have server span kind" | ||
assert server_span.attributes.get('traceloop.entity.name') == 'mcp.server', \ | ||
"Server span should have mcp.server entity name" | ||
|
||
# Verify tool span attributes | ||
assert tool_span.attributes.get('traceloop.span.kind') == 'tool', \ | ||
"Tool span should have tool span kind" | ||
assert tool_span.attributes.get('traceloop.entity.name') == 'test_tool', \ | ||
"Tool span should have correct entity name" |
Oops, something went wrong.
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.