Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cancelled handler. Fixes #88 #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/mcp/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def main():
import logging
import warnings
from collections.abc import Awaitable, Callable
from typing import Any, Sequence
from typing import Any, Optional, Sequence

from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from pydantic import AnyUrl
Expand Down Expand Up @@ -379,6 +379,20 @@ async def handler(req: types.ProgressNotification):

return decorator

def cancellation_notification(self):
def decorator(
func: Callable[[Optional[int], Optional[str]], Awaitable[None]],
):
logger.debug("Registering handler for ProgressNotification")

async def handler(req: types.CancellationNotification):
await func(req.params.requestId, req.params.reason)

self.notification_handlers[types.CancellationNotification] = handler
return func

return decorator

def completion(self):
"""Provides completions for prompts and resource templates"""

Expand Down
23 changes: 21 additions & 2 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Generic, Literal, TypeVar
from typing import Any, Generic, Literal, Optional, TypeVar

from pydantic import BaseModel, ConfigDict, FileUrl, RootModel
from pydantic.networks import AnyUrl
Expand Down Expand Up @@ -325,6 +325,21 @@ class ProgressNotificationParams(NotificationParams):
model_config = ConfigDict(extra="allow")


class CancelledParams(BaseModel):
requestId: Optional[int] = None
reason: Optional[str] = ""


class CancellationNotification(Notification):
"""
An out-of-band notification used to inform the receiver of a progress update for a
long-running request.
"""

method: Literal["cancelled"]
params: CancelledParams


class ProgressNotification(Notification):
"""
An out-of-band notification used to inform the receiver of a progress update for a
Expand Down Expand Up @@ -997,7 +1012,10 @@ class ClientRequest(

class ClientNotification(
RootModel[
ProgressNotification | InitializedNotification | RootsListChangedNotification
ProgressNotification
| InitializedNotification
| RootsListChangedNotification
| CancellationNotification
]
):
pass
Expand All @@ -1019,6 +1037,7 @@ class ServerNotification(
| ResourceListChangedNotification
| ToolListChangedNotification
| PromptListChangedNotification
| CancellationNotification
]
):
pass
Expand Down