@@ -34,10 +34,10 @@ def __init__(self, **kwargs):
34
34
self .tracer = trace .get_tracer (__name__ , __version__ , tracer_provider = kwargs .get ("tracer_provider" , None ))
35
35
36
36
def instrumentation_dependencies (self ) -> Collection [str ]:
37
- return "mcp >= 1.6.0"
37
+ return ( "mcp >= 1.6.0" ,)
38
38
39
39
def _instrument (self , ** kwargs : Any ) -> None :
40
-
40
+
41
41
register_post_import_hook (
42
42
lambda _ : wrap_function_wrapper (
43
43
"mcp.shared.session" ,
@@ -63,6 +63,7 @@ def _wrap_send_request(
63
63
self , wrapped : Callable , instance : Any , args : Tuple [Any , ...], kwargs : Dict [str , Any ]
64
64
) -> Callable :
65
65
import mcp .types as types
66
+
66
67
67
68
"""
68
69
Patches BaseSession.send_request which is responsible for sending requests from the client to the MCP server.
@@ -103,23 +104,16 @@ async def async_wrapper():
103
104
new_args = (modified_request ,) + args [1 :]
104
105
105
106
return await wrapped (* new_args , ** kwargs )
107
+ return async_wrapper ()
106
108
107
- return async_wrapper
108
-
109
- # Handle Request Wrapper
110
109
async def _wrap_handle_request (
111
110
self , wrapped : Callable , instance : Any , args : Tuple [Any , ...], kwargs : Dict [str , Any ]
112
111
) -> Any :
113
- """
114
- Patches Server._handle_request which is responsible for processing requests on the MCP server.
115
- This patched MCP server intercepts incoming requests to extract tracing context from
116
- the request's params._meta field and creates server-side spans linked to the client spans.
117
- """
118
112
req = args [1 ] if len (args ) > 1 else None
119
113
carrier = {}
120
114
121
115
if req and hasattr (req , "params" ) and req .params and hasattr (req .params , "meta" ) and req .params .meta :
122
- carrier = req .params .meta .__dict__
116
+ carrier = req .params .meta .model_dump ()
123
117
124
118
parent_ctx = self .propagators .extract (carrier = carrier )
125
119
@@ -128,8 +122,7 @@ async def _wrap_handle_request(
128
122
MCPSpanNames .SPAN_MCP_SERVER , kind = trace .SpanKind .SERVER , context = parent_ctx
129
123
) as mcp_server_span :
130
124
self ._set_mcp_server_attributes (mcp_server_span , req )
131
-
132
- return await wrapped (* args , ** kwargs )
125
+ return await wrapped (* args , ** kwargs )
133
126
134
127
@staticmethod
135
128
def _set_mcp_client_attributes (span : trace .Span , request : Any ) -> None :
@@ -149,9 +142,9 @@ def _set_mcp_client_attributes(span: trace.Span, request: Any) -> None:
149
142
def _set_mcp_server_attributes (span : trace .Span , request : Any ) -> None :
150
143
import mcp .types as types # pylint: disable=import-outside-toplevel,consider-using-from-import
151
144
152
- if isinstance (span , types .ListToolsRequest ):
145
+ if isinstance (request , types .ListToolsRequest ):
153
146
span .set_attribute (MCP_METHOD_NAME , TOOLS_LIST )
154
- if isinstance (span , types .CallToolRequest ):
147
+ if isinstance (request , types .CallToolRequest ):
155
148
tool_name = request .params .name
156
149
span .update_name (f"{ TOOLS_CALL } { tool_name } " )
157
150
span .set_attribute (MCP_METHOD_NAME , TOOLS_CALL )
0 commit comments