AI Hints
Home›MCP Tutorial›Rich Inputs, Outputs and Errors
MCP · Lesson 17 of 120

Structured Output

Return a model.

⏱ 2 min read▶ Video at 15:12🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

Tools can return more than text. When a tool returns a Pydantic model, the SDK sends structured data with a schema, as well as a text version. Programs can then use the fields directly.

Structured Output
From the video: Structured Output
  • More than text
  • Return a model
  • Fields for programs

Returning an order

Order is a model with an ID, a status, and a list of items. The tool returns an Order. The result has structured content, a dictionary. Run it. We read the status straight from the structured content.

structured.pypython
from pydantic import BaseModel
from mcp.server.mcpserver import MCPServer
from tester import call

mcp = MCPServer("shop")

class Order(BaseModel):
    id: int
    status: str
    items: list[str]

@mcp.tool()
def get_order(order_id: int) -> Order:
    """Get one Northwind order."""
    return Order(id=order_id, status="shipped", items=["helmet", "lights"])

r = call(mcp, "get_order", {"order_id": 1042})
print("structured:", r.structured_content)
print("status:    ", r.structured_content["status"])
Output
structured: {'id': 1042, 'status': 'shipped', 'items': ['helmet', 'lights']}
status:     shipped
Also uses tester.py
import asyncio
from mcp import Client

def call(server, tool, args=None, **options):
    async def go():
        async with Client(server, **options) as client:
            return await client.call_tool(tool, args or {})
    return asyncio.run(go())

def tools(server):
    async def go():
        async with Client(server) as client:
            return (await client.list_tools()).tools
    return asyncio.run(go())

Text and structure

Every result has content, usually text, for the model to read. Structured content is for programs. Returning a model, a dictionary, or even a plain string all give structured output, with an output schema.

keyvalue
contentfor the model
structured_contentfor programs
output_schemadescribes it
Returna model or dict

Summary

Return Pydantic models to give clients structured data, with an output schema. Next, tool errors.

Key takeaways
  • Return a model
  • structured_content
  • output_schema
Next up: Tool Errors — ToolError