11# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22# SPDX-License-Identifier: Apache-2.0
3+ from dataclasses import dataclass
4+ import json
35from typing import Any , AsyncGenerator , Callable , Collection , Dict , Optional , Tuple , cast
46
5- from wrapt import register_post_import_hook , wrap_function_wrapper
7+ from wrapt import ObjectProxy , register_post_import_hook , wrap_function_wrapper
68
7- from opentelemetry import trace
9+ from opentelemetry import context , trace
810from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
911from opentelemetry .instrumentation .utils import unwrap
1012from opentelemetry .semconv .trace import SpanAttributes
1517from .semconv import (
1618 CLIENT_INITIALIZED ,
1719 MCP_METHOD_NAME ,
20+ MCP_REQUEST_ARGUMENT ,
1821 TOOLS_CALL ,
1922 TOOLS_LIST ,
2023 MCPAttributes ,
@@ -34,7 +37,7 @@ def __init__(self, **kwargs):
3437 self .tracer = trace .get_tracer (__name__ , __version__ , tracer_provider = kwargs .get ("tracer_provider" , None ))
3538
3639 def instrumentation_dependencies (self ) -> Collection [str ]:
37- return ("mcp >= 1.6.0 " ,)
40+ return ("mcp >= 1.8.1 " ,)
3841
3942 def _instrument (self , ** kwargs : Any ) -> None :
4043
@@ -88,14 +91,14 @@ async def async_wrapper():
8891
8992 with self .tracer .start_as_current_span (
9093 MCPSpanNames .SPAN_MCP_CLIENT , kind = trace .SpanKind .CLIENT
91- ) as mcp_client_span :
94+ ) as client_span :
9295
9396 if request :
94- span_ctx = trace .set_span_in_context (mcp_client_span )
97+ span_ctx = trace .set_span_in_context (client_span )
9598 parent_span = {}
9699 self .propagators .inject (carrier = parent_span , context = span_ctx )
97100
98- McpInstrumentor ._set_mcp_client_attributes (mcp_client_span , request )
101+ McpInstrumentor ._set_mcp_client_attributes (client_span , request )
99102
100103 request_as_json ["params" ]["_meta" ].update (parent_span )
101104
@@ -132,6 +135,10 @@ def _set_mcp_client_attributes(span: trace.Span, request: Any) -> None:
132135 span .set_attribute (MCP_METHOD_NAME , TOOLS_LIST )
133136 if isinstance (request , types .CallToolRequest ):
134137 tool_name = request .params .name
138+ tool_arguments = request .params .arguments
139+ if tool_arguments :
140+ for arg_name , arg_val in tool_arguments .items ():
141+ span .set_attribute (f"{ MCP_REQUEST_ARGUMENT } .{ arg_name } " , McpInstrumentor .serialize (arg_val ))
135142 span .update_name (f"{ TOOLS_CALL } { tool_name } " )
136143 span .set_attribute (MCP_METHOD_NAME , TOOLS_CALL )
137144 span .set_attribute (MCPAttributes .MCP_TOOL_NAME , tool_name )
@@ -149,3 +156,13 @@ def _set_mcp_server_attributes(span: trace.Span, request: Any) -> None:
149156 span .update_name (f"{ TOOLS_CALL } { tool_name } " )
150157 span .set_attribute (MCP_METHOD_NAME , TOOLS_CALL )
151158 span .set_attribute (MCPAttributes .MCP_TOOL_NAME , tool_name )
159+
160+
161+ @staticmethod
162+ def serialize (args ):
163+ try :
164+ return json .dumps (args )
165+ except Exception :
166+ return str (args )
167+
168+
0 commit comments