Files
Max Isbey 1b74b06753 Tighten comments and docstrings repo-wide
Cut comment and docstring volume roughly in half across src, tests,
examples, and docs_src: removed comments that restate the adjacent code,
leftover development narration, section banners, and self-evident
Args/Returns blocks, and compressed the remaining docstrings to a
Google-style summary line plus only the detail that earns its place.

Kept (and tightened) the load-bearing content: Raises sections,
deprecation and version-availability notes, spec/RFC/issue references,
why-comments for non-obvious decisions, and all coverage pragmas. The
generated mcp_types.v* wire modules are untouched.
2026-06-29 15:10:27 +00:00

59 lines
1.6 KiB
Python

"""Four ways to type a tool parameter so MCPServer derives and enforces inputSchema."""
from dataclasses import dataclass
from typing import Any
from pydantic import BaseModel
# pydantic requires typing_extensions.TypedDict (not typing.TypedDict) for parameter types on Python < 3.12.
from typing_extensions import TypedDict
from mcp.server.mcpserver import MCPServer
from stories._hosting import run_server_from_args
class PersonModel(BaseModel):
name: str
title: str = "friend"
class PersonTD(TypedDict):
name: str
title: str
@dataclass
class PersonDC:
name: str
title: str = "friend"
def build_server() -> MCPServer:
mcp = MCPServer("schema-validators-example")
@mcp.tool()
def greet_pydantic(who: PersonModel) -> str:
"""`who` arrives as a validated PersonModel instance."""
return f"Hello {who.name}, my {who.title}"
@mcp.tool()
def greet_typeddict(who: PersonTD) -> str:
"""`who` arrives as a plain dict; TypedDict drives the schema and editor hints."""
return f"Hello {who['name']}, my {who['title']}"
@mcp.tool()
def greet_dataclass(who: PersonDC) -> str:
"""`who` arrives as a PersonDC instance (pydantic coerces the wire dict)."""
return f"Hello {who.name}, my {who.title}"
@mcp.tool()
def greet_dict(who: dict[str, Any]) -> str:
"""`who` is a free-form object — any dict passes; the handler must check it."""
return f"Hello {who['name']}, my {who.get('title', 'friend')}"
return mcp
if __name__ == "__main__":
run_server_from_args(build_server)