mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Address review comments: use 'unknown' default and derive JSONRPC version from mcp.types
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user