From 2fcbd9cec96f5792431ee5afcac8750f82fc6c2b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 20:41:23 +0000 Subject: [PATCH] Address review comments: use 'unknown' default and derive JSONRPC version from mcp.types Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> --- python/packages/core/agent_framework/_mcp.py | 10 ++++++++-- python/packages/core/agent_framework/observability.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/python/packages/core/agent_framework/_mcp.py b/python/packages/core/agent_framework/_mcp.py index c990b9dd61..1e5607e711 100644 --- a/python/packages/core/agent_framework/_mcp.py +++ b/python/packages/core/agent_framework/_mcp.py @@ -8,6 +8,7 @@ import json import logging import re import sys +import typing from abc import abstractmethod from collections.abc import Callable, Collection, Sequence from contextlib import AsyncExitStack, _AsyncGeneratorContextManager # type: ignore @@ -62,6 +63,11 @@ logger = logging.getLogger(__name__) _MCP_REMOTE_NAME_KEY = "_mcp_remote_name" _MCP_NORMALIZED_NAME_KEY = "_mcp_normalized_name" +# Derive the JSON-RPC protocol version used by the MCP library from its type annotations. +# mcp.types.JSONRPCRequest defines `jsonrpc: Literal["2.0"]`; extracting it here ensures +# we always emit the version the library actually uses rather than a hardcoded magic string. +_JSONRPC_PROTOCOL_VERSION: str = typing.get_args(types.JSONRPCRequest.model_fields["jsonrpc"].annotation)[0] + # region: Helpers LOG_LEVEL_MAPPING: dict[types.LoggingLevel, int] = { @@ -937,7 +943,7 @@ class MCPTool: OtelAttr.MCP_METHOD_NAME: "tools/call", OtelAttr.TOOL_NAME: tool_name, OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION, - OtelAttr.JSONRPC_PROTOCOL_VERSION: "2.0", + OtelAttr.JSONRPC_PROTOCOL_VERSION: _JSONRPC_PROTOCOL_VERSION, } if self._mcp_protocol_version: span_attributes[OtelAttr.MCP_PROTOCOL_VERSION] = self._mcp_protocol_version @@ -1035,7 +1041,7 @@ class MCPTool: span_attributes: dict[str, Any] = { OtelAttr.MCP_METHOD_NAME: "prompts/get", OtelAttr.PROMPT_NAME: prompt_name, - OtelAttr.JSONRPC_PROTOCOL_VERSION: "2.0", + OtelAttr.JSONRPC_PROTOCOL_VERSION: _JSONRPC_PROTOCOL_VERSION, } if self._mcp_protocol_version: span_attributes[OtelAttr.MCP_PROTOCOL_VERSION] = self._mcp_protocol_version diff --git a/python/packages/core/agent_framework/observability.py b/python/packages/core/agent_framework/observability.py index 7e6d2b1ea9..714129e0f1 100644 --- a/python/packages/core/agent_framework/observability.py +++ b/python/packages/core/agent_framework/observability.py @@ -1709,7 +1709,7 @@ def get_mcp_call_span( Returns: A context manager that starts the span as the current span. """ - method_name = attributes.get(OtelAttr.MCP_METHOD_NAME, "mcp") + method_name = attributes.get(OtelAttr.MCP_METHOD_NAME, "unknown") target = attributes.get(OtelAttr.TOOL_NAME) or attributes.get(OtelAttr.PROMPT_NAME) span_name = f"{method_name} {target}" if target else method_name return get_tracer().start_as_current_span(