0Pricing
MCP Academy · レッスン

キャンセルリクエストを処理する

クライアントがキャンセルしたとき、処理を適切に停止します。

「キャンセルリクエストを処理する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「キャンセルリクエストを処理する」で何を学びますか?

クライアントがキャンセルしたとき、処理を適切に停止します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「キャンセルリクエストを処理する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. 長時間タスクの進捗を送信する
  2. キャンセルリクエストを処理する
  3. クライアントへの構造化ログ
  4. 実行時にログレベルを設定する
← MCP Academyに戻る