Validate & Sanitize Everything
Treat model-supplied input as untrusted.
Validate & Sanitize Everything is a free MCP Academy lesson on CoddyKit — lesson 3 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.
Inputs Are Untrusted
Every argument the model passes to a tool is untrusted input. The model may be honest, but its arguments can be shaped by injected text, so verify them. 🔎
Validate Before You Act
Check shape, type, and range before a tool does anything real. Validation at the door turns a vague bad call into a clear, safe rejection.
Let Pydantic Help
Type hints and Pydantic models reject malformed arguments before your code runs. This input must be a positive int, so a string or a negative value is refused early.
from pydantic import Field
@mcp.tool()
def get_page(n: int = Field(gt=0, le=100)) -> str:
return load(n)Bound the Values
Cap sizes, lengths, and counts. A limit field on a query keeps the model from asking for a million rows and turning one call into a denial of service.
Sanitize for the Destination
Sanitizing means making input safe for where it lands. A value safe in a log can be dangerous in a shell, a SQL string, or a file path.
Never Build SQL by Hand
String-concatenated SQL invites injection. Use parameterized queries so the database treats model input strictly as a value, never as executable code.
cur.execute(
"SELECT * FROM orders WHERE id = ?",
(order_id,),
)Guard the Shell
If a tool runs a command, never paste arguments into a shell string. Pass an argument list and avoid shell parsing so input cannot smuggle in extra commands.
Resolve and Confine Paths
For file tools, resolve the path to its canonical form and confirm it stays inside your allowed root. Reject anything with a path traversal like dot-dot.
p = (ROOT / name).resolve()
if not p.is_relative_to(ROOT):
raise ValueError("path escapes root")Fail Closed
When input does not pass a check, stop and return a clear error. Failing closed beats guessing, because a wrong guess can be exactly what an attacker wants.
Do Not Trust Tool Output Either
Data your tool returns can carry injected instructions onward. Where it matters, label or escape fetched content so the model treats it as data, not orders.
Validate at Every Boundary
Check input as it enters your tool and again before it hits a database, file, or API. Each boundary is a fresh chance to catch something unsafe.
Quick Check
Which choice best protects a SQL query from injection?
Recap
Treat all tool inputs as untrusted: validate types and ranges, sanitize for the destination, confine paths, and fail closed. Distrust the input. ✅
Frequently asked questions
Is the “Validate & Sanitize Everything” lesson free?
Yes — the full text of “Validate & Sanitize Everything” 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 “Validate & Sanitize Everything”?
Treat model-supplied input as untrusted. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Validate & Sanitize Everything” 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
- Threats Unique to MCP
- Least-Privilege Tool Access
- Validate & Sanitize Everything
- Guard Destructive Actions