| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- """
- 第 3 步测试:一个 MCP Server 暴露多个工具,并分别调用它们。
- 运行:
- python step3_call_multiple_tools.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()
- tool_map = {tool.name: tool for tool in tools}
- print("MCP Client 发现了这些工具:")
- for tool_name in tool_map:
- print(f"- {tool_name}")
- hotel_result = await tool_map["search_hotels"].ainvoke(
- {
- "city": "重庆",
- "nights": 2,
- "budget_per_night": 400,
- }
- )
- transport_result = await tool_map["search_transport"].ainvoke(
- {
- "origin": "成都",
- "destination": "重庆",
- "travel_date": "2026-08-15",
- }
- )
- print("\n住宿工具返回:")
- print(hotel_result)
- print("\n交通工具返回:")
- print(transport_result)
- if __name__ == "__main__":
- asyncio.run(main())
|