fix(openai-agents): handle MCP list tools spans#4508
fix(openai-agents): handle MCP list tools spans#4508Ayush-Gupta100 wants to merge 8 commits intoopen-telemetry:mainfrom
Conversation
| - Populate instructions and tool definitions from Response obj. | ||
| ([#4196](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4196)) | ||
|
|
||
| ### Fixed |
There was a problem hiding this comment.
No need to add these headers.
| @@ -34,6 +34,7 @@ | |||
| GenerationSpanData, | |||
There was a problem hiding this comment.
Is there any additional attributes we need to handle from these types of operations in _extract_genai_attributes?
|
Also this pr seems to be a duplicate of this? #4387 |
There was a problem hiding this comment.
Pull request overview
Updates the OpenAI Agents v2 span processor to recognize MCP “list tools” spans so they no longer fall back to an unknown GenAI operation name, and adds unit-test coverage plus changelog notes for the new mapping.
Changes:
- Add
MCPListToolsSpanDatadetection to_get_operation_name, mapping it tolist_tools. - Extend test stubs and unit tests to cover the MCP list-tools span-data path.
- Add a changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/span_processor.py | Imports and recognizes MCPListToolsSpanData to map its operation name to list_tools. |
| instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/test_z_span_processor_unit.py | Adds a unit assertion for MCP list-tools operation-name mapping. |
| instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/stubs/agents/tracing/init.py | Adds a stub MCPListToolsSpanData to support unit tests. |
| instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/CHANGELOG.md | Notes the MCP list-tools span handling fix in Unreleased. |
Comments suppressed due to low confidence (1)
instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/span_processor.py:41
- The new
MCPListToolsSpanDataimport is inside atrythat only catchesModuleNotFoundError. If theagents.tracingmodule exists but that symbol is not present (e.g., olderopenai-agentsversions within the supported range), this will raiseImportErrorat import time and break instrumentation. Please make the import resilient to missing span-data classes (e.g., catchImportErroras well and/or import the module thengetattr(..., Any)for optional classes).
try:
from agents.tracing import Span, Trace, TracingProcessor
from agents.tracing.span_data import (
AgentSpanData,
FunctionSpanData,
GenerationSpanData,
GuardrailSpanData,
HandoffSpanData,
MCPListToolsSpanData,
ResponseSpanData,
SpeechSpanData,
TranscriptionSpanData,
)
| return GenAIOperationName.HANDOFF | ||
| if _is_instance_of(span_data, MCPListToolsSpanData): | ||
| return "list_tools" | ||
| return "unknown" |
There was a problem hiding this comment.
_get_operation_name now recognizes MCPListToolsSpanData, but _extract_genai_attributes has no corresponding branch for this span type. As a result, metrics (and any later logic relying on the extracted attribute dict) will see no gen_ai.operation.name for MCP list-tools spans, even though it was set at span start. Add MCP list-tools handling in the attribute extraction path (at minimum emitting gen_ai.operation.name, and ideally any relevant MCP/tool-definition attributes) so end-of-span enrichment and metrics are consistent.
| mcp_data = MCPListToolsSpanData() | ||
| assert processor._get_operation_name(mcp_data) == "list_tools" | ||
|
|
There was a problem hiding this comment.
This unit test only asserts _get_operation_name returns "list_tools". Given the span processor enriches spans at start/end and records metrics based on the extracted attribute dict, it would be good to add a regression assertion that a processed MCPListToolsSpanData span actually ends up with the expected gen_ai.operation.name attribute (and correct span name, if applicable) after on_span_start/on_span_end.
|
|
||
| ### Fixed | ||
|
|
||
| - `opentelemetry-instrumentation-openai-agents`: Handle MCP list tools spans correctly. |
There was a problem hiding this comment.
The changelog entry names the package as opentelemetry-instrumentation-openai-agents, but this changelog is for opentelemetry-instrumentation-openai-agents-v2 (see pyproject.toml). Please update the package name in this entry to match the actual distribution so users can find the fix in the right package.
| - `opentelemetry-instrumentation-openai-agents`: Handle MCP list tools spans correctly. | |
| - `opentelemetry-instrumentation-openai-agents-v2`: Handle MCP list tools spans correctly. |
| ### Fixed | ||
|
|
||
| - `opentelemetry-instrumentation-openai-agents`: Handle MCP list tools spans correctly. | ||
| ([#4508](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4508)) |
There was a problem hiding this comment.
The PR description’s change-type checklist marks this as a breaking change and says a documentation update is required, but the code changes look like a non-breaking bug fix and the only doc-facing update here is the changelog entry. Please reconcile the PR description with the actual scope/impact of the change.
the bug is same but my pr is different as PR #4387 implements MCP semantics: sets |
- Add GenAIOperationName.LIST_TOOLS constant - Implement _get_attributes_from_mcp_list_tools_span_data to extract operation name, tool definitions, and output type - Update _extract_genai_attributes dispatch for MCPListToolsSpanData - Add test coverage for MCP list tools attribute extraction - Update CHANGELOG Fixes open-telemetry#4197
|
@Ayush-Gupta100 It seems like this pr is a subset of #4387. Seeing as they opened the PR first, it would make sense to work with that other one. Perhaps you can review it and leave comments if it addresses your concerns? |
Description
Fixes #4197
This change teaches the OpenAI Agents v2 span processor to recognize
MCPListToolsSpanDataand name the spanlist_toolsinstead of falling back tounknown. It also adds test coverage for the MCP list-tools span-data path so the behavior stays covered.Motivation and context
When an agent uses an MCP server, the SDK emits
MCPListToolsSpanDatawhile fetching the available tools. Before this change, that span was not handled by the processor, so the resulting telemetry had anunknownoperation name and no useful MCP-specific behavior. This fix makes the emitted span consistent with the rest of the GenAI operation naming.Dependencies
No new dependencies are required for this change.
Type of change
How Has This Been Tested?
python -m pytest instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/test_z_span_processor_unit.py::test_operation_and_span_naming -vpython -m pytest instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/test_z_span_processor_unit.py -vThese tests verify the new MCP operation mapping and confirm the full unit test file still passes.
Does This PR Require a Core Repo Change?
Checklist: