server
The server side of MCP Events (SEP-3415): Events, an MCPServer extension.
events = Events(webhooks=Webhooks())
incidents = events.event("incident.created", arguments=IncidentFilter, payload=Incident)
@incidents.match
def same_service(arguments: IncidentFilter, incident: Incident) -> bool:
return arguments.service == incident.service
mcp = MCPServer("pager", extensions=[events])
...
incidents.emit(Incident(service="billing", title="Latency spike"))
access_token_principal
access_token_principal(
ctx: ServerRequestContext[Any, Any],
) -> str | None
The caller's OAuth identity (client, issuer, subject), or None on an unauthenticated server.
Source code in src/mcp/experimental/events/server.py
78 79 80 81 | |
Events
Bases: Extension
Offer events to clients, delivered by poll and, with webhooks=, by webhook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
webhooks
|
Webhooks | None
|
Enables webhook delivery. Webhook calls need an authenticated principal. |
None
|
principal
|
PrincipalResolver
|
Who is calling. Subscriptions and poll leases are scoped to it. |
access_token_principal
|
poll_interval
|
timedelta
|
How long clients wait between polls, and how often webhook loops
run check functions. A check function's |
timedelta(seconds=5)
|
poll_lease
|
timedelta | None
|
How long after its last poll a poll subscription counts as ended,
firing |
None
|
max_events
|
int
|
The most events one poll returns when the client sets no |
100
|
buffer_size
|
int
|
How many emitted events each event type keeps for readers. |
1000
|
buffer_max_age
|
timedelta
|
How long an emitted event stays readable. |
timedelta(minutes=10)
|
Source code in src/mcp/experimental/events/server.py
84 85 86 87 88 89 90 91 92 93 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 | |
event
event(
name: str,
*,
arguments: type[ArgsT],
payload: type[PayloadT],
description: str | None = None
) -> EventType[ArgsT, PayloadT]
Declare an event type. arguments is what subscribers pass; payload is what each event carries.
Raises:
| Type | Description |
|---|---|
ValueError
|
An event type with this name already exists. |
Source code in src/mcp/experimental/events/server.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
Classes
EventBatch— What a check function answers: the events found, and the cursor after them.EventQuery— What a check function is asked: the events aftercursorthat matcharguments.Events— Offer events to clients, delivered by poll and, withwebhooks=, by webhook.EventType— An event type a server offers. Create it withEvents.event.NoArguments— The arguments of an event type that takes none; any argument is rejected.Occurrence— One event.idshould be the upstream's stable identifier, so clients can deduplicate.Subscriber— A subscription as the lifecycle hooks see it.Webhooks— Configuration for webhook delivery. Passing it toEventsoffers webhook on every event type.
Functions
access_token_principal— The caller's OAuth identity (client, issuer, subject), orNoneon an unauthenticated server.