Skip to content

fix(stdio): allow configurable memory stream buffer size#2215

Open
yaowubarbara wants to merge 3 commits intomodelcontextprotocol:mainfrom
yaowubarbara:fix/stdio-configurable-buffer-size
Open

fix(stdio): allow configurable memory stream buffer size#2215
yaowubarbara wants to merge 3 commits intomodelcontextprotocol:mainfrom
yaowubarbara:fix/stdio-configurable-buffer-size

Conversation

@yaowubarbara
Copy link

Summary

Add and parameters to to decouple the stdin reader from the message processor.

Problem

With the current hardcoded in , the stdin reader blocks on until the message processor calls . When processing is slow (database queries, API calls), the reader cannot accept new messages including pings, causing the server to become unresponsive. See #1333 for details.

Solution

  • Add two optional parameters with default value (preserving current behavior)
  • Pass them through to
  • Users can set to allow the reader to queue messages ahead of the processor

Test plan

  • Added : verifies basic functionality with non-zero buffer
  • Added : verifies buffered mode allows concurrent message reading during slow processing
  • Existing continues to pass (backward compatible)

Closes #1333

Add read_stream_buffer_size and write_stream_buffer_size parameters to stdio_server() to allow decoupling the stdin reader from the message processor. With the default buffer_size=0, the reader blocks on send() until the processor consumes the message, causing the server to become unresponsive during slow operations. A non-zero buffer allows the reader to queue messages ahead, preventing ping timeouts and request starvation.

Closes modelcontextprotocol#1333
The two new buffer tests did not close write_stream, causing
stdout_writer to block indefinitely on write_stream_reader iteration.
This prevented the task group from exiting, hanging the test until
CI timeout.
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.

stdio_server uses unbuffered memory streams which can cause server to block and become unresponsive

1 participant