Skip to content
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

stdio_client not starting in windows: getting NotImplementedError #359

Open
JexinSam opened this issue Mar 24, 2025 · 8 comments
Open

stdio_client not starting in windows: getting NotImplementedError #359

JexinSam opened this issue Mar 24, 2025 · 8 comments
Labels
bug Something isn't working

Comments

@JexinSam
Copy link

JexinSam commented Mar 24, 2025

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?

@Kludex
Copy link
Member

Kludex commented Mar 24, 2025

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

@dsp-ant dsp-ant added the bug Something isn't working label Mar 25, 2025
@saqadri
Copy link
Contributor

saqadri commented Mar 26, 2025

@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.

@isakulaksiz
Copy link

isakulaksiz commented Mar 27, 2025

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

@JexinSam
Copy link
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

@saqadri
Copy link
Contributor

saqadri commented Mar 27, 2025

@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.

@JexinSam
Copy link
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

@saqadri
Copy link
Contributor

saqadri commented Mar 28, 2025

@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.

@JexinSam
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants