Pydantic schemas

Optional. When you set them, the library validates inputs and shapes outputs. When you don't, it falls back to Django field introspection.

Install

pip install 'django-zeromcp[schemas]'

Three slots

from pydantic import BaseModel, EmailStr, Field
from zeromcp.base import BaseResource
from myapp.models import User

class UserCreate(BaseModel):
    email: EmailStr
    password: str = Field(min_length=8)

class UserUpdate(BaseModel):
    email: EmailStr | None = None
    name: str | None = None

class UserOut(BaseModel):
    id: int
    email: EmailStr
    name: str

class UserResource(BaseResource):
    model = User
    create_schema = UserCreate    # validates POST body
    update_schema = UserUpdate    # validates PATCH body
    list_schema = UserOut         # shapes GET responses

What happens on POST/PATCH

{
  "success": false,
  "status": 422,
  "detail": [
    {"field": "email", "message": "value is not a valid email address"},
    {"field": "password", "message": "String should have at least 8 characters"}
  ]
}

What happens on GET

When list_schema is set, every row in the response is run through the schema before serialization. Extra fields are dropped, types are coerced, missing fields cause errors loud and early.

Hybrid mode

Schemas are per-slot. Set create_schema only and PATCH still uses update_fields. Set list_schema only and writes still use field whitelists. Mix and match as needed.

When schemas are absent

Without any schema, the library falls back to:

Both modes coexist in the same project. Adopt schemas one resource at a time.

📦

Pydantic is an optional dependency. Without it installed, resources without schemas keep working; resources with schemas raise a clear RuntimeError asking you to install the extra.

0-mcp by Stamatios Stamou Jr — github.com/ssjunior/0-mcp