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) Has been cancelled
98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
"""Example showing structured output with tools."""
|
|
|
|
from typing import TypedDict
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from mcp.server.mcpserver import MCPServer
|
|
|
|
mcp = MCPServer("Structured Output Example")
|
|
|
|
|
|
# Using Pydantic models for rich structured data
|
|
class WeatherData(BaseModel):
|
|
"""Weather information structure."""
|
|
|
|
temperature: float = Field(description="Temperature in Celsius")
|
|
humidity: float = Field(description="Humidity percentage")
|
|
condition: str
|
|
wind_speed: float
|
|
|
|
|
|
@mcp.tool()
|
|
def get_weather(city: str) -> WeatherData:
|
|
"""Get weather for a city - returns structured data."""
|
|
# Simulated weather data
|
|
return WeatherData(
|
|
temperature=22.5,
|
|
humidity=45.0,
|
|
condition="sunny",
|
|
wind_speed=5.2,
|
|
)
|
|
|
|
|
|
# Using TypedDict for simpler structures
|
|
class LocationInfo(TypedDict):
|
|
latitude: float
|
|
longitude: float
|
|
name: str
|
|
|
|
|
|
@mcp.tool()
|
|
def get_location(address: str) -> LocationInfo:
|
|
"""Get location coordinates"""
|
|
return LocationInfo(latitude=51.5074, longitude=-0.1278, name="London, UK")
|
|
|
|
|
|
# Using dict[str, Any] for flexible schemas
|
|
@mcp.tool()
|
|
def get_statistics(data_type: str) -> dict[str, float]:
|
|
"""Get various statistics"""
|
|
return {"mean": 42.5, "median": 40.0, "std_dev": 5.2}
|
|
|
|
|
|
# Ordinary classes with type hints work for structured output
|
|
class UserProfile:
|
|
name: str
|
|
age: int
|
|
email: str | None = None
|
|
|
|
def __init__(self, name: str, age: int, email: str | None = None):
|
|
self.name = name
|
|
self.age = age
|
|
self.email = email
|
|
|
|
|
|
@mcp.tool()
|
|
def get_user(user_id: str) -> UserProfile:
|
|
"""Get user profile - returns structured data"""
|
|
return UserProfile(name="Alice", age=30, email="alice@example.com")
|
|
|
|
|
|
# Classes WITHOUT type hints cannot be used for structured output
|
|
class UntypedConfig:
|
|
def __init__(self, setting1, setting2): # type: ignore[reportMissingParameterType]
|
|
self.setting1 = setting1
|
|
self.setting2 = setting2
|
|
|
|
|
|
@mcp.tool()
|
|
def get_config() -> UntypedConfig:
|
|
"""This returns unstructured output - no schema generated"""
|
|
return UntypedConfig("value1", "value2")
|
|
|
|
|
|
# Lists and other types are wrapped automatically
|
|
@mcp.tool()
|
|
def list_cities() -> list[str]:
|
|
"""Get a list of cities"""
|
|
return ["London", "Paris", "Tokyo"]
|
|
# Returns: {"result": ["London", "Paris", "Tokyo"]}
|
|
|
|
|
|
@mcp.tool()
|
|
def get_temperature(city: str) -> float:
|
|
"""Get temperature as a simple float"""
|
|
return 22.5
|
|
# Returns: {"result": 22.5}
|