util-genai | Add MCPInvocation type for MCP span#105
Conversation
71d8cbd to
549a1b6
Compare
| def test_span_name_with_tool_name(self) -> None: | ||
| self.handler.mcp("tools/call", tool_name="get_weather").stop() | ||
| self.assertEqual( | ||
| self._get_finished_spans()[0].name, "tools/call get_weather" |
There was a problem hiding this comment.
The test locks in the wrong behavior if the name should be "execute_tool get_weather"
549a1b6 to
80828b2
Compare
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds first-class support for emitting spans/metrics for Model Context Protocol (MCP) operations by introducing an MCPInvocation type and a TelemetryHandler.mcp() factory, along with a dedicated test suite and changelog entry.
Changes:
- Added
TelemetryHandler.mcp()factory method returning a newMCPInvocation. - Implemented
MCPInvocationto set MCP, JSON-RPC, network, tool/prompt, and error attributes and record metrics. - Added comprehensive tests validating span naming, attributes, context-manager behavior, error handling, and sampler attribute capture.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| util/opentelemetry-util-genai/tests/test_handler_mcp.py | New tests covering MCP span creation, attributes, context manager semantics, errors, and sampling attributes. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/invocation.py | Exports MCPInvocation from the public invocation module. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py | Adds TelemetryHandler.mcp() factory to create and start MCP spans. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_mcp_invocation.py | Implements MCPInvocation and MCP-specific semantic attribute handling. |
| util/opentelemetry-util-genai/.changelog/105.added | Changelog entry for MCP support. |
| def should_sample( | ||
| self, | ||
| parent_context, | ||
| trace_id, | ||
| name, | ||
| kind=None, | ||
| attributes=None, | ||
| links=None, | ||
| ): | ||
| captured.update(attributes or {}) | ||
| return SamplingResult(Decision.RECORD_AND_SAMPLE, attributes) |
| _operation_name = ( | ||
| GenAI.GenAiOperationNameValues.EXECUTE_TOOL.value | ||
| if mcp_method_name == "tools/call" | ||
| else "" | ||
| ) |
| def _get_base_attributes(self) -> dict[str, Any]: | ||
| attrs: dict[str, Any] = { | ||
| _MCP_METHOD_NAME: self.mcp_method_name, | ||
| } | ||
| if self._operation_name: | ||
| attrs[GenAI.GEN_AI_OPERATION_NAME] = self._operation_name | ||
|
|
||
| optional: tuple[tuple[str, Any], ...] = ( | ||
| (GenAI.GEN_AI_TOOL_NAME, self.tool_name), | ||
| (GenAI.GEN_AI_PROMPT_NAME, self.prompt_name), | ||
| ) | ||
| optional += self._network_endpoint_attrs() | ||
| attrs.update({k: v for k, v in optional if v is not None}) | ||
| return attrs |
…etry#94) Assisted-by: Claude Opus 4.6
80828b2 to
db21883
Compare
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
Description
Add
MCPInvocationtype andTelemetryHandler.mcp()factory method to support MCP (Model Context Protocol) span tracking in the shared util-genai package.A single
MCPInvocationclass handles both client and server spans via anis_clientflag, per the MCP semantic conventions.Key behaviors:
{mcp.method.name} {target}(target = tool_name or prompt_name)is_clientgen_ai.operation.name = "execute_tool"only fortools/callmcp.method.name,mcp.session.id,mcp.protocol.version,mcp.resource.uri,jsonrpc.request.id, network/server/client attributesgen_ai.tool.call.arguments/gen_ai.tool.call.resultMetrics support is deferred to a follow-up PR.
Fixes #94
Type of change
How has this been tested?
test_handler_mcp.pycovering span creation, attributes, context manager usage, sampling, CLIENT/SERVER kinds, error handling, and content captureChecklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.