Skip to content

Commit c4ebb77

Browse files
g97iulio1609Copilot
andcommitted
fix(stdio): allow configurable memory-stream buffer size
Add read_stream_buffer_size and write_stream_buffer_size parameters to stdio_server() so callers can increase the internal memory-object-stream capacity. The default (0 = synchronous hand-off) is preserved for backward compatibility. With max_buffer_size=0 the stdin reader blocks whenever the message processor is busy, making the server unresponsive to pings and new requests during slow operations. A small buffer (e.g. 8) decouples reading from processing and lets the server remain responsive. Closes #1333 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 62575ed commit c4ebb77

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/mcp/server/stdio.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ async def run_server():
3030

3131

3232
@asynccontextmanager
33-
async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.AsyncFile[str] | None = None):
33+
async def stdio_server(
34+
stdin: anyio.AsyncFile[str] | None = None,
35+
stdout: anyio.AsyncFile[str] | None = None,
36+
read_stream_buffer_size: int = 0,
37+
write_stream_buffer_size: int = 0,
38+
):
3439
"""Server transport for stdio: this communicates with an MCP client by reading
3540
from the current process' stdin and writing to stdout.
3641
"""
@@ -49,8 +54,8 @@ async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.
4954
write_stream: MemoryObjectSendStream[SessionMessage]
5055
write_stream_reader: MemoryObjectReceiveStream[SessionMessage]
5156

52-
read_stream_writer, read_stream = anyio.create_memory_object_stream(0)
53-
write_stream, write_stream_reader = anyio.create_memory_object_stream(0)
57+
read_stream_writer, read_stream = anyio.create_memory_object_stream(read_stream_buffer_size)
58+
write_stream, write_stream_reader = anyio.create_memory_object_stream(write_stream_buffer_size)
5459

5560
async def stdin_reader():
5661
try:

0 commit comments

Comments
 (0)