Model Inputs with Pydantic
Define a typed shape for a tool's arguments.
Model Inputs with Pydantic is a free MCP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Model Per Input
To shape a tool's arguments, you write a Pydantic model: a small class where each attribute is one typed field of the input.
from pydantic import BaseModel
class SearchInput(BaseModel):
query: str
limit: intSubclass BaseModel
Every input model inherits from BaseModel, which gives it parsing, validation, and JSON schema generation for free.
Fields Are Typed Attributes
Each line names a field and its type. Pydantic reads those annotations to know what valid input looks like.
Pass the Model as One Argument
In a FastMCP tool, accept the model as a single typed parameter, and the framework builds the schema from it.
@mcp.tool()
def search(args: SearchInput) -> str:
return run(args.query, args.limit)Access Fields by Name
Inside the tool you read values as attributes: args.query and args.limit. No dict lookups, and your editor autocompletes them.
Coercion Where Sensible
Pydantic will gently coerce compatible values, like turning the string 5 into the integer 5, so minor mismatches just work.
Rejecting Bad Types
If a value cannot become the declared type, Pydantic raises a clear validation error instead of letting it slip through.
Nested Models
A field can itself be another model, letting you describe nested objects cleanly instead of deeply nested raw dicts.
class Address(BaseModel):
city: str
class User(BaseModel):
name: str
home: AddressSchema Is Generated
Pydantic turns your model into a JSON schema automatically, which MCP forwards to the client so the model knows the input shape.
Reuse Models Across Tools
Define a model once and share it across several tools. That keeps your input shapes consistent and your code dry.
Type Safety End to End
From the model's call to your function body, the data keeps one verified type, so you never juggle untyped dicts again.
Quick Check
How do you define a typed input shape for a tool with Pydantic?
Recap
Subclass BaseModel, list typed fields, and pass the model as one argument. Pydantic validates input and generates the schema. ✅
Frequently asked questions
Is the “Model Inputs with Pydantic” lesson free?
Yes — the full text of “Model Inputs with Pydantic” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Model Inputs with Pydantic”?
Define a typed shape for a tool's arguments. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MCP Academy?
No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Model Inputs with Pydantic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MCP Academy lesson?
Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Schemas Beat Loose Args
- Model Inputs with Pydantic
- Constraints, Defaults & Enums
- Field Descriptions That Guide the Model