0Pricing
MCP Academy · درس

الفشل بأمان دون تعليق التنفيذ

احموا النظام من المهلات والاستثناءات غير المعالجة.

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

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

A Hung Tool Is Worse Than a Failed One

A tool that errors clearly lets the model move on. A tool that hangs freezes the whole call, so always design for a timely exit.

Catch the Unexpected

Wrap risky work so a surprise exception becomes a clean error, not a crash. The server stays up and the model learns what failed.

try:
    data = parse(raw)
except Exception as e:
    raise ValueError("could not parse input")

Always Set a Timeout

Any network or external call needs a timeout. Without one, a slow upstream can pin your tool open indefinitely and stall the client.

import httpx
r = httpx.get(url, timeout=10.0)

Prefer Async for I/O

Async tools let one slow request wait without blocking everything else, so your server keeps serving other calls meanwhile.

@mcp.tool()
async def fetch(url: str) -> str:
    async with httpx.AsyncClient() as c:
        r = await c.get(url, timeout=10)

Honor Cancellation

If the client cancels, stop promptly. Respecting cancellation frees resources and avoids work nobody is waiting for anymore.

Bound Loops and Retries

Cap any loop or retry. An unbounded retry on a failing API can spin forever, so set a max attempts and then give up gracefully.

for attempt in range(3):
    if try_call():
        break

Release What You Open

Use with blocks or finally so files and connections always close, even when an error fires partway through the function.

with open(path) as f:
    return f.read()

Degrade Gracefully

When part of a job fails, return what you have with a note rather than nothing. A partial result still helps the model decide.

Log the Failure Server-Side

Record the full detail in your logs while sending the model only a safe summary. You get to debug without leaking internals.

One Crash Shouldn't End the Server

A single bad call must not take down the process. Isolating failures per tool keeps every other capability available to clients.

Always Return Something

Every path through your tool should end in a response, success or error. Silence leaves the client waiting and the model stuck.

Quick Check

What is the key reason to add a timeout to an external call in a tool?

Recap

Fail fast, never hang: catch surprises, set timeouts, bound retries, release resources, log safely, and always return a response. ✅

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

هل درس «الفشل بأمان دون تعليق التنفيذ» مجاني؟

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

ماذا ستتعلم في «الفشل بأمان دون تعليق التنفيذ»؟

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

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

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

كم من الوقت يستغرق درس «الفشل بأمان دون تعليق التنفيذ»؟

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

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

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

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

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