subscriptions
Server-side subscriptions/listen support (2026-07-28, SEP-2575).
On the 2026-07-28 wire there is no standing GET stream: a client opts in to
server events by sending a subscriptions/listen request whose response IS
the stream. This module provides the two pieces a server needs:
SubscriptionBus: the pluggable fan-out seam. The bus carries typedServerEventvalues, not wire notifications - the listen handler owns subscription-id stamping and per-stream filtering, so a custom bus (e.g. backed by Redis pub/sub for multi-replica deployments) never sees JSON-RPC. The in-process default isInMemorySubscriptionBus.ListenHandler: the request handler that servessubscriptions/listen.MCPServerregisters one automatically; lowlevelServerusers pass an instance ason_subscriptions_listen=.
The event vocabulary lives in mcp.shared.subscriptions, shared with the client driver, and is re-exported here.
Per the spec, the handler acknowledges first (the ack is the first frame on
the stream), tags every frame with the listen request's JSON-RPC id under
_meta["io.modelcontextprotocol/subscriptionId"], and never delivers an
event kind the client did not request. Delivery is fire-and-forget with no
replay: a dropped stream is not resumable - clients re-listen and refetch.
InMemorySubscriptionBus
In-process SubscriptionBus: synchronous fan-out to listeners in subscription order.
Source code in src/mcp/server/subscriptions.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
publish
async
publish(event: ServerEvent) -> None
Deliver event to every subscribed listener.
A raising listener is logged and skipped: one bad listener must not starve the others or fail the publishing handler. Ends with a checkpoint so a burst of publishes from one task lets listen streams drain between events instead of overflowing their buffers unread.
Source code in src/mcp/server/subscriptions.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
subscribe
subscribe(
listener: Callable[[ServerEvent], None],
) -> Callable[[], None]
Register listener and return an idempotent unsubscribe callable.
Source code in src/mcp/server/subscriptions.py
117 118 119 120 121 122 123 124 125 | |
ListenHandler
Serves subscriptions/listen: one call is one subscription stream.
Register on a lowlevel Server via on_subscriptions_listen= (or
add_request_handler); MCPServer does so automatically. Each call
acknowledges the honored filter first, then forwards matching bus events
onto the request's response stream until the client disconnects (which
cancels the handler; the stream just ends, per the spec's abrupt-close
contract) or close ends all streams gracefully.
Serves any transport whose dispatch context forwards request-scoped notifications: streamable HTTP's SSE mode (the response IS the stream) and the stream-pair dual-era loop (stdio), where the listen request simply stays pending while the ack and events ride the duplex pipe.
max_subscriptions bounds concurrent streams (further listen requests are
rejected with INTERNAL_ERROR, before the ack). max_buffered_events
bounds each stream's event backlog: a stream whose client has stopped
reading is ended at the cap (the client re-listens and refetches - there
is no replay, so ending the stream loses nothing the backlog wasn't
already losing).
Source code in src/mcp/server/subscriptions.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
__call__
async
__call__(
ctx: ServerRequestContext[Any, Any],
params: SubscriptionsListenRequestParams,
) -> SubscriptionsListenResult
Serve one listen stream.
Source code in src/mcp/server/subscriptions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
close
close() -> None
Initiate graceful closure of every open listen stream.
Each stream then drains its buffered events and sends its
SubscriptionsListenResult (stamped with the subscription id) as the
final frame from its own handler task - the spec's graceful closure
flow, telling clients the stream ended deliberately rather than
dropping. This method only initiates that; it does not wait for the
streams to finish flushing.
Source code in src/mcp/server/subscriptions.py
245 246 247 248 249 250 251 252 253 254 255 256 | |
SubscriptionBus
Bases: Protocol
Fan-out seam between event publishers and open listen streams.
Implement this over an external pub/sub backend (Redis, NATS, ...) to fan
events out across replicas: publish forwards the event to the backend,
and each replica's bus invokes its local listeners for events arriving
from the backend. The same instance can be shared across servers.
publish is async so backend implementations can do network I/O.
subscribe is synchronous local registration. Listeners are synchronous,
must not raise, and are invoked on the server's event loop.
Source code in src/mcp/server/subscriptions.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
publish
async
publish(event: ServerEvent) -> None
Deliver event to every subscribed listener.
Source code in src/mcp/server/subscriptions.py
85 86 87 | |
subscribe
subscribe(
listener: Callable[[ServerEvent], None],
) -> Callable[[], None]
Register listener and return an idempotent unsubscribe callable.
Source code in src/mcp/server/subscriptions.py
89 90 91 | |
Classes
InMemorySubscriptionBus— In-processSubscriptionBus: synchronous fan-out to listeners in subscription order.ListenHandler— Servessubscriptions/listen: one call is one subscription stream.PromptsListChanged— The server's prompt list changed.ResourcesListChanged— The server's resource list changed.ResourceUpdated— The resource aturichanged and may need to be read again.SubscriptionBus— Fan-out seam between event publishers and open listen streams.ToolsListChanged— The server's tool list changed.
Attributes
ServerEvent— An event a server publishes for delivery to listen subscribers.SUBSCRIPTION_ID_META_KEY— The_metakey on every listen-stream frame; the value is thesubscriptions/listenrequest's JSON-RPC id.