-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathclient.py
More file actions
38 lines (29 loc) · 1.7 KB
/
Copy pathclient.py
File metadata and controls
38 lines (29 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Asserts each variant publishes a `who` object schema and the call round-trips."""
from mcp_types import TextContent
from mcp.client import Client
from stories._harness import Target, run_client
async def main(target: Target, *, mode: str = "auto") -> None:
async with Client(target, mode=mode) as client:
listed = await client.list_tools()
by_name = {t.name: t for t in listed.tools}
assert set(by_name) == {"greet_pydantic", "greet_typeddict", "greet_dataclass", "greet_dict"}
for name in ("greet_pydantic", "greet_typeddict", "greet_dataclass"):
schema = by_name[name].input_schema
assert schema["required"] == ["who"], schema
# MCPServer emits a $defs/$ref pair; lowlevel inlines. Resolve either.
who = schema["properties"]["who"]
if "$ref" in who:
who = schema["$defs"][who["$ref"].rsplit("/", 1)[-1]]
assert "name" in who["properties"], who
result = await client.call_tool(name, {"who": {"name": "Ada", "title": "colleague"}})
assert not result.is_error, result
assert isinstance(result.content[0], TextContent)
assert result.content[0].text == "Hello Ada, my colleague"
# dict[str, Any] → free-form object schema, no nested `properties` required.
dict_who = by_name["greet_dict"].input_schema["properties"]["who"]
assert dict_who["type"] == "object" and "$ref" not in dict_who
result = await client.call_tool("greet_dict", {"who": {"name": "Ada"}})
assert isinstance(result.content[0], TextContent)
assert result.content[0].text == "Hello Ada, my friend"
if __name__ == "__main__":
run_client(main)