-
Notifications
You must be signed in to change notification settings - Fork 810
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
Comments
Hmmm... I think we need to choose the spawn mode somewhere... |
NotImplementedError in asyncio subprocess_exec when using MCP client on Windows with Python 3.12 #369 |
@saqadri #372 PR still giving me the same error in windows: 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. |
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}
|
@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?
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. |
Yes the issue is related to asyncio in windows. But the above code doesn't solve it for me. Turns out it happens only when --reload is enabled in fastapi. |
anyio.open_process in stdio_client not working in windows. It raises NotImplementedError.
It gives out error:
Is there a solution to this?
The text was updated successfully, but these errors were encountered: