util-genai | Add MCPInvocation type for MCP span#105
Open
etserend wants to merge 1 commit into
Open
Conversation
71d8cbd to
549a1b6
Compare
shuningc
reviewed
Jun 1, 2026
shuningc
reviewed
Jun 1, 2026
| 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" |
Contributor
There was a problem hiding this comment.
The test locks in the wrong behavior if the name should be "execute_tool get_weather"
shuningc
reviewed
Jun 1, 2026
shuningc
reviewed
Jun 1, 2026
549a1b6 to
80828b2
Compare
Contributor
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. |
Comment on lines
+321
to
+331
| 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) |
Comment on lines
+61
to
+65
| _operation_name = ( | ||
| GenAI.GenAiOperationNameValues.EXECUTE_TOOL.value | ||
| if mcp_method_name == "tools/call" | ||
| else "" | ||
| ) |
Comment on lines
+100
to
+113
| 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 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.