Repository navigation
fix(sdk): reject header values that can't be sent with a clear error - #138
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sentry ZERNIO-MCP-11: an agent passed an
Idempotency-Keycontainingț(Romanian text) toposts_create_post. httpx only sends ASCII header values, so the request failed before it left with: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.pychecks 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 existingLateValidationError(field= the header name), with a message that names the header, the first offending character and its position, and how to fix it:token,authorization,secret,cookieorapi-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:
packages/cli/src/lib/custom-headers.ts: rejects header values outside Latin-1 or with CR/LF/NUL with a clearConfigError, added after incident CLI-31G.validate_ascii_metadata: raisesParamValidationErrorfor non-ASCII S3 metadata headers.Behaviour
retry\tkey)ț,é, emoji)UnicodeEncodeErrorfrom httpxLateValidationErrorwith a clear messageLateValidationErrorLocalProtocolErrorLateValidationError\x01, or DEL (\x7f)LateValidationErrorMCP effect
Generated tools already re-raise as
ToolError(f"Error: {e}") from e, andis_expected_failuretreats aToolErrorcaused by aLateErroras 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.pynow drops characters that aren't printable ASCII from the calling client's name, which the server copies into its own outboundUser-Agent. Starlette decodes an inbound UTF-8 User-Agent as latin-1, so a client name likeCaféwould otherwise hit the same httpx error on every call. Example:Café-țară/2.0becomeszernio-mcp (mcp; Caf-ar).Tests
New
tests/test_non_ascii_headers.py:LateValidationErrorbefore 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.X-Connect-Token) error shows neither the value nor the character.posts_create_postas a tool error and records no Sentry event. With the check disabled, the same test sees the old'ascii' codectext and one Sentry event.tests/test_mcp_user_agent.pygains a test for a non-ASCII client name.Local results on this head:
pytest tests -qgives 3867 passed, 44 skipped;mypy src --ignore-missing-importsreports no issues;ruff check src testspasses. The CHANGELOG has a Fixed entry under Unreleased.🤖 Generated with Claude Code