Skip to content

Commit dc2c730

Browse files
committed
add further tool instrumentation
1 parent c97c8df commit dc2c730

File tree

1 file changed

+23
-6
lines changed
  • aws-opentelemetry-distro/src/amazon/opentelemetry/distro/instrumentation/mcp

1 file changed

+23
-6
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/instrumentation/mcp/instrumentation.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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
35
from 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
810
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
911
from opentelemetry.instrumentation.utils import unwrap
1012
from opentelemetry.semconv.trace import SpanAttributes
@@ -15,6 +17,7 @@
1517
from .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

Comments
 (0)