inbound
Inbound request classification for the modern per-request-envelope path.
Pure module: no I/O, no transport, no mcp.server imports. Runs the
validation ladder against a decoded JSON-RPC body and returns either an
:class:InboundModernRoute (every rung passed) or an
:class:InboundLadderRejection (the first rung that failed). Callers map a
rejection's code through :data:ERROR_CODE_HTTP_STATUS to pick the HTTP
status.
Also hosts the shared header-value codec and the x-mcp-header schema
validator so client emit and server validate read the same source of truth.
MCP_PROTOCOL_VERSION_HEADER
module-attribute
MCP_PROTOCOL_VERSION_HEADER: Final = 'mcp-protocol-version'
Canonical lowercase name of the HTTP header carrying the MCP protocol version.
MCP_METHOD_HEADER
module-attribute
MCP_METHOD_HEADER: Final = 'mcp-method'
Canonical lowercase name of the HTTP header carrying the JSON-RPC method.
MCP_NAME_HEADER
module-attribute
MCP_NAME_HEADER: Final = 'mcp-name'
Canonical lowercase name of the HTTP header carrying the resource name (tool/prompt name, resource URI, task id).
X_MCP_HEADER_KEY
module-attribute
X_MCP_HEADER_KEY: Final = 'x-mcp-header'
JSON-Schema property annotation that designates an Mcp-Param-* HTTP header.
NAME_BEARING_METHODS
module-attribute
NAME_BEARING_METHODS: Final[Mapping[str, str]] = (
MappingProxyType(
{
"tools/call": "name",
"prompts/get": "name",
"resources/read": "uri",
"tasks/get": "taskId",
"tasks/update": "taskId",
"tasks/cancel": "taskId",
}
)
)
Method → wire params key whose value is mirrored as the Mcp-Name HTTP header.
The first three rows are SEP-2243; the tasks/* rows are SEP-2663 §Streamable
HTTP: Routing Headers, which mandates Mcp-Name: <taskId> so intermediaries can
route task requests to the server instance holding the task's state.
Shared by client emit (which header to send) and server validate (which body field to compare against), so both ends agree on the field by construction.
encode_header_value
Wrap value in the =?base64?...?= sentinel when it would not survive an HTTP field round-trip.
Plain printable ASCII without leading/trailing whitespace passes verbatim; anything else (control chars, non-ASCII, edge whitespace, or a value that already looks like the sentinel) is base64-wrapped so the receiver can recover the exact bytes.
Source code in src/mcp/shared/inbound.py
155 156 157 158 159 160 161 162 163 164 165 | |
decode_header_value
Inverse of :func:encode_header_value.
Returns the value verbatim unless it carries the =?base64?...?= sentinel,
in which case the payload is decoded as UTF-8. A malformed sentinel (bad
base64, non-canonical base64, or bad UTF-8) yields None so a corrupt
header never matches a body value by accident. None in → None out so
callers can pass headers.get(...) directly.
Source code in src/mcp/shared/inbound.py
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 | |
find_invalid_x_mcp_header
Return a reason string if any x-mcp-header annotation in input_schema is invalid; else None.
Walks every JSON Schema 2020-12 schema position. An annotation is valid
only when it sits on a property statically reachable from the root via a
chain of pure properties keys, names a non-empty RFC 9110 token, is on
an integer/string/boolean property, and is case-insensitively unique
across the whole schema. A None / non-mapping schema has no schema
positions and returns None.
Source code in src/mcp/shared/inbound.py
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 | |
MCP_PARAM_HEADER_PREFIX
module-attribute
MCP_PARAM_HEADER_PREFIX: Final = 'Mcp-Param-'
Prefix the x-mcp-header token is joined to, forming the per-parameter HTTP header name.
x_mcp_header_map
Map each property carrying a valid x-mcp-header to its annotation token, keyed by property path.
The key is the chain of properties keys from the schema root to the
annotated property; a top-level property has a one-element path, a nested
one a longer path. Call only on a schema that
:func:find_invalid_x_mcp_header accepts; an invalid schema yields an
undefined subset.
Source code in src/mcp/shared/inbound.py
244 245 246 247 248 249 250 251 252 253 | |
mcp_param_headers
mcp_param_headers(
header_map: Mapping[tuple[str, ...], str],
arguments: Mapping[str, Any],
) -> dict[str, str]
Build the Mcp-Param-* headers a tools/call mirrors from its arguments.
For each (path, token) in header_map, read the value at that property
path in arguments and, when it is present and not None, emit
Mcp-Param-<token> carrying it: bool as true/false, other scalars via
str, each passed through :func:encode_header_value so a non-token value
is base64-wrapped. A path that hits a missing key or a non-mapping node is
skipped, matching the spec's "omit the header when no value is present",
as is a value with no header rendering.
Source code in src/mcp/shared/inbound.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
ERROR_CODE_HTTP_STATUS
module-attribute
ERROR_CODE_HTTP_STATUS: Final[Mapping[int, int]] = (
MappingProxyType(
{
PARSE_ERROR: 400,
INVALID_REQUEST: 400,
INVALID_PARAMS: 400,
HEADER_MISMATCH: 400,
MISSING_REQUIRED_CLIENT_CAPABILITY: 400,
UNSUPPORTED_PROTOCOL_VERSION: 400,
METHOD_NOT_FOUND: 404,
}
)
)
HTTP status to send for a JSON-RPC error.code.
Consulted for classifier-origin and handler-origin errors, so one table decides the wire status regardless of where the error was produced. Unmapped codes fall back to the caller's default (typically 200).
InboundModernRoute
dataclass
A modern-protocol request whose envelope passed every ladder rung.
client_info and client_capabilities are the raw envelope values;
the classifier checks presence only, not shape. Method existence is not a
ladder rung — kernel dispatch is the single source of truth for that.
Source code in src/mcp/shared/inbound.py
333 334 335 336 337 338 339 340 341 342 343 344 | |
InboundLadderRejection
dataclass
The first ladder rung that failed, as JSON-RPC error fields.
Source code in src/mcp/shared/inbound.py
347 348 349 350 351 352 353 | |
find_duplicated_routing_header
Name of a routing header supplied more than once in raw header lines, or None.
Takes raw (name, value) pairs — a folded mapping hides duplicates. A
duplicate is rejected because first-copy and last-copy readers would
disagree. Mcp-Param-* duplicates are :func:validate_mcp_param_headers's job.
Source code in src/mcp/shared/inbound.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
classify_inbound_request
classify_inbound_request(
body: Mapping[str, Any],
*,
headers: Mapping[str, str] | None = None,
supported_modern_versions: Sequence[
str
] = MODERN_PROTOCOL_VERSIONS
) -> InboundModernRoute | InboundLadderRejection
Run the modern-protocol validation ladder over a decoded JSON-RPC body.
Rungs, in order — first failure wins:
params._metais a mapping carrying every reserved envelope key (protocol version, client info, client capabilities) → else :data:~mcp_types.jsonrpc.INVALID_PARAMS.- When
headersis given,MCP-Protocol-Versionequals the envelope's protocol version,Mcp-Methodequalsbody.method, and — for the methods in :data:NAME_BEARING_METHODS—Mcp-Nameequals the named body param → else :data:~mcp_types.jsonrpc.HEADER_MISMATCH. Runs before the supported-version rung so a client that disagrees with itself is told so, rather than told the body's version is unsupported. - The envelope's protocol version is in
supported_modern_versions→ else :data:~mcp_types.jsonrpc.UNSUPPORTED_PROTOCOL_VERSIONwithdata = {"supported": [...], "requested": <value>}.
Method existence is not a rung: kernel dispatch owns that decision so custom-registered methods route and the answer lives in one place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
Mapping[str, Any]
|
The decoded JSON-RPC request mapping. Envelope shape
( |
required |
headers
|
Mapping[str, str] | None
|
Transport headers keyed by lowercase name, or |
None
|
supported_modern_versions
|
Sequence[str]
|
Modern protocol revisions this server accepts on the per-request-envelope path. |
MODERN_PROTOCOL_VERSIONS
|
Source code in src/mcp/shared/inbound.py
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
validate_mcp_param_headers
validate_mcp_param_headers(
input_schema: Any,
arguments: Mapping[str, Any],
headers: Mapping[str, str],
) -> InboundLadderRejection | None
Compare a tools/call request's Mcp-Param-* headers against its body arguments.
Each annotated property's header and argument must agree: present together
and equal after sentinel decoding, or absent together (null counts as
absent). Returns the first failure as a HEADER_MISMATCH rejection, else None.
A header whose argument is absent or unrenderable is deliberately rejected:
the spec's purpose clause is exactly an intermediary routing on a value the
body never carried. A duplicated recognized header is rejected — first-copy
and last-copy readers would disagree. A schema :func:find_invalid_x_mcp_header
rejects validates nothing: conforming clients drop the tool and emit no headers.
Source code in src/mcp/shared/inbound.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |