0Pricing
MCP Academy · Lesson

Validate Inputs Before Acting

Reject bad arguments with clear messages.

Validate Inputs Before Acting 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.

Trust Nothing the Model Sends

The arguments your tool receives come from an AI, which can guess. Validate every input before you touch a file, a database, or an API.

Check First, Act Second

Run your checks at the very top of the function. Confirm the input is sane before doing any real or irreversible work.

@mcp.tool()
def set_age(age: int) -> str:
    if age < 0 or age > 130:
        raise ValueError("age out of range")

Reject With a Clear Reason

When input fails a check, say exactly why. A precise message lets the model send a corrected value on the next attempt.

Type Hints Catch a Lot

Your parameter hints become the tool schema, so basic type mismatches are caught before your code even runs. Lean on them first.

@mcp.tool()
def repeat(text: str, times: int) -> str:
    return text * times

Hints Are Not Enough

Types alone cannot express ranges, formats, or allowed values. You still validate that a count is positive or a status is one you accept.

Bound Your Ranges

Guard numeric inputs against extremes. A limit on page size or loop count stops the model from accidentally overloading your server.

if limit > 100:
    raise ValueError("limit must be 100 or less")

Whitelist Allowed Values

For fixed choices, check membership instead of trusting free text. A whitelist rejects anything outside the options you actually support.

if status not in {"open", "closed"}:
    raise ValueError("status must be open or closed")

Sanitize Paths and Queries

Model input that becomes a file path or SQL query is dangerous. Sanitize it so a stray .. or quote cannot reach beyond what you intend.

Required Fields Must Exist

Confirm that anything mandatory is actually present and non-empty. An early guard beats a confusing failure deep inside your logic.

if not query.strip():
    raise ValueError("query cannot be empty")

Validation Doubles as Docs

Clear checks teach the model your rules. After one rejection it learns the format and sends valid input the rest of the session.

Fail Before Side Effects

The golden rule: never start an irreversible action until every input has passed. Validate up front so half-done writes never happen.

Quick Check

Why validate inputs before performing the tool's real work?

Recap

Treat model input as untrusted: validate types, ranges, and choices at the top, reject with clear reasons, and never act before checks pass. ✅

Frequently asked questions

Is the “Validate Inputs Before Acting” lesson free?

Yes — the full text of “Validate Inputs Before Acting” 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 Inputs Before Acting”?

Reject bad arguments with clear messages. 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 Inputs Before Acting” 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

  1. Tool Errors the Model Can See
  2. Protocol Errors vs Tool Failures
  3. Validate Inputs Before Acting
  4. Fail Safely, Never Hang
← Back to MCP Academy