@@ -254,6 +254,11 @@ Tools automatically generate JSON Schema definitions for their return types, hel
254254
255255``` python
256256from pydantic import BaseModel
257+ from mcp.server.fastmcp import FastMCP
258+
259+ # Create server
260+ mcp = FastMCP(" Output Schema Demo" )
261+
257262
258263# Tools with primitive return types
259264@mcp.tool ()
@@ -262,50 +267,60 @@ def get_temperature(city: str) -> float:
262267 # In a real implementation, this would fetch actual weather data
263268 return 72.5
264269
270+
265271# Tools with dictionary return types
266272@mcp.tool ()
267273def get_user (user_id : int ) -> dict :
268274 """ Get user information by ID"""
269275 return {" id" : user_id, " name" : " John Doe" , " email" : " john@example.com" }
270276
277+
271278# Using Pydantic models for structured output
272279class WeatherData (BaseModel ):
273280 temperature: float
274281 humidity: float
275282 conditions: str
276283
284+
277285@mcp.tool ()
278286def get_weather_data (city : str ) -> WeatherData:
279287 """ Get structured weather data for a city"""
280288 # In a real implementation, this would fetch actual weather data
281289 return WeatherData(
282290 temperature = 72.5 ,
283291 humidity = 65.0 ,
284- conditions = " Partly cloudy"
292+ conditions = " Partly cloudy" ,
285293 )
286294
295+
287296# Complex nested models
288297class Location (BaseModel ):
289298 city: str
290299 country: str
291300 coordinates: tuple[float , float ]
292301
302+
293303class WeatherForecast (BaseModel ):
294304 current: WeatherData
295305 location: Location
296306 forecast: list[WeatherData]
297307
308+
298309@mcp.tool ()
299310def get_weather_forecast (city : str ) -> WeatherForecast:
300311 """ Get detailed weather forecast for a city"""
301312 # In a real implementation, this would fetch actual forecast data
302313 return WeatherForecast(
303- current = WeatherData(temperature = 72.5 , humidity = 65.0 , conditions = " Partly cloudy" ),
314+ current = WeatherData(
315+ temperature = 72.5 ,
316+ humidity = 65.0 ,
317+ conditions = " Partly cloudy" ,
318+ ),
304319 location = Location(city = city, country = " USA" , coordinates = (37.7749 , - 122.4194 )),
305320 forecast = [
306321 WeatherData(temperature = 75.0 , humidity = 62.0 , conditions = " Sunny" ),
307- WeatherData(temperature = 68.0 , humidity = 80.0 , conditions = " Rainy" )
308- ]
322+ WeatherData(temperature = 68.0 , humidity = 80.0 , conditions = " Rainy" ),
323+ ],
309324 )
310325```
311326
0 commit comments