0Pricing
MCP Academy · レッスン

Pydanticでモデル入力を定義する

ツールの引数に型付きの構造を定義します。

「Pydanticでモデル入力を定義する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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: int

Subclass 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: Address

Schema 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. ✅

よくある質問

「Pydanticでモデル入力を定義する」レッスンは無料ですか?

はい。「Pydanticでモデル入力を定義する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「Pydanticでモデル入力を定義する」で何を学びますか?

ツールの引数に型付きの構造を定義します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MCP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Pydanticでモデル入力を定義する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMCP Academyレッスンでコードを書いて実行できますか?

はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 自由形式の引数よりスキーマが優れている理由
  2. Pydanticでモデル入力を定義する
  3. 制約、デフォルト値、列挙型
  4. モデルを導くフィールド説明
← MCP Academyに戻る