step1_list_tools.py 903 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. 第 1 步测试:连接本地 MCP Server,并列出它提供的工具。
  3. 运行:
  4. python step1_list_tools.py
  5. """
  6. from __future__ import annotations
  7. import asyncio
  8. import sys
  9. from pathlib import Path
  10. from langchain_mcp_adapters.client import MultiServerMCPClient
  11. async def main() -> None:
  12. server_file = Path(__file__).with_name("my_mcp_demo.py").resolve()
  13. # MCP Client 会自动启动 my_mcp_demo.py,然后询问它有哪些工具。
  14. client = MultiServerMCPClient(
  15. {
  16. "travel-tools": {
  17. "transport": "stdio",
  18. "command": sys.executable,
  19. "args": [str(server_file)],
  20. }
  21. }
  22. )
  23. tools = await client.get_tools()
  24. print("MCP Client 发现了这些工具:")
  25. for tool in tools:
  26. print(f"- {tool.name}: {tool.description}")
  27. if __name__ == "__main__":
  28. asyncio.run(main())