refactor: replace lowlevel Server decorators with on_* constructor kwargs (#1985)

This commit is contained in:
Max Isbey 2026-02-12 15:55:54 +00:00 • committed by GitHub
commit 0a22a9dc33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 3563 additions and 5481 deletions

View file

@ -13,8 +13,12 @@ SessionT = TypeVar("SessionT", bound=BaseSession[Any, Any, Any, Any, Any])
@dataclass(kw_only=True)
class RequestContext(Generic[SessionT]):
"""Common context for handling incoming requests."""
"""Common context for handling incoming requests.
For request handlers, request_id is always populated.
For notification handlers, request_id is None.
"""
request_id: RequestId
meta: RequestParamsMeta | None
session: SessionT
request_id: RequestId | None = None
meta: RequestParamsMeta | None = None

View file

@ -72,9 +72,8 @@ async def cancel_task(
- Task is already in a terminal state (completed, failed, cancelled)
Example:
@server.experimental.cancel_task()
async def handle_cancel(request: CancelTaskRequest) -> CancelTaskResult:
return await cancel_task(store, request.params.taskId)
async def handle_cancel(ctx, params: CancelTaskRequestParams) -> CancelTaskResult:
return await cancel_task(store, params.task_id)
"""
task = await store.get_task(task_id)
if task is None:

View file

@ -6,6 +6,7 @@ to support transport-specific features like resumability.
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from mcp.types import JSONRPCMessage, RequestId
@ -30,8 +31,10 @@ class ServerMessageMetadata:
"""Metadata specific to server messages."""
related_request_id: RequestId | None = None
# Request-specific context (e.g., headers, auth info)
request_context: object | None = None
# Transport-specific request context (e.g. starlette Request for HTTP
# transports, None for stdio). Typed as Any because the server layer is
# transport-agnostic.
request_context: Any = None
# Callback to close SSE stream for the current request without terminating
close_sse_stream: CloseSSEStreamCallback | None = None
# Callback to close the standalone GET SSE stream (for unsolicited notifications)