LangChain MCP Adapter throws "Connection closed" error when calling tools on Windows with npx stdio transport

I’m using the langchain_mcp_adapters library to integrate MCP (Model Context Protocol) filesystem tools with LangChain on Windows. The initial connection succeeds and lists tools correctly, but fails with a Connection closed error when attempting to call any tool.

from langchain.chat_models import init_chat_model
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage
from langchain_mcp_adapters.client import MultiServerMCPClient

MCP_CONFIG = {
    "filesystem": {
        "command": "cmd",
        "args": [
            "/c",
            "npx",
            "-y",
            "@modelcontextprotocol/server-filesystem",
            "{path}",
        ],
        "transport": "stdio",
    }
}

async def main():
    client = MultiServerMCPClient(MCP_CONFIG)
    tools = await client.get_tools()
    print(f"🔧 Tools available: {[t.name for t in tools]}")  
    
    model = init_chat_model(model="claude-3-opus")
    model_with_tools = model.bind_tools(tools)
    
    messages = [
        SystemMessage(content="You are a helpful assistant"),
        HumanMessage(content="List files in the directory")
    ]
    
    response = model_with_tools.invoke(messages)
    
    # Handle tool calls - THIS FAILS
    if response.tool_calls:
        tool_lookup = {t.name: t for t in tools}
        for call in response.tool_calls:
            result = await tool_lookup[call["name"]].ainvoke(call["args"])  # ❌ Error here

Output:

🔧 Tools available: ['read_file', 'read_text_file', 'read_media_file', 'read_multiple_files', 
'write_file', 'edit_file', 'create_directory', 'list_directory', 'list_directory_with_sizes', 
'directory_tree', 'move_file', 'search_files', 'get_file_info', 'list_allowed_directories']

ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
  | Traceback (most recent call last):
  |   File "langchain_mcp_adapters/tools.py", line 126, in call_tool
  |     call_tool_result = await cast("ClientSession", tool_session).call_tool(...)
  |   File "mcp/client/session.py", line 279, in call_tool
  |     result = await self.send_request(...)
  |   File "mcp/shared/session.py", line 286, in send_request
  |     raise McpError(response_or_error.error)
  | mcp.shared.exceptions.McpError: Connection closed

The issue appears to be that langchain_mcp_adapters creates a new ephemeral session for each tool call by spawning a new npx process, which closes before the tool execution completes.

The initial get_tools() succeeds because it maintains a persistent connection, but subsequent tool calls fail because they attempt to create new short-lived sessions.

Question:
Is there a way to force langchain_mcp_adapters to reuse the same session for all tool calls?

Hi @lreal

have you tries response = await model_with_tools.ainvoke(messages)?

Yes, same error