Skip to content

stdio_client not starting in windows: getting NotImplementedError #359

Closed

Description

@JexinSam

anyio.open_process in stdio_client not working in windows. It raises NotImplementedError.

async with stdio_client(server_params) as (read, write):
        async with ClientSession(
            read, write, sampling_callback=handle_sampling_message
        ) as session:
            # Initialize the connection
            await session.initialize()

It gives out error:

   async with stdio_client(server_param) as (read, write):
  File "C:\Python311\Lib\contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\mcp\client\stdio.py", line 104, in stdio_client
    process = await anyio.open_process(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\anyio\_core\_subprocesses.py", line 190, in open_process
    return await get_async_backend().open_process(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 2561, in open_process
    process = await asyncio.create_subprocess_exec(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\asyncio\base_events.py", line 1680, in subprocess_exec
    transport = await self._make_subprocess_transport(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\asyncio\base_events.py", line 502, in _make_subprocess_transport
    raise NotImplementedError
NotImplementedError

Is there a solution to this?

Activity

  1. Kludex commented on Mar 24, 2025

    @Kludex
    Member

    Hmmm... I think we need to choose the spawn mode somewhere...

  2. added theissue type on Mar 25, 2025
  3. saqadri commented on Mar 26, 2025

    @saqadri
    Contributor

    @dsp-ant I had several users report issues with mcp-agent on Windows at least in part due to this. I got around to spending several hours to fix this, and figured I'd contribute the fix back upstream (or downstream? 🙃 ). #372 should cover most cases.

  4. isakulaksiz commented on Mar 27, 2025

    @isakulaksiz

    NotImplementedError in asyncio subprocess_exec when using MCP client on Windows with Python 3.12 #369

  5. JexinSam commented on Mar 27, 2025

    @JexinSam
    Author

    @saqadri #372 PR still giving me the same error in windows: NotImplementedError

    return await anext(self.gen)
               ^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\__init__.py", line 113, in stdio_client
        process = await _create_platform_compatible_process(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\__init__.py", line 210, in _create_platform_compatible_process
        process = await create_windows_process(command, args, env, errlog, cwd)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\win32.py", line 85, in create_windows_process
        process = await anyio.open_process(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\anyio\_core\_subprocesses.py", line 190, in open_process
        return await get_async_backend().open_process(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 2561, in open_process
        process = await asyncio.create_subprocess_exec(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
        transport, protocol = await loop.subprocess_exec(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\base_events.py", line 1680, in subprocess_exec
        transport = await self._make_subprocess_transport(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\base_events.py", line 502, in _make_subprocess_transport
        raise NotImplementedError
    NotImplementedError
    
  6. saqadri commented on Mar 27, 2025

    @saqadri
    Contributor

    @saqadri #372 PR still giving me the same error in windows: NotImplementedError

    return await anext(self.gen)
               ^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\__init__.py", line 113, in stdio_client
        process = await _create_platform_compatible_process(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\__init__.py", line 210, in _create_platform_compatible_process
        process = await create_windows_process(command, args, env, errlog, cwd)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\mcp\client\stdio\win32.py", line 85, in create_windows_process
        process = await anyio.open_process(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\anyio\_core\_subprocesses.py", line 190, in open_process
        return await get_async_backend().open_process(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 2561, in open_process
        process = await asyncio.create_subprocess_exec(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
        transport, protocol = await loop.subprocess_exec(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\base_events.py", line 1680, in subprocess_exec
        transport = await self._make_subprocess_transport(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Python311\Lib\asyncio\base_events.py", line 502, in _make_subprocess_transport
        raise NotImplementedError
    NotImplementedError
    

    @JexinSam I'll investigate - is there anything special about your setup. For example are you running in a Jupyter notebook or regular Python script? A snippet and repro steps would be super helpful.

  7. JexinSam commented on Mar 28, 2025

    @JexinSam
    Author

    I am trying to run mcp client in fastapi server.

    Here is a sample code:

    mcp-server-config.json

    {
      "mcpServers": {
        "mysql": {
          "command": "python",
          "args": ["path/to/mcp-host/mysql-server"],
          "env": {},
          "transport": "stdio"
        }
      }
    }

    app.py

    from mcp import ClientSession, StdioServerParameters, types
    from typing import List
    from mcp.client.stdio import stdio_client
    import os, json
    from fastapi import FastAPI, HTTPException
    
    app = FastAPI()
    
    CONFIG_FILE = 'mcp-server-config.json'
    
    def load_server_config() -> dict:
        # Load server configuration from the config file
        if os.path.exists(CONFIG_FILE):
            with open(CONFIG_FILE, "r", encoding="utf-8") as f:
                return json.load(f)  # Load server configuration
        raise FileNotFoundError(f"Could not find config file {CONFIG_FILE}")
    
    
    def create_server_parameters(server_config: dict) -> List[StdioServerParameters]:
        """Create server parameters from the server configuration."""
        server_parameters = []
        # Create server parameters for each server configuration
        for config in server_config["mcpServers"].values():
            server_parameter = StdioServerParameters(
                command=config["command"],
                args=config.get("args", []),
                env={**config.get("env", {}), "PATH": os.getenv("PATH")}
            )
            # Add environment variables from the system if not provided
            for key, value in server_parameter.env.items():
                if len(value) == 0 and key in os.environ:
                    server_parameter.env[key] = os.getenv(key)
            server_parameters.append(server_parameter)
        return server_parameters
    
    async def get_mcp_tools(server_param: StdioServerParameters) -> List:
        """Asynchronously retrieves and converts tools from a server using specified parameters"""
        mcp_tools = []
    
        async with stdio_client(server_param) as (read, write):
            async with ClientSession(read, write) as session:
                await session.initialize()  # Initialize the session
                tools: types.ListToolsResult = await session.list_tools()  # Retrieve tools from the server
    
                for tool_data in tools.tools:
                    mcp_tools.append(tool_data.__dict__)
    
        return mcp_tools
    
    @app.get("/api/mcp-tools")
    async def get_mcp_tools_api():
        """API endpoint to retrieve MCP tools."""
        server_config = load_server_config()  # Load server configuration
        server_params = create_server_parameters(server_config)
        tools = await get_mcp_tools(server_params[0])  # Get tools from the first server
        return {"tools": tools}
    fastapi dev app.py
    # accessing /api/mcp-tools is resulting in NotImplementedError
    
  8. saqadri commented on Mar 28, 2025

    @saqadri
    Contributor

    @JexinSam thanks for the repro steps. I can try looking into this later today. In the meantime, can you please try setting this to see if it helps?

    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
    

    It is a hunch, but I noticed this issue in windows when using streamlit and jupyter as well, and at least for the streamlit case having the above resolved the issue.

  9. JexinSam commented on Mar 28, 2025

    @JexinSam
    Author

    Yes the issue is related to asyncio in windows. But the above code doesn't solve it for me.
    fastapi/fastapi#7770

    Turns out it happens only when --reload is enabled in fastapi.

  10. felixweinberger commented on Sep 26, 2025

    @felixweinberger
    Contributor

    Hi all, thanks for reporting this issue - we shipped several windows fixes in the last ~1-2 months especially around process management and termination.

    Curious if anyone here is still experiencing this? Otherwise would be closing this one soon.

  11. maxisbey commented on Mar 24, 2026

    @maxisbey
    Contributor

    Closing as stale — we asked for additional information and haven't heard back. If you're still experiencing this on the latest release, please reopen with a minimal reproducible example and we'll take another look.

    AI Disclaimer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds confirmationNeeds confirmation that the PR is actually required or needed.

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions