Fix Windows subprocess NotImplementedError (STDIO clients) by theailanguage · Pull Request #596 · modelcontextprotocol/python-sdk

ihrpr

ihrpr previously requested changes May 28, 2025

felixweinberger added a commit that referenced this pull request

Jun 23, 2025
On Windows, asyncio.create_subprocess_exec raises NotImplementedError due to
incomplete subprocess transport support in ProactorEventLoop. This commit
implements a fallback using subprocess.Popen directly with async wrappers.

The fix introduces a DummyProcess class that wraps subprocess.Popen and
provides async-compatible stdin/stdout streams using anyio's FileReadStream
and FileWriteStream. This allows MCP STDIO clients to work on Windows
without encountering the NotImplementedError.

Based on PR #596 by @theailanguage

Github-Issue: #596
Reported-by: theailanguage

felixweinberger

@theailanguage

@theailanguage

@theailanguage

@theailanguage

…o_client on windows due to tee command issues
…est_stdio_client on windows due to tee command issues"

This reverts commit fef614d8cfc2b48ffa302f54257ec8d09ab26fbf.
…ogic

Revert "Merge branch 'main' into fix/windows_stdio_subprocess"

This reverts commit d3e097502338040e972930bdcc00fb73ffc82f73, reversing
changes made to 1c6c6fbdcfb191b7b5ee89a6762ad2337f61e4d0.

@theailanguage

… ResourceWarning by properly closing file handles in DummyProcess

@theailanguage

…ng a bad inline script. This ensures the subprocess fails to launch, which better matches the test's intent.
…sLookupError. This can happen if the command failed to launch properly (as in this test case).

felixweinberger

@theailanguage

@theailanguage

felixweinberger

ihrpr

felixweinberger added a commit that referenced this pull request

Jul 4, 2025
This restores the pre-#596 behavior where anyio.open_process is tried
first, falling back to subprocess.Popen only when necessary (e.g., when
using SelectorEventLoop on Windows).

The approach:
1. Try anyio.open_process with CREATE_NO_WINDOW flag
2. If NotImplementedError, fall back to subprocess.Popen wrapper
3. If other exception, retry anyio.open_process without flags

Also refactored to extract the fallback logic into a separate
_create_windows_fallback_process function for better code organization.

felixweinberger added a commit that referenced this pull request

Jul 4, 2025
This re-establishes behavior before #596 in the default case.

- Attempt to use anyio's native open_process function on Windows
- Fall back to subprocess.Popen only if NotImplementedError is raised
- This improves compatibility with event loops that support async subprocesses
- Extract fallback logic into separate function for clarity

felixweinberger added a commit that referenced this pull request

Jul 7, 2025
This re-establishes behavior before #596 in the default case.

- Attempt to use anyio's native open_process function on Windows
- Fall back to subprocess.Popen only if NotImplementedError is raised
- This improves compatibility with event loops that support async subprocesses
- Extract fallback logic into separate function for clarity