0Pricing
MCP Academy · درس

مدخلات النموذج باستخدام Pydantic

حدّدوا بنية مكتوبة الأنواع لمعلمات الأداة.

مدخلات النموذج باستخدام Pydantic درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.

ماذا ستتعلم في «مدخلات النموذج باستخدام Pydantic»؟

حدّدوا بنية مكتوبة الأنواع لمعلمات الأداة. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟

لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «مدخلات النموذج باستخدام Pydantic»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟

نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا تتفوق المخططات على المعلمات غير المنظمة
  2. مدخلات النموذج باستخدام Pydantic
  3. القيود والقيم الافتراضية والتعدادات
  4. أوصاف الحقول التي ترشد النموذج
← العودة إلى MCP Academy