Skip to content

Expose optional stdin/stdout on MCPServer stdio entrypoints#2343

Open
mangelajo wants to merge 3 commits intomodelcontextprotocol:mainfrom
mangelajo:expose-mcpserver-stdio-streams
Open

Expose optional stdin/stdout on MCPServer stdio entrypoints#2343
mangelajo wants to merge 3 commits intomodelcontextprotocol:mainfrom
mangelajo:expose-mcpserver-stdio-streams

Conversation

@mangelajo
Copy link

@mangelajo mangelajo commented Mar 25, 2026

Summary

mcp.server.stdio.stdio_server already supports optional stdin and stdout. This PR threads those parameters through MCPServer.run_stdio_async and the stdio branch of MCPServer.run, so applications can use the public API instead of calling stdio_server manually and depending on internal wiring.

Motivation

MCP over stdio is JSON-RPC on a dedicated pair of streams. In real processes, fd 1 is also used for logging, print(), subprocesses, and native code. Any extra bytes on the protocol stream corrupt JSON-RPC and break clients.

A common pattern is to duplicate the real stdout fd for MCP-only I/O and redirect fd 1 (and often sys.stdout) to stderr so other output stays off the wire. That requires passing custom stdin/stdout into the same code path the server uses for stdio. stdio_server already accepts them; this exposes the same capability on MCPServer.

Testing

Adds tests/server/mcpserver/test_run_stdio_custom_streams.py, which asserts custom streams are forwarded to stdio_server (with stdio_server and low-level run stubbed).

Example consumer

Downstream servers jumpstarter-dev/jumpstarter#383 need this to keep logs and tooling output off the MCP stream while staying on supported APIs.
We have a lot of logging originating from gRPC and other libraries that we want to keep out of the channel, and we are using async io.

MCPServer.run_stdio_async and run(..., transport="stdio") now accept
optional async text streams, matching mcp.server.stdio.stdio_server.

This supports hosts that redirect process stdout for logging while
keeping a dedicated MCP JSON-RPC stream.

Made-with: Cursor
The SSE and streamable-http overloads declare transport-specific params
but the implementation catches them all via **kwargs.  Follow the same
pattern for stdin/stdout so the implementation signature stays uniform.

Made-with: Cursor
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