-
Notifications
You must be signed in to change notification settings - Fork 336
Description
Description:
I am encountering a Connection closed error when using MultiServerMCPClient to connect to a local MCP server installed in my current virtual environment.
I am running a standard python -m <package> command via the stdio transport. The package (mcp-server-fetch) is installed and verified working in the active virtual environment. However, when MultiServerMCPClient attempts to initialize the connection, it crashes immediately.
Reproduction Steps:
- Install a simple MCP server package (e.g.,
mcp-server-fetch) in a virtual environment. - Run the following client code from within the same virtual environment:
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
async def main():
client = MultiServerMCPClient(
{
"fetch": {
"transport": "stdio",
"command": "python",
"args": ["-m", "mcp_server_fetch"],
},
}
)
# This line raises the exception
tools = await client.get_tools()
print("Available tools:", tools)
if __name__ == "__main__":
asyncio.run(main())Expected Behavior:
The client should successfully start the subprocess using the active environment, import the module mcp_server_fetch, and retrieve the tools.
Actual Behavior:
The execution fails with a Connection closed error during initialization.
Error Log:
+ Exception Group Traceback (most recent call last):
...
| File "...\client.py", line 17, in main
| tools = await client.get_tools()
| ^^^^^^^^^^^^^^^^^^^^^^^^
...
| File "...\mcp\client\session.py", line 153, in initialize
| result = await self.send_request(
| ^^^^^^^^^^^^^^^^^^^^^^^^
| File "...\mcp\shared\session.py", line 288, in send_request
| raise McpError(response_or_error.error)
| mcp.shared.exceptions.McpError: Connection closed
Environment:
- OS: Windows (MINGW64)
- Python: 3.12.10
- Package:
mcp-server-fetch(installed in venv) - Library:
langchain-mcp-adapters
Additional Context:
- Running
pip show mcp-server-fetchconfirms the package is installed in the active.venv. - This issue seems related to how
_create_stdio_sessionhandles environment variables; if it passesenv=None(or an empty dict) to the subprocess, the virtual environment activation is lost, and thepythoncommand fails to find the installed module. - Same error when using the command
uvx mcp-server-fetch.