Skip to content

Fix Windows subprocess NotImplementedError (STDIO clients) #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

theailanguage
Copy link

@theailanguage theailanguage commented Apr 28, 2025

Fix subprocess creation errors on Windows for STDIO client connections.
This updates create_windows_process to properly handle the case where asyncio cannot create subprocess transports on Windows by falling back to using the subprocess module manually.

Additionally, minor import sorting and style issues have been fixed according to ruff linter checks.


Motivation and Context

On Windows systems, MCP clients that connect via STDIO were failing with a NotImplementedError during anyio.open_process(). This is because asyncio on Windows does not implement full support for subprocess transports.

This change provides a clean workaround:

  • Attempt async process creation first.
  • If not supported, fall back to classic subprocess.Popen with asyncio wrapping.

This allows MCP STDIO clients (e.g., streamlit_client_ui.py) to work successfully on Windows without changing user workflows.

edit:
the default event loop comes out as (Windows 11, Python 3.13) -
<class 'asyncio.windows_events._WindowsSelectorEventLoop'>

My understanding is that #555 might work under the ProactorEventLoop, but my patch "might" help to support environments where that’s not the default

  • Frameworks like Streamlit, which may run their own event loop internally
  • Python 3.13 and above (where SelectorEventLoop is often the default on Windows)
  • CI setups or test runners that don’t customize the loop policy
    (note - personally I have faced this with my Streamlit UI only)

How Has This Been Tested?

  • Tested manually on Windows 11, Python 3.13.
  • Ran streamlit_client_ui.py to launch the MCP client.
  • Successfully connected to STDIO MCP servers without errors.
  • Validated tool loading and interaction without subprocess creation issues.
  • Verified no new ruff or pyright issues for the updated file.

Breaking Changes

No breaking changes.

  • The fallback mechanism is backward compatible.
  • Linux/macOS users are unaffected.
  • Windows users gain compatibility without needing to change their code.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update (style improvements, minor doc updates)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This fix is especially important for making MCP tools and agents accessible on all platforms, including Windows, thereby helping broader adoption.

@theailanguage
Copy link
Author

Hi team!

This is my first pull request. PR fixes Windows compatibility for MCP STDIO clients by introducing a fallback to subprocess.Popen wrapped with async I/O streams. It ensures that Windows users can now use streamlit and similar STDIO clients without errors.

Please let me know if you'd like any further changes. Thank you for your time and for maintaining such an awesome project!

Regards
[Kartik / theailanguage]

@theailanguage
Copy link
Author

@dsp-ant @jerome3o-anthropic would appreciate any kind of feedback on this if possible from your end. Thanks a ton!

Regards
[Kartik / theailanguage]

@ihrpr ihrpr added this to the r-05-25 milestone Apr 29, 2025
@BenMawnMahlauNBTC
Copy link

#555 solves this in a much cleaner way

@theailanguage
Copy link
Author

theailanguage commented May 7, 2025

#555 solves this in a much cleaner way

Thanks for sharing 🙏 I just tested it on my setup (Windows 11 + Python 3.13), and I’m still hitting the same error:
raise NotImplementedError

I think I have tested #555 well by updating the init and win32 python files in my .venv mcp path with the edits suggested here.

Baically #555 still uses await anyio.open_process(...) in both primary and fallback paths, which internally relies on asyncio.create_subprocess_exec(). Unfortunately, that call raises NotImplementedError on my machine, so the fallback logic never helps.

In my fork, I catch the failure and bypass the entire async subprocess path by:
Falling back to a sync subprocess.Popen
Then manually wrapping stdin, stdout in FileWriteStream and FileReadStream
This makes it work regardless of the event loop or OS internals

So for Windows users with newer Python versions or CI environments, my fallback is the only one that works when the async event loop doesn’t support subprocesses.

Let me know what you think. Have you tested this in event loop scenarios like the one pointed out in this PR?

Thanks
Kartik / theailanguage

@BenMawnMahlauNBTC
Copy link

BenMawnMahlauNBTC commented May 7, 2025

I am on windows 11; also using it in an event loop. are you using windows proactor for the async loop?

edit:
I also have this change in the version i am using

mcp\client\session.py,

import asyncio
import os
if os.name == 'nt':
  asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

@theailanguage
Copy link
Author

theailanguage commented May 7, 2025

I am on windows 11; also using it in an event loop. are you using windows proactor for the async loop?

edit: I also have this change in the version i am using

mcp\client\session.py,

import asyncio
import os
if os.name == 'nt':
  asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

Aah that explains it :)

For me it is
<class 'asyncio.windows_events._WindowsSelectorEventLoop'>

This I think is the default.

I actually tried to add your session.py code but cannot get the event loop to change for some reason (I guess due to Streamlit's own eventloop).

I understand #555 might work perfectly under the ProactorEventLoop, but this patch "might" help to support environments where that’s not the default

  • Frameworks like Streamlit, which may run their own event loop internally
  • Python 3.13 and above (where SelectorEventLoop is often the default on Windows)
  • CI setups or test runners that don’t customize the loop policy

(note - personally I have faced this with my Streamlit UI only)

That said, I really appreciate the clean structure of the other solution.

Thanks
Kartik / theailanguage

@theailanguage
Copy link
Author

One more thing, in case you'd want to look at the implementation of the streamlit code - you can have a look at

Repo - https://github.com/theailanguage/mcp_client
UI code - https://github.com/theailanguage/mcp_client/blob/main/streamlit_client_ui.py

This is a tutorial video for the same that I built for a course
https://youtu.be/Ln-Tgz8Pmek

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.

3 participants