Skip to content

Send SEP-2243 Mcp-Method/Mcp-Name headers from the StreamableHTTP client#2730

Open
vidigoat wants to merge 1 commit into
modelcontextprotocol:mainfrom
vidigoat:fix/sep-2243-mcp-post-headers
Open

Send SEP-2243 Mcp-Method/Mcp-Name headers from the StreamableHTTP client#2730
vidigoat wants to merge 1 commit into
modelcontextprotocol:mainfrom
vidigoat:fix/sep-2243-mcp-post-headers

Conversation

@vidigoat
Copy link
Copy Markdown

@vidigoat vidigoat commented May 30, 2026

Summary

Fixes #2715. Per SEP-2243 (accepted — see the draft changelog), the Streamable HTTP transport requires Mcp-Method and Mcp-Name headers on POST requests so servers/intermediaries can route without parsing the JSON-RPC body. The Python client did not send them.

Changes

  • _set_mcp_request_headers() sets, for outgoing POST messages:
    • Mcp-Method: the JSON-RPC method — for requests and notifications
    • Mcp-Name: params.name (tools/prompts) or, when absent, params.uri (resources)
    • responses/errors (no method) get neither header
  • _encode_mcp_header_value() applies the SEP-2243 value-encoding rules to Mcp-Name: safe printable-ASCII values pass through; non-ASCII, control characters, or significant leading/trailing whitespace are wrapped as =?base64?<b64>?= (matching the spec's examples).

The encoding is not just spec-compliance — without it, a tool name/URI containing non-ASCII raises UnicodeEncodeError and breaks the request, and a value containing CR/LF is a header-injection vector. Verified both: café now sends as =?base64?Y2Fmw6k=?= and is accepted by httpx; "a\r\nEvil: 1" is base64-wrapped.

The broader x-mcp-header custom-header part of SEP-2243 is intentionally out of scope here (separate feature); this PR covers the Mcp-Method/Mcp-Name requirement from #2715.

Tests

  • test_set_mcp_request_headers — request with name, with uri, no params, non-string name, notification, response, error, and a non-ASCII name (end-to-end encoding).
  • test_encode_mcp_header_value — passthrough + each base64 trigger (non-ASCII, whitespace, control char, sentinel collision), using SEP-2243's own example values.

Full tests/shared/test_streamable_http.py passes (76 tests); ruff and pyright clean.

uv run --frozen pytest tests/shared/test_streamable_http.py

Authored with the assistance of an AI coding assistant; reviewed and verified by me.

@vidigoat vidigoat force-pushed the fix/sep-2243-mcp-post-headers branch from 1732572 to 8f1d353 Compare May 30, 2026 12:19
Per SEP-2243, MCP clients should send routing headers on POST requests so
spec-compliant servers and intermediaries can route without parsing the
JSON-RPC body. The StreamableHTTP client did not send them.

Add Mcp-Method (the JSON-RPC method, for requests and notifications) and
Mcp-Name (params.name for tools and prompts, else params.uri for resources)
to outgoing POST headers. Mcp-Name values are encoded per the SEP-2243 value
rules: safe printable-ASCII values are sent unchanged, while non-ASCII,
control characters, or significant leading/trailing whitespace are wrapped as
=?base64?<b64>?=. Besides matching the spec, this prevents a UnicodeEncodeError
when a tool name or URI contains non-ASCII characters, and neutralizes header
injection via CR/LF.

Responses and errors, which have no method, send neither header.

Fixes modelcontextprotocol#2715
@vidigoat vidigoat force-pushed the fix/sep-2243-mcp-post-headers branch from 8f1d353 to 5fb16c8 Compare May 30, 2026 13:18
@vidigoat
Copy link
Copy Markdown
Author

Updated to also apply SEP-2243's value-encoding rules to Mcp-Name. Without it, a tool name or URI containing non-ASCII characters raises UnicodeEncodeError and breaks the request, and CR/LF in a value is a header-injection vector. Now safe printable-ASCII values pass through unchanged and everything else is wrapped as =?base64?<b64>?=, matching the spec's examples. Added test_encode_mcp_header_value covering passthrough plus each base64 trigger (non-ASCII, leading/trailing whitespace, control char, sentinel collision).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SEP-2243: Send required Mcp-Method/Mcp-Name HTTP headers

1 participant