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

Pydantic Inputs

Rich arguments.

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

Some inputs have structure, like a delivery address with a street, city, and postcode. A Pydantic model describes that shape, and the SDK checks every call against it.

Pydantic Inputs
From the video: Pydantic Inputs
  • Structured inputs
  • A Pydantic model
  • Checked for you

An address input

Address is a Pydantic model with three fields. The tool takes one address. We call it once with a full address, and once with the postcode missing. Run it. The first works. The second is rejected before our code even runs.

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

mcp = MCPServer("shop")
class Address(BaseModel):
    street: str
    city: str
    postcode: str

@mcp.tool()
def check_address(address: Address) -> str:
    """Check that Northwind can deliver to an address."""
    return f"We deliver to {address.city} ({address.postcode})"

full = {"street": "1 Park Row", "city": "Leeds", "postcode": "LS1 5AB"}
for a in [full, {"street": "2 High St", "city": "York"}]:
    r = call(mcp, "check_address", {"address": a})
    print(r.is_error, "|", r.content[0].text[:60])
Output
False | We deliver to Leeds (LS1 5AB)
True | Error executing tool check_address: 1 validation error for c
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())

Why Pydantic

The model sees the full shape in the schema. Bad calls are rejected with a clear message, so the model can fix them and try again. And your function gets a real object, not a loose dictionary.

keyvalue
Schemafull shape
Validationbefore your code
Errorsclear for the model
You geta real object

Summary

Use Pydantic models for structured inputs. The SDK validates them before your code runs. Next, structured output.

Key takeaways
  • BaseModel
  • Validated
  • Real objects
Next up: Structured Output — return a model