""" 第 2 步测试:连接本地 MCP Server,并直接调用 MCP 工具。 这一步还不接大模型,只验证: 1. MCP Client 能发现工具 2. MCP Client 能调用工具 3. MCP Server 能返回结果 运行: python step2_call_tool.py """ from __future__ import annotations import asyncio import sys from pathlib import Path from langchain_mcp_adapters.client import MultiServerMCPClient async def main() -> None: server_file = Path(__file__).with_name("my_mcp_demo.py").resolve() client = MultiServerMCPClient( { "travel-tools": { "transport": "stdio", "command": sys.executable, "args": [str(server_file)], } } ) tools = await client.get_tools() hotel_tool = next(tool for tool in tools if tool.name == "search_hotels") # 调用这个mcp工具 result = await hotel_tool.ainvoke( { "city": "重庆", "nights": 2, "budget_per_night": 400, } ) print("MCP 工具调用结果:") print(result) if __name__ == "__main__": asyncio.run(main())