Skip to content

mcp: reject unencoded non-ASCII characters in Mcp-Param headers - #1521

Merged
guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
luiacuaniello:fix-mcp-param-invalid-chars
Nov 13, 2026
Merged

guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
luiacuaniello:fix-mcp-param-invalid-chars

Conversation

@luiacuaniello

Copy link
Copy Markdown
Contributor

Fixes #1520.

validateParamHeaders decodes an Mcp-Param-{Name} value when it contains the =?base64?...?= wrapper, and otherwise compares it directly with the body value. Therefore, with protocol version 2026-07-28, a value with characters outside visible ASCII, sent unencoded, is accepted if it matches the body argument: Mcp-Param-Region: café with "region": "café" runs the tool. The specification requires that such values be Base64-encoded, and that servers reject a recognized Mcp-Param-{Name} header containing invalid characters (Server Behavior for Custom Headers).

The value is now rejected before decoding if it contains any byte other than visible ASCII, space or horizontal tab (RFC 9110, section 5.5), with the same 400 status and -32020 (HeaderMismatch) error as the other header checks. A tab within the value is still accepted: it is a valid character in a field value, and the TypeScript SDK sends it unencoded. Base64-encoded values behave as before.

The conformance check for this requirement, sep-2243-server-reject-invalid-param-chars, only sends invalid Base64, which the server already rejects; this is why it passes on main.

Tests:

  • TestValidateParamHeaders_InvalidCharacters: unencoded non-ASCII characters and an unencoded control character are rejected; a non-ASCII value encoded in Base64 and a value containing a space and a tab are accepted. Without the change, both rejection cases fail.

go test ./..., go test -race on mcp and auth, go vet ./..., staticcheck v0.6.1 and the server conformance suite (0.2.0-alpha.11, both modes as in CI, with the baseline) pass, with the same conformance results as on main.

AI disclosure: I found this and prepared the fix with Claude Code; I reviewed the change and can explain it.

validateParamHeaders compared an Mcp-Param-{Name} value without the
=?base64?...?= wrapper directly with the body value, so a value with
characters outside visible ASCII would pass whenever it matched the
body argument.

The 2026-07-28 specification requires such values to be Base64-encoded,
and servers to reject a recognized Mcp-Param header that contains
invalid characters.

Reject a value containing any byte other than visible ASCII, space or
horizontal tab before decoding it, with the usual header mismatch
error -32020.

Fixes modelcontextprotocol#1520
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 12, 2026
@github-actions github-actions Bot closed this Nov 12, 2026
@github-actions github-actions Bot reopened this Nov 13, 2026
@github-actions github-actions Bot removed the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 13, 2026
@guglielmo-san
guglielmo-san merged commit 0fa5e84 into modelcontextprotocol:main Nov 13, 2026
18 checks passed
Tangerg added a commit to Tangerg/go-sdk that referenced this pull request Dec 2, 2026
Brings the fork up to upstream main at 723d70e, which is v1.9.0 plus
fourteen later commits. Upstream changes: modelcontextprotocol#1238, modelcontextprotocol#1511, modelcontextprotocol#1515, modelcontextprotocol#1517,
modelcontextprotocol#1521, modelcontextprotocol#1523, modelcontextprotocol#1524, modelcontextprotocol#1527, modelcontextprotocol#1533, modelcontextprotocol#1537, modelcontextprotocol#1539, modelcontextprotocol#1540, modelcontextprotocol#1544, modelcontextprotocol#1547,
modelcontextprotocol#1549, modelcontextprotocol#1550, modelcontextprotocol#1553, modelcontextprotocol#1554, modelcontextprotocol#1558, modelcontextprotocol#1560, modelcontextprotocol#1561, modelcontextprotocol#1562.

Fork-side adaptations:

- internal/jsonrpc2: the -32600 reply to a duplicate in-flight ID
  (modelcontextprotocol#1550) is written off the read loop and holds its admission slot
  until written, like every other reply under MaxIncomingRequests.
- mcp: ClientSession.initialize takes the session state lock that
  upstream added, and still projects the negotiated version to the
  connection.
- mcp: the fork's copies of hintomitempty, nowrapinvalidparams and
  nomethodnotfoundcodeinerror go away with upstream's removal (modelcontextprotocol#1533).
- extensions/skills: reads the protocol version from the fork's
  json.RawMessage Meta.
- Tests: the duplicate-ID admission test drains the new reply, and the
  client cancellation test runs on 2025-11-25 only, because 2026-07-28
  clients reject every standalone server request (modelcontextprotocol#1537, modelcontextprotocol#1540, modelcontextprotocol#1554).
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.

mcp: streamable server accepts unencoded non-ASCII in Mcp-Param headers

2 participants