0Pricing
MCP Academy · Lesson

Constraints, Defaults & Enums

Bound values and offer fixed choices safely.

Constraints, Defaults & Enums 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.

Beyond Just Types

Types say what kind of value is allowed, but you often want tighter rules: a limit must be positive, a name not empty. That's where constraints come in.

Meet Field

Pydantic's Field lets you attach rules to a field, like minimum and maximum values, right in the model definition.

from pydantic import BaseModel, Field

class Page(BaseModel):
    size: int = Field(ge=1, le=100)

Numeric Bounds

Use ge and le for greater-or-equal and less-or-equal, or gt and lt for strict bounds, to keep numbers in a safe range.

String Length Limits

For text, min_length and max_length stop empty strings and runaway input before they reach your logic.

name: str = Field(min_length=1, max_length=50)

Pattern Matching

Add a pattern to require a string match a regular expression, perfect for codes, slugs, or simple identifiers.

Simple Defaults

Give a field a value and it becomes optional with a default. The model may omit it and your tool uses the fallback.

limit: int = 10

Defaults Inside Field

You can combine a default with constraints by passing it to Field, keeping the rule and the fallback in one place.

limit: int = Field(default=10, ge=1, le=100)

Fixed Choices with Enum

When only a few values make sense, an Enum limits the field to that exact set, so the model cannot invent options.

from enum import Enum

class Sort(str, Enum):
    asc = "asc"
    desc = "desc"

Use the Enum as a Type

Declare a field with the enum type and Pydantic accepts only its members, rejecting any other value automatically.

order: Sort = Sort.asc

Literals for Tiny Sets

For a quick fixed set you can also use Literal, which lists allowed values inline without a separate Enum class.

from typing import Literal
unit: Literal["c", "f"] = "c"

Constraints Reach the Model

These rules become part of the JSON schema, so the model sees the allowed ranges and choices and tends to obey them.

Quick Check

You want a field to accept only asc or desc. What fits best?

Recap

Use Field for bounds and lengths, plain defaults for optional values, and Enum or Literal for fixed choices the model must respect. ✅

Frequently asked questions

Is the “Constraints, Defaults & Enums” lesson free?

Yes — the full text of “Constraints, Defaults & Enums” 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 “Constraints, Defaults & Enums”?

Bound values and offer fixed choices safely. 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 “Constraints, Defaults & Enums” 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. Why Schemas Beat Loose Args
  2. Model Inputs with Pydantic
  3. Constraints, Defaults & Enums
  4. Field Descriptions That Guide the Model
← Back to MCP Academy