|
| 1 | +import importlib.util |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from langchain_core.messages import AIMessage |
| 6 | +from langchain_core.tools import BaseTool |
| 7 | + |
| 8 | +from langchain_mcp_adapters.client import MultiServerMCPClient |
| 9 | + |
| 10 | + |
| 11 | +def _load_module(module_name: str, server_path: str) -> any: |
| 12 | + module_spec = importlib.util.spec_from_file_location(module_name, server_path) |
| 13 | + assert module_spec is not None |
| 14 | + |
| 15 | + module = importlib.util.module_from_spec(module_spec) |
| 16 | + module_spec.loader.exec_module(module) |
| 17 | + return module |
| 18 | + |
| 19 | + |
| 20 | +async def test_multi_server_mcp_client( |
| 21 | + socket_enabled, |
| 22 | + websocket_server, |
| 23 | + websocket_server_port: int, |
| 24 | +): |
| 25 | + """Test that MultiServerMCPClient can connect to multiple servers and load tools.""" |
| 26 | + # Get the absolute path to the server scripts |
| 27 | + current_dir = Path(__file__).parent |
| 28 | + math_server_path = os.path.join(current_dir, "servers/math_server.py") |
| 29 | + weather_server_path = os.path.join(current_dir, "servers/weather_server.py") |
| 30 | + # import weather_server |
| 31 | + weather_server_module = _load_module("weather_server", weather_server_path) |
| 32 | + |
| 33 | + client = MultiServerMCPClient( |
| 34 | + { |
| 35 | + "math": { |
| 36 | + "command": "python3", |
| 37 | + "args": [math_server_path], |
| 38 | + "transport": "stdio", |
| 39 | + }, |
| 40 | + "weather": { |
| 41 | + "server": weather_server_module.mcp, |
| 42 | + "transport": "in_memory", |
| 43 | + }, |
| 44 | + }, |
| 45 | + ) |
| 46 | + # Check that we have tools from both servers |
| 47 | + all_tools = await client.get_tools() |
| 48 | + |
| 49 | + # Should have 3 tools (add, multiply, get_weather) |
| 50 | + assert len(all_tools) == 3 |
| 51 | + |
| 52 | + # Check that tools are BaseTool instances |
| 53 | + for tool in all_tools: |
| 54 | + assert isinstance(tool, BaseTool) |
| 55 | + |
| 56 | + # Verify tool names |
| 57 | + tool_names = {tool.name for tool in all_tools} |
| 58 | + assert tool_names == {"add", "multiply", "get_weather"} |
| 59 | + |
| 60 | + # Check math server tools |
| 61 | + math_tools = await client.get_tools(server_name="math") |
| 62 | + assert len(math_tools) == 2 |
| 63 | + math_tool_names = {tool.name for tool in math_tools} |
| 64 | + assert math_tool_names == {"add", "multiply"} |
| 65 | + |
| 66 | + # Check weather server tools |
| 67 | + weather_tools = await client.get_tools(server_name="weather") |
| 68 | + assert len(weather_tools) == 1 |
| 69 | + assert weather_tools[0].name == "get_weather" |
| 70 | + |
| 71 | + # Test that we can call a math tool |
| 72 | + add_tool = next(tool for tool in all_tools if tool.name == "add") |
| 73 | + result = await add_tool.ainvoke({"a": 2, "b": 3}) |
| 74 | + assert result == "5" |
| 75 | + |
| 76 | + # Test that we can call a weather tool |
| 77 | + weather_tool = next(tool for tool in all_tools if tool.name == "get_weather") |
| 78 | + result = await weather_tool.ainvoke({"location": "London"}) |
| 79 | + assert result == "It's always sunny in London" |
| 80 | + |
| 81 | + # Test the multiply tool |
| 82 | + multiply_tool = next(tool for tool in all_tools if tool.name == "multiply") |
| 83 | + result = await multiply_tool.ainvoke({"a": 4, "b": 5}) |
| 84 | + assert result == "20" |
| 85 | + |
| 86 | + |
| 87 | +async def test_get_prompt(): |
| 88 | + """Test retrieving prompts from MCP servers.""" |
| 89 | + # Get the absolute path to the server scripts |
| 90 | + current_dir = Path(__file__).parent |
| 91 | + math_server_path = os.path.join(current_dir, "servers/math_server.py") |
| 92 | + # import weather_server |
| 93 | + math_server_module = _load_module("math_server", math_server_path) |
| 94 | + |
| 95 | + client = MultiServerMCPClient( |
| 96 | + { |
| 97 | + "math": { |
| 98 | + "server": math_server_module.mcp, |
| 99 | + "transport": "in_memory", |
| 100 | + } |
| 101 | + }, |
| 102 | + ) |
| 103 | + # Test getting a prompt from the math server |
| 104 | + messages = await client.get_prompt( |
| 105 | + "math", |
| 106 | + "configure_assistant", |
| 107 | + arguments={"skills": "math, addition, multiplication"}, |
| 108 | + ) |
| 109 | + |
| 110 | + # Check that we got an AIMessage back |
| 111 | + assert len(messages) == 1 |
| 112 | + assert isinstance(messages[0], AIMessage) |
| 113 | + assert "You are a helpful assistant" in messages[0].content |
| 114 | + assert "math, addition, multiplication" in messages[0].content |
0 commit comments