-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathserver.py
More file actions
59 lines (40 loc) · 1.6 KB
/
Copy pathserver.py
File metadata and controls
59 lines (40 loc) · 1.6 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""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) on Python < 3.12
# when a TypedDict is used as a field/parameter type.
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)