connection
Connection - per-client connection state and the standalone outbound channel.
Always present on Context (never None), even in stateless deployments.
Holds peer info, per-connection scratch state and an exit_stack for
teardown, and an Outbound for the standalone stream (the SSE GET stream in
streamable HTTP, or the single duplex stream in stdio).
Construct via the factories: Connection.from_envelope for the 2026-era
single-exchange path (born ready, no back-channel) and Connection.for_loop
for the handshake-driven loop path. Both populate protocol_version so the
kernel reads it as a fact.
notify is best-effort: it never raises. If there's no standalone channel
or the stream has been dropped, the notification is debug-logged and silently
discarded - server-initiated notifications are inherently advisory.
send_raw_request raises NoBackChannelError when there's no channel; ping
is the only spec-sanctioned standalone request.
Connection
Per-client connection state and standalone-stream Outbound.
Construct via from_envelope (modern single-exchange: born ready, no
back-channel) or for_loop (handshake-driven: ready once the client's
notifications/initialized arrives). Either way protocol_version is
populated at construction.
Source code in src/mcp/server/connection.py
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
outbound
instance-attribute
outbound: Outbound = outbound
The connection-scoped channel for server-initiated messages.
protocol_version
instance-attribute
protocol_version: str = protocol_version
The protocol version this connection speaks. Populated at construction
by the factory and overwritten by _handle_initialize once the handshake
commits on the loop path.
client_params
instance-attribute
client_params: InitializeRequestParams | None = (
client_params
)
The full initialize request params, or the equivalent built from the
2026-era envelope. None when no client info was supplied.
initialized
instance-attribute
initialized: Event = Event()
Set when notifications/initialized arrives (matches TS oninitialized);
the point from which the spec permits server-initiated requests beyond
ping/logging. Pre-set on connections built via from_envelope.
state
instance-attribute
Per-connection scratch state; persists across requests on this connection.
exit_stack
instance-attribute
exit_stack: AsyncExitStack = AsyncExitStack()
Per-connection teardown, unwound LIFO (shielded) when the connection closes. Push cleanup from handlers or middleware; exceptions are logged and swallowed.
from_envelope
classmethod
from_envelope(
protocol_version: str,
client_info: Implementation | None,
client_capabilities: ClientCapabilities | None,
*,
outbound: Outbound = _NO_CHANNEL
) -> Connection
A born-ready connection populated from a request's _meta envelope.
initialized is set and the envelope's client info/capabilities (when
both supplied) are recorded as client_params so capability checks
work. outbound defaults to the no-channel sentinel for the
single-exchange HTTP path; duplex modern transports (e.g. stdio) pass
the dispatcher so server-initiated messages have a back-channel.
Source code in src/mcp/server/connection.py
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 | |
for_loop
classmethod
for_loop(
outbound: Outbound,
*,
session_id: str | None = None,
protocol_version_hint: str | None = None
) -> Connection
A connection for the handshake-driven loop path.
Not born-ready: initialized is set later by the kernel when
notifications/initialized arrives. protocol_version is seeded from
the transport hint (or LATEST_PROTOCOL_VERSION) so it's never None;
the handshake overwrites it once negotiated.
Source code in src/mcp/server/connection.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
has_standalone_channel
property
has_standalone_channel: bool
Whether this connection has a real back-channel for server-initiated
messages. Derived from outbound - the no-channel sentinel is the only
case that doesn't.
initialize_accepted
property
initialize_accepted: bool
True once the inbound request gate is open: initialize recorded the
peer info, or the handshake completed outright (born-ready, or a bare
notifications/initialized). Derived, never stored.
send_raw_request
async
send_raw_request(
method: str,
params: Mapping[str, Any] | None,
opts: CallOptions | None = None,
) -> dict[str, Any]
Send a raw request on the standalone stream.
Low-level Outbound channel. Prefer the typed send_request or the
convenience methods below; use this directly only for off-spec
messages. opts carries per-call timeout / on_progress /
resumption hints; see CallOptions.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer responded with an error. |
NoBackChannelError
|
|
Source code in src/mcp/server/connection.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
send_request
async
send_request(
req: CreateMessageRequest,
*,
opts: CallOptions | None = None
) -> CreateMessageResult
send_request(
req: ElicitRequest, *, opts: CallOptions | None = None
) -> ElicitResult
send_request(
req: ListRootsRequest,
*,
opts: CallOptions | None = None
) -> ListRootsResult
send_request(
req: PingRequest, *, opts: CallOptions | None = None
) -> EmptyResult
send_request(
req: Request[Any, Any],
*,
result_type: type[ResultT],
opts: CallOptions | None = None
) -> ResultT
send_request(
req: Request[Any, Any],
*,
result_type: type[BaseModel] | None = None,
opts: CallOptions | None = None
) -> BaseModel
Send a typed server-to-client request and return its typed result.
For spec request types the result type is inferred. For custom requests
pass result_type= explicitly.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer responded with an error. |
NoBackChannelError
|
No back-channel for server-initiated requests. |
ValidationError
|
The peer's result does not match the expected result type. |
KeyError
|
|
Source code in src/mcp/server/connection.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
notify
async
Send a best-effort notification on the standalone stream.
Never raises. If there's no standalone channel or the stream is broken, the notification is dropped and debug-logged.
Source code in src/mcp/server/connection.py
278 279 280 281 282 283 284 285 286 287 | |
ping
async
ping(
*,
meta: Meta | None = None,
opts: CallOptions | None = None
) -> None
Send a ping request on the standalone stream.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer responded with an error. |
NoBackChannelError
|
|
Source code in src/mcp/server/connection.py
289 290 291 292 293 294 295 296 | |
log
async
log(
level: LoggingLevel,
data: Any,
logger: str | None = None,
*,
meta: Meta | None = None
) -> None
Send a notifications/message log entry on the standalone stream. Best-effort.
Source code in src/mcp/server/connection.py
298 299 300 301 302 303 304 | |
check_capability
check_capability(capability: ClientCapabilities) -> bool
Return whether the connected client declared the given capability.
Returns False when no client info has been recorded.
Source code in src/mcp/server/connection.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |