Dane wejściowe modelu z Pydantic
Zdefiniuj typowany kształt argumentów narzędzia.
Dane wejściowe modelu z Pydantic to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. ✅
Często zadawane pytania
Czy lekcja „Dane wejściowe modelu z Pydantic” jest bezpłatna?
Tak — pełny tekst „Dane wejściowe modelu z Pydantic” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Dane wejściowe modelu z Pydantic”?
Zdefiniuj typowany kształt argumentów narzędzia. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć MCP Academy?
Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Dane wejściowe modelu z Pydantic”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?
Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Dlaczego schematy są lepsze od luźnych argumentów
- Dane wejściowe modelu z Pydantic
- Ograniczenia, wartości domyślne i wyliczenia
- Opisy pól pomagające modelowi