Files
modelcontextprotocol--pytho…/examples/stories/schema_validators/client.py
T
wehub-resource-sync 49b9bb6724
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 12:10:27 +08:00

39 lines
1.7 KiB
Python

"""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)