@@ -239,84 +239,73 @@ def test_mcp_client_without_structured_content():
239239
240240
241241def test_call_tool_sync_with_meta ():
242- """Test that call_tool_sync works correctly when meta is provided ."""
242+ """Test that call_tool_sync forwards meta to the MCP server ."""
243243 stdio_mcp_client = MCPClient (
244244 lambda : stdio_client (StdioServerParameters (command = "python" , args = ["tests_integ/mcp/echo_server.py" ]))
245245 )
246246
247247 with stdio_mcp_client :
248248 result = stdio_mcp_client .call_tool_sync (
249249 tool_use_id = "test-meta-sync" ,
250- name = "echo " ,
251- arguments = {"to_echo" : "META_TEST" },
250+ name = "echo_meta " ,
251+ arguments = {},
252252 meta = {"com.example/request_id" : "abc-123" },
253253 )
254254
255255 assert result ["status" ] == "success"
256- assert result ["content" ] == [{"text" : "META_TEST" }]
256+ received_meta = json .loads (result ["content" ][0 ]["text" ])
257+ assert received_meta ["com.example/request_id" ] == "abc-123"
257258
258259
259260@pytest .mark .asyncio
260261async def test_call_tool_async_with_meta ():
261- """Test that call_tool_async works correctly when meta is provided ."""
262+ """Test that call_tool_async forwards meta to the MCP server ."""
262263 stdio_mcp_client = MCPClient (
263264 lambda : stdio_client (StdioServerParameters (command = "python" , args = ["tests_integ/mcp/echo_server.py" ]))
264265 )
265266
266267 with stdio_mcp_client :
267268 result = await stdio_mcp_client .call_tool_async (
268269 tool_use_id = "test-meta-async" ,
269- name = "echo " ,
270- arguments = {"to_echo" : "META_ASYNC_TEST" },
270+ name = "echo_meta " ,
271+ arguments = {},
271272 meta = {"com.example/request_id" : "def-456" },
272273 )
273274
274275 assert result ["status" ] == "success"
275- assert result ["content" ] == [{"text" : "META_ASYNC_TEST" }]
276+ received_meta = json .loads (result ["content" ][0 ]["text" ])
277+ assert received_meta ["com.example/request_id" ] == "def-456"
276278
277279
278280def test_instrumentation_preserves_meta_on_tool_call ():
279- """Test that OTel instrumentation correctly sets _meta on outgoing tool call requests."""
280- captured_params = []
281-
282- def spy_send_request (wrapped , instance , args , kwargs ):
283- if args :
284- request = args [0 ]
285- method = getattr (getattr (request , "root" , None ), "method" , None )
286- if method == "tools/call" and hasattr (request .root , "params" ):
287- params = request .root .params
288- if hasattr (params , "model_dump" ):
289- captured_params .append (params .model_dump (by_alias = True ))
290- elif isinstance (params , dict ):
291- captured_params .append (params .copy ())
292- return wrapped (* args , ** kwargs )
293-
294- stdio_mcp_client = MCPClient (
295- lambda : stdio_client (StdioServerParameters (command = "python" , args = ["tests_integ/mcp/echo_server.py" ]))
296- )
297-
298- with stdio_mcp_client :
299- from mcp .shared .session import BaseSession
300- from wrapt import wrap_function_wrapper
301-
302- original_send = BaseSession .send_request
303- wrap_function_wrapper ("mcp.shared.session" , "BaseSession.send_request" , spy_send_request )
281+ """Test that OTel instrumentation sets _meta that reaches the MCP server."""
282+ from unittest .mock import MagicMock , patch
283+
284+ # Mock the propagator to always inject a known value, bypassing the need for
285+ # an active span on the background thread where send_request runs
286+ mock_textmap = MagicMock ()
287+ mock_textmap .inject = lambda carrier , ** kwargs : carrier .update ({"traceparent" : "00-abc-def-01" })
288+
289+ with patch ("opentelemetry.propagate.get_global_textmap" , return_value = mock_textmap ):
290+ stdio_mcp_client = MCPClient (
291+ lambda : stdio_client (
292+ StdioServerParameters (command = "python" , args = ["tests_integ/mcp/echo_server.py" ])
293+ )
294+ )
304295
305- try :
296+ with stdio_mcp_client :
306297 result = stdio_mcp_client .call_tool_sync (
307298 tool_use_id = "test-instrumentation" ,
308- name = "echo " ,
309- arguments = {"to_echo" : "INSTRUMENTATION_TEST" },
299+ name = "echo_meta " ,
300+ arguments = {},
310301 )
311302
312- assert result ["status" ] == "success"
313- assert len (captured_params ) > 0
314-
315- params = captured_params [- 1 ]
316- assert "_meta" in params
317- assert isinstance (params ["_meta" ], dict )
318- finally :
319- BaseSession .send_request = original_send
303+ assert result ["status" ] == "success"
304+ received_meta = json .loads (result ["content" ][0 ]["text" ])
305+ # OTel instrumentation should have injected _meta with tracing context
306+ assert received_meta is not None
307+ assert isinstance (received_meta , dict )
308+ assert received_meta ["traceparent" ] == "00-abc-def-01"
320309
321310
322311@pytest .mark .skipif (
0 commit comments