Skip to content

fix(stdio): preserve multi-byte UTF-8 across chunk boundaries - #2041

Closed
fmunteanu wants to merge 3 commits into
modelcontextprotocol:v1.xfrom
fmunteanu:fix/readbuffer-stringdecoder-v1
Closed

fix(stdio): preserve multi-byte UTF-8 across chunk boundaries#2041
fmunteanu wants to merge 3 commits into
modelcontextprotocol:v1.xfrom
fmunteanu:fix/readbuffer-stringdecoder-v1

Conversation

@fmunteanu

@fmunteanu fmunteanu commented May 9, 2026

Copy link
Copy Markdown

Fixes UTF-8 corruption in stdio when multi-byte sequences split across chunk boundaries.

Motivation and Context

ReadBuffer decodes with Buffer.toString('utf8', ...), which produces replacement characters when a multi-byte sequence (em-dash, emoji) splits across chunks. The corrupted bytes break JSON.parse and clients see misleading "expected object, received undefined" Zod errors pointing at the wrong field.

How Has This Been Tested?

Swapped Buffer.toString for TextDecoder in streaming mode, which holds incomplete multi-byte sequences across chunks. Added tests for em-dash, 3-byte emoji, 4-byte emoji, and byte-by-byte delivery. All stdio tests pass locally.

Breaking Changes

None. Public API unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

TextDecoder is a Web Standards API (no new dependency) used by node:readline for the same reason.

@fmunteanu
fmunteanu requested a review from a team as a code owner May 9, 2026 23:00
@changeset-bot

changeset-bot Bot commented May 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 503444a

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

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

pkg-pr-new Bot commented May 9, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@2041

commit: 503444a

@fmunteanu fmunteanu closed this by deleting the head repository May 9, 2026
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