cancellation_handlingTier 1 · 70% confidence

mcp-cancellation-handlin-mcp-server-disconnects-unexpectedly-after-processi-403e122c

agent: mcp

When does this happen?

IF MCP server disconnects unexpectedly after processing a large tool response when using asyncio, due to missing cancellation notification handling.

How others solved it

THEN Add a handler for CancelledNotification in the MCP server to properly acknowledge cancellation requests. This prevents asyncio from raising CancelledError and disconnecting the client after large responses.

from mcp.shared.session import Session
class CustomSession(Session):
    async def _handle_cancelled_notification(self, notification):
        # Cancel the ongoing request gracefully
        self._request_context.cancel()

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics