0Pricing
MCP Academy · درس

معالجة طلبات الإلغاء

أوقفوا العمل بطريقة سليمة عند إلغاء العميل.

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

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

Why Cancellation Exists

A user may close a chat or change their mind while a long tool runs. MCP lets the client send a cancellation so wasted work stops.

The cancelled Notification

The client signals this with a notifications/cancelled message naming the requestId it wants to stop. It is fire-and-forget, with no reply.

{"method": "notifications/cancelled",
 "params": {"requestId": 7, "reason": "user aborted"}}

It Is Just a Request

Only an in-flight request can be cancelled, matched by its requestId. Notifications, which have no id, can never be cancelled.

Cancellation Is Best-Effort

A cancel is a polite request, not a hard kill. Your work may already be finishing, so treat it as a best-effort hint to wind down.

asyncio Does the Wiring

In the Python SDK, a cancel arrives as an asyncio.CancelledError raised inside your awaiting tool. You usually do not poll a flag yourself.

import asyncio
# CancelledError is raised at the next await point

Let It Propagate

The simplest correct behavior is to let CancelledError bubble up. The framework then stops the task and skips sending a normal result.

@mcp.tool()
async def crunch(ctx: Context) -> str:
    for item in items:
        await heavy(item)  # cancel lands here

Clean Up on Cancel

If your tool holds a file or lock, wrap it so a cancel still releases resources. A try/finally guarantees cleanup runs on the way out.

try:
    await do_work()
finally:
    handle.close()

Do Not Swallow It

Never catch CancelledError and quietly keep going. That defeats the whole point and leaves the client thinking the work was stopped.

# Anti-pattern:
# except asyncio.CancelledError:
#     pass  # do NOT do this

No Response After Cancel

Once a request is cancelled, do not send a result or error for it. The client has moved on and any late response is simply ignored.

Free the Budget

Honoring cancellation quickly frees CPU, network, and model tokens. A server that stops promptly is a good citizen in a busy agent.

Check at Natural Points

For tight CPU loops with no awaits, sprinkle a short await asyncio.sleep(0) so the cancellation has a chance to land between iterations.

for chunk in chunks:
    crunch(chunk)
    await asyncio.sleep(0)

Quick Check

Check how cancellation surfaces in a Python MCP tool.

Recap: Cancellation

You saw the cancelled notification, why cancels are best-effort, and how to let CancelledError propagate while cleaning up. Well done!

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

هل درس «معالجة طلبات الإلغاء» مجاني؟

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

ماذا ستتعلم في «معالجة طلبات الإلغاء»؟

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

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

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

كم من الوقت يستغرق درس «معالجة طلبات الإلغاء»؟

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

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

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

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

  1. إرسال التقدم للمهام الطويلة
  2. معالجة طلبات الإلغاء
  3. التسجيل المنظم إلى العميل
  4. ضبط مستويات السجل أثناء التشغيل
← العودة إلى MCP Academy