session
ServerSession: server-to-client requests and notifications.
A thin proxy over JSONRPCDispatcher and Connection. One instance per
client connection (built by ServerRunner). Handlers reach it as
ctx.session and use the typed helpers (create_message, elicit_form,
send_log_message, ...) to call back to the client.
The receive-loop, initialize handling, and per-request task isolation that
used to live here are now owned by JSONRPCDispatcher and ServerRunner.
ServerSession
Connection-scoped proxy for server-to-client requests and notifications.
send_request / send_notification model-dump their argument and forward
to the dispatcher; the typed helpers below are unchanged from the previous
implementation and only call those two methods.
Source code in src/mcp/server/session.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
client_params
property
client_params: InitializeRequestParams | None
The client's initialize request params; None before initialization.
protocol_version
property
protocol_version: str | None
The protocol version negotiated during initialize.
None before initialization completes. Stateless connections don't
require the handshake, so this is normally None there (on streamable
HTTP the per-request version is the MCP-Protocol-Version header,
available via ctx.request.headers).
send_request
async
send_request(
request: ServerRequest,
result_type: type[ResultT],
request_read_timeout_seconds: float | None = None,
metadata: ServerMessageMetadata | None = None,
progress_callback: ProgressFnT | None = None,
) -> ResultT
Send a typed server-to-client request and validate the result.
metadata.related_request_id (when supplied) routes the outgoing
message onto the originating request's response stream over
streamable HTTP; it is the only metadata field honored here.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer responded with an error. |
NoBackChannelError
|
If there is no related request to ride on and the connection has no standalone channel (stateless HTTP), so a response could never arrive. |
ValidationError
|
The peer's result does not match |
Source code in src/mcp/server/session.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
send_notification
async
send_notification(
notification: ServerNotification,
related_request_id: RequestId | None = None,
) -> None
Send a typed server-to-client notification.
Source code in src/mcp/server/session.py
101 102 103 104 105 106 107 108 | |
check_client_capability
check_client_capability(
capability: ClientCapabilities,
) -> bool
Check if the client supports a specific capability.
Source code in src/mcp/server/session.py
110 111 112 | |
send_log_message
async
send_log_message(
level: LoggingLevel,
data: Any,
logger: str | None = None,
related_request_id: RequestId | None = None,
) -> None
Send a log message notification.
Source code in src/mcp/server/session.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
send_resource_updated
async
Send a resource updated notification.
Source code in src/mcp/server/session.py
133 134 135 136 137 138 139 | |
create_message
async
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: None = None,
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResult
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: list[Tool],
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResultWithTools
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: list[Tool] | None = None,
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResult | CreateMessageResultWithTools
Send a sampling/create_message request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[SamplingMessage]
|
The conversation messages to send. |
required |
max_tokens
|
int
|
Maximum number of tokens to generate. |
required |
system_prompt
|
str | None
|
Optional system prompt. |
None
|
include_context
|
IncludeContext | None
|
Optional context inclusion setting. Should only be set to "thisServer" or "allServers" if the client has sampling.context capability. |
None
|
temperature
|
float | None
|
Optional sampling temperature. |
None
|
stop_sequences
|
list[str] | None
|
Optional stop sequences. |
None
|
metadata
|
dict[str, Any] | None
|
Optional metadata to pass through to the LLM provider. |
None
|
model_preferences
|
ModelPreferences | None
|
Optional model selection preferences. |
None
|
tools
|
list[Tool] | None
|
Optional list of tools the LLM can use during sampling. Requires client to have sampling.tools capability. |
None
|
tool_choice
|
ToolChoice | None
|
Optional control over tool usage behavior. Requires client to have sampling.tools capability. |
None
|
related_request_id
|
RequestId | None
|
Optional ID of a related request. |
None
|
Returns:
| Type | Description |
|---|---|
CreateMessageResult | CreateMessageResultWithTools
|
The sampling result from the client. |
Raises:
| Type | Description |
|---|---|
MCPError
|
If tools are provided but client doesn't support them. |
ValueError
|
If tool_use or tool_result message structure is invalid. |
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.py
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 | |
list_roots
async
list_roots() -> ListRootsResult
Send a roots/list request.
Source code in src/mcp/server/session.py
255 256 257 258 259 260 261 262 | |
elicit
async
elicit(
message: str,
requested_schema: ElicitRequestedSchema,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a form mode elicitation/create request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to present to the user. |
required |
requested_schema
|
ElicitRequestedSchema
|
Schema defining the expected response structure. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response. |
Note
This method is deprecated in favor of elicit_form(). It remains for backward compatibility but new code should use elicit_form().
Source code in src/mcp/server/session.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
elicit_form
async
elicit_form(
message: str,
requested_schema: ElicitRequestedSchema,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a form mode elicitation/create request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to present to the user. |
required |
requested_schema
|
ElicitRequestedSchema
|
Schema defining the expected response structure. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response with form data. |
Raises:
| Type | Description |
|---|---|
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.py
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 | |
elicit_url
async
elicit_url(
message: str,
url: str,
elicitation_id: str,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a URL mode elicitation/create request.
This directs the user to an external URL for out-of-band interactions like OAuth flows, credential collection, or payment processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable explanation of why the interaction is needed. |
required |
url
|
str
|
The URL the user should navigate to. |
required |
elicitation_id
|
str
|
Unique identifier for tracking this elicitation. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response indicating acceptance, decline, or cancellation. |
Raises:
| Type | Description |
|---|---|
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.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 349 350 351 352 353 354 | |
send_ping
async
send_ping() -> EmptyResult
Send a ping request.
Source code in src/mcp/server/session.py
356 357 358 359 360 361 | |
send_progress_notification
async
send_progress_notification(
progress_token: str | int,
progress: float,
total: float | None = None,
message: str | None = None,
related_request_id: str | None = None,
) -> None
Send a progress notification.
Source code in src/mcp/server/session.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
send_resource_list_changed
async
send_resource_list_changed() -> None
Send a resource list changed notification.
Source code in src/mcp/server/session.py
384 385 386 | |
send_tool_list_changed
async
send_tool_list_changed() -> None
Send a tool list changed notification.
Source code in src/mcp/server/session.py
388 389 390 | |
send_prompt_list_changed
async
send_prompt_list_changed() -> None
Send a prompt list changed notification.
Source code in src/mcp/server/session.py
392 393 394 | |
send_elicit_complete
async
Send an elicitation completion notification.
This should be sent when a URL mode elicitation has been completed out-of-band to inform the client that it may retry any requests that were waiting for this elicitation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elicitation_id
|
str
|
The unique identifier of the completed elicitation |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this notification |
None
|
Source code in src/mcp/server/session.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |