docs/python-sdk/fastmcp-server-tasks-notifications.mdx
fastmcp.server.tasks.notificationsDistributed notification queue for background task events (SEP-1686).
Enables distributed Docket workers to send MCP notifications to clients without holding session references. Workers push to a Redis queue, the MCP server process subscribes and forwards to the client's session.
Pattern: Fire-and-forward with retry
Note: Docket's execution.subscribe() handles task state/progress events via Redis Pub/Sub. This module handles elicitation-specific notifications that require reliable delivery (input_required prompts, cancel signals).
push_notification <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/notifications.py#L48" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>push_notification(session_id: str, notification: dict[str, Any], docket: Docket) -> None
Push notification to session's queue (called from Docket worker).
Used for elicitation-specific notifications (input_required, cancel) that need reliable delivery across distributed processes.
Args:
session_id: Target session's identifiernotification: MCP notification dict (method, params, _meta)docket: Docket instance for Redis accessnotification_subscriber_loop <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/notifications.py#L76" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>notification_subscriber_loop(session_id: str, session: ServerSession, docket: Docket, fastmcp: FastMCP) -> None
Subscribe to notification queue and forward to session.
Runs in the MCP server process. Bridges distributed workers to clients.
This loop:
Args:
session_id: Session identifier to subscribe tosession: MCP ServerSession for sending notificationsdocket: Docket instance for Redis accessfastmcp: FastMCP server instance (for elicitation relay)ensure_subscriber_running <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/notifications.py#L246" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>ensure_subscriber_running(session_id: str, session: ServerSession, docket: Docket, fastmcp: FastMCP) -> None
Start notification subscriber if not already running (idempotent).
Subscriber is created on first task submission and cleaned up on disconnect. Safe to call multiple times for the same session.
Args:
session_id: Session identifiersession: MCP ServerSessiondocket: Docket instancefastmcp: FastMCP server instance (for elicitation relay)stop_subscriber <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/notifications.py#L286" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>stop_subscriber(session_id: str) -> None
Stop notification subscriber for a session.
Called when session disconnects. Pending messages remain in queue for delivery if client reconnects (with TTL expiration).
Args:
session_id: Session identifierget_subscriber_count <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/notifications.py#L306" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_subscriber_count() -> int
Get number of active subscribers (for monitoring).