Skip to content

encode MCP message headers with Base64#550

Merged
deathbyknowledge merged 7 commits intocloudflare:mainfrom
ainergiz:fix/mcp-header-encoding
Oct 10, 2025
Merged

encode MCP message headers with Base64#550
deathbyknowledge merged 7 commits intocloudflare:mainfrom
ainergiz:fix/mcp-header-encoding

Conversation

@ainergiz
Copy link
Copy Markdown
Contributor

@ainergiz ainergiz commented Oct 9, 2025

encode MCP message headers with Base64 to support unicode characters

Fixes #549

brief description - detailed explanation is in the issue:
JSON-RPC payloads with Unicode characters (emoji, em dashes, smart quotes) fail in HTTP headers. This happened when creating a ChatGPT app with agents library because ChatGPT sends requests with Unicode characters.

Solution:

  • Base64 encode cf-mcp-message header content
  • Add cf-mcp-message-encoding header to indicate encoding type
  • Maintain backward compatibility for unencoded messages

Changes:

  • src/mcp/utils.ts: encodes outgoing messages
  • src/mcp/index.ts: decodes incoming messages with fallback
  • src/mcp/transport.ts: clears encoding header
  • src/tests/mcp/transports/streamable-http.test.ts: added Unicode test

Trade-offs:

  • (small one with solution) Size increase: ~35% (e.g., 730 bytes → 971 bytes), still well within 8KB header limits
  • future optimisation related to this increase: could use Node.js's brand new zlib module for compression even if header size is not a concern

End to end test: tested the solution by tarballing my fork into existing mcp server and it worked with previous messages that has unicode characters

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Oct 9, 2025

🦋 Changeset detected

Latest commit: b600291

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Oct 9, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@550

commit: b600291

Copy link
Copy Markdown
Member

@deathbyknowledge deathbyknowledge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this. We can prune a lot of the code regarding format but this will fix the issue 👍

Copy link
Copy Markdown
Member

@deathbyknowledge deathbyknowledge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just needs to remove an unnecessary check.

@ainergiz
Copy link
Copy Markdown
Contributor Author

ainergiz commented Oct 9, 2025

thanks! I've just removed the message size check, hopefully should be good to go @deathbyknowledge

Copy link
Copy Markdown
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but I'll let @deathbyknowledge verify and land when he's up

@deathbyknowledge deathbyknowledge merged commit 336602f into cloudflare:main Oct 10, 2025
3 checks passed
@threepointone threepointone mentioned this pull request Oct 10, 2025
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.

4 participants