Skip to content

fix(stdio): handle bufferless text streams in fallback - #3575

Closed
sidinsearch wants to merge 1 commit into
modelcontextprotocol:mainfrom
sidinsearch:fix/stdio-bufferless-text-streams
Closed

sidinsearch wants to merge 1 commit into
modelcontextprotocol:mainfrom
sidinsearch:fix/stdio-bufferless-text-streams

Conversation

@sidinsearch

Copy link
Copy Markdown

Summary

Fixes a crash in stdio_server() when sys.stdin/sys.stdout are replaced with bufferless text streams (e.g., io.StringIO).

The _claim_fd function's fallback path unconditionally dereferences stream.buffer, but _is_backed_by_fd returns False when a stream has no .buffer attribute. This raises AttributeError before the transport serves a single message.

This is a distinct issue from the original #1933 (which was fixed by #3117). The original issue was about the wrapper closing the real stdio handles; this is about bufferless text streams (like io.StringIO) crashing the fallback path.

Changes

  • src/mcp/server/stdio.py: Check for the buffer attribute in _claim_fd's fallback path; return None when the stream is bufferless. The caller (stdio_server) then wraps the text stream in place since there is no binary layer to re-encode.

  • tests/server/test_stdio.py: Added test_stdio_server_serves_bufferless_text_streams_in_place which monkeypatches sys.stdin/sys.stdout to io.StringIO and verifies the transport serves messages correctly.

Test results

  • All 5971 tests pass
  • New test covers the regression case
  • Ruff and pyright clean

Disclosure

Written with AI assistance, reviewed and verified by me.

Fixes #1933

_claim_fd's fallback path dereferences stream.buffer unconditionally,
but _is_backed_by_fd returns False when a stream has no .buffer
attribute (e.g. io.StringIO). This raises AttributeError before the
transport serves a single message.

Fix by checking for the buffer attribute and returning None when the
stream is bufferless. The caller then wraps the text stream in place
since there is no binary layer to re-encode.

Closes modelcontextprotocol#1933

Co-authored-by: openhands <openhands@all-hands.dev>
@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 Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #1933.

If a maintainer assigns you to #1933, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using transport="stdio" closes real stdio, causing ValueError after server exits

1 participant