MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / test_tool

Function test_tool

tests/client/test_sampling_callback.py:81–94  ·  view source on GitHub ↗
(message: str, ctx: Context)

Source from the content-addressed store, hash-verified

79
80 @server.tool("test_backwards_compat")
81 async def test_tool(message: str, ctx: Context) -> bool:
82 # Call create_message WITHOUT tools
83 result = await ctx.session.create_message( # pyright: ignore[reportDeprecated]
84 messages=[SamplingMessage(role="user", content=TextContent(type="text", text=message))],
85 max_tokens=100,
86 )
87 # Backwards compat: result should be CreateMessageResult
88 assert isinstance(result, CreateMessageResult)
89 # Content should be single (not a list) - this is the key backwards compat check
90 assert isinstance(result.content, TextContent)
91 assert result.content.text == "Hello from LLM"
92 # CreateMessageResult should NOT have content_as_list (that's on WithTools)
93 assert not hasattr(result, "content_as_list") or not callable(getattr(result, "content_as_list", None))
94 return True
95
96 async with Client(server, sampling_callback=sampling_callback, mode="legacy") as client:
97 result = await client.call_tool("test_backwards_compat", {"message": "Test"})

Callers

nothing calls this directly

Calls 3

SamplingMessageClass · 0.90
TextContentClass · 0.90
create_messageMethod · 0.80

Tested by

no test coverage detected