Skip to content

fix(sdk): reject header values that can't be sent with a clear error - #138

Merged
Ooscaar merged 1 commit into
developfrom
mcp-watch/non-ascii-headers
Nov 27, 2026
Merged

Ooscaar merged 1 commit into
developfrom
mcp-watch/non-ascii-headers

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

Sentry ZERNIO-MCP-11: an agent passed an Idempotency-Key containing ț (Romanian text) to posts_create_post. httpx only sends ASCII header values, so the request failed before it left with:

ToolError: Error: 'ascii' codec can't encode character 'ț' in position 35: ordinal not in range(128)

The agent got an opaque message and Sentry recorded it as a bug. Any per-request header the caller controls (Idempotency-Key, x-request-id, If-Match, X-Connect-Token, X-Temp-Token) could hit this, and a leading or trailing space failed the same way inside h11 (LocalProtocolError: Illegal header value).

Fix

src/late/client/base.py checks every per-request header value before the request is built, on the sync and async paths (in _with_request_id, ahead of the retry loop), so every generated resource is covered. A value that httpx/h11 can't send raises the existing LateValidationError (field = the header name), with a message that names the header, the first offending character and its position, and how to fix it:

Idempotency-Key header value must contain only printable ASCII characters (no accents, emoji, line breaks or leading/trailing spaces); got a value with 'ț' at position 36. Use a plain ASCII key, e.g. a UUID.
  • No value is changed. Anything that passes is sent exactly as before; no hashing or encoding.
  • Values are never echoed. Only a position and the character are shown. For token-like headers (name contains token, authorization, secret, cookie or api-key), not even the character: got a value with a character that cannot be sent at position 4.

The standard this follows

Most SDKs (stripe-python, Adyen, Twilio, the Stainless-generated openai/anthropic clients) pass caller headers through and let the HTTP library fail with a low-level error. The ones that handle it explicitly validate early with a clear error, which is what this does:

  • Sentry CLI, packages/cli/src/lib/custom-headers.ts: rejects header values outside Latin-1 or with CR/LF/NUL with a clear ConfigError, added after incident CLI-31G.
  • botocore validate_ascii_metadata: raises ParamValidationError for non-ASCII S3 metadata headers.

Behaviour

Header value Before After
Printable ASCII, incl. internal spaces or tabs (e.g. a UUID, retry\tkey) sent sent unchanged
Non-ASCII (ț, é, emoji) UnicodeEncodeError from httpx LateValidationError with a clear message
CR, LF, NUL httpx/h11 error LateValidationError
Leading or trailing space or tab h11 LocalProtocolError LateValidationError
Rare control characters such as \x01, or DEL (\x7f) sent (h11 allows them) now rejected with LateValidationError

MCP effect

Generated tools already re-raise as ToolError(f"Error: {e}") from e, and is_expected_failure treats a ToolError caused by a LateError as expected. So the agent now gets the clear message as a normal tool error (isError) it can act on, and no Sentry issue is opened. No MCP tool code changed.

User-Agent client name

src/late/mcp/user_agent.py now drops characters that aren't printable ASCII from the calling client's name, which the server copies into its own outbound User-Agent. Starlette decodes an inbound UTF-8 User-Agent as latin-1, so a client name like Café would otherwise hit the same httpx error on every call. Example: Café-țară/2.0 becomes zernio-mcp (mcp; Caf-ar).

Tests

New tests/test_non_ascii_headers.py:

  • ASCII keys, including one with an internal tab, reach the API unchanged.
  • Non-ASCII, CR/LF, NUL, and leading or trailing whitespace each raise LateValidationError before any request is sent (the mocked route is never called). The message names the header, gives the position and fix, and never contains the value.
  • A token header (X-Connect-Token) error shows neither the value nor the character.
  • The async path checks headers too.
  • The hosted MCP server (with telemetry) returns the clear message from posts_create_post as a tool error and records no Sentry event. With the check disabled, the same test sees the old 'ascii' codec text and one Sentry event.

tests/test_mcp_user_agent.py gains a test for a non-ASCII client name.

Local results on this head: pytest tests -q gives 3867 passed, 44 skipped; mypy src --ignore-missing-imports reports no issues; ruff check src tests passes. The CHANGELOG has a Fixed entry under Unreleased.

🤖 Generated with Claude Code

httpx only sends ASCII header values and h11 refuses leading or trailing
whitespace, so an agent-supplied Idempotency-Key holding human text
("...client-ț") failed with a bare UnicodeEncodeError, which the MCP tool
wrapped as "Error: 'ascii' codec can't encode character..." and Sentry
recorded as a bug (ZERNIO-MCP-11).

The SDK now checks every per-request header value before the request is
built, sync and async, and raises LateValidationError naming the header,
the first offending character and its position, and how to fix it. It never
echoes the value, and for token headers not even the character. Values are
never transformed: printable ASCII with spaces or tabs between characters is
sent exactly as before. Generated MCP tools wrap the error as a ToolError
caused by a LateError, so the agent gets the message and the telemetry treats
it as an expected failure with no Sentry issue.

The MCP server's outbound User-Agent also drops non-ASCII characters from
the calling client's name: Starlette decodes an inbound UTF-8 User-Agent as
latin-1, which would hit the same httpx error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit a3712c1 into develop Nov 27, 2026
4 checks passed
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.

1 participant