0Pricing
MCP Academy · درس

التحقق من المدخلات قبل التنفيذ

ارفضوا المعلمات غير الصالحة برسائل واضحة.

التحقق من المدخلات قبل التنفيذ درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Trust Nothing the Model Sends

The arguments your tool receives come from an AI, which can guess. Validate every input before you touch a file, a database, or an API.

Check First, Act Second

Run your checks at the very top of the function. Confirm the input is sane before doing any real or irreversible work.

@mcp.tool()
def set_age(age: int) -> str:
    if age < 0 or age > 130:
        raise ValueError("age out of range")

Reject With a Clear Reason

When input fails a check, say exactly why. A precise message lets the model send a corrected value on the next attempt.

Type Hints Catch a Lot

Your parameter hints become the tool schema, so basic type mismatches are caught before your code even runs. Lean on them first.

@mcp.tool()
def repeat(text: str, times: int) -> str:
    return text * times

Hints Are Not Enough

Types alone cannot express ranges, formats, or allowed values. You still validate that a count is positive or a status is one you accept.

Bound Your Ranges

Guard numeric inputs against extremes. A limit on page size or loop count stops the model from accidentally overloading your server.

if limit > 100:
    raise ValueError("limit must be 100 or less")

Whitelist Allowed Values

For fixed choices, check membership instead of trusting free text. A whitelist rejects anything outside the options you actually support.

if status not in {"open", "closed"}:
    raise ValueError("status must be open or closed")

Sanitize Paths and Queries

Model input that becomes a file path or SQL query is dangerous. Sanitize it so a stray .. or quote cannot reach beyond what you intend.

Required Fields Must Exist

Confirm that anything mandatory is actually present and non-empty. An early guard beats a confusing failure deep inside your logic.

if not query.strip():
    raise ValueError("query cannot be empty")

Validation Doubles as Docs

Clear checks teach the model your rules. After one rejection it learns the format and sends valid input the rest of the session.

Fail Before Side Effects

The golden rule: never start an irreversible action until every input has passed. Validate up front so half-done writes never happen.

Quick Check

Why validate inputs before performing the tool's real work?

Recap

Treat model input as untrusted: validate types, ranges, and choices at the top, reject with clear reasons, and never act before checks pass. ✅

الأسئلة الشائعة

هل درس «التحقق من المدخلات قبل التنفيذ» مجاني؟

نعم — نص درس «التحقق من المدخلات قبل التنفيذ» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحقق من المدخلات قبل التنفيذ»؟

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

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

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

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

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

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

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

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

  1. أخطاء الأدوات التي يستطيع النموذج رؤيتها
  2. أخطاء البروتوكول في مقابل أعطال الأدوات
  3. التحقق من المدخلات قبل التنفيذ
  4. الفشل بأمان دون تعليق التنفيذ
← العودة إلى MCP Academy