client_raw.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import asyncio
  2. import logging
  3. from python_a2a.mcp import MCPClient
  4. # 配置日志
  5. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  6. logger = logging.getLogger(__name__)
  7. async def test_mcp_tools():
  8. # 连接到服务端,端口 8010
  9. client = MCPClient("http://localhost:8010")
  10. try:
  11. # 步骤 1:获取可用工具列表
  12. tools = await client.get_tools()
  13. logger.info("可用工具列表:")
  14. for tool in tools:
  15. print(tool)
  16. logger.info(f"- {tool.get('name', '未知')}: {tool.get('description', '无描述')}")
  17. # 步骤 2:调用查询高频问题工具
  18. result_qhf = await client.call_tool("query_high_frequency_question")
  19. logger.info(f"高频问题查询结果:{result_qhf}")
  20. # 步骤 3:调用查询天气工具
  21. result_weather = await client.call_tool("get_weather")
  22. logger.info(f"天气查询结果:{result_weather}")
  23. except Exception as e:
  24. logger.error(f"MCP 客户端出错:{str(e)}", exc_info=True)
  25. finally:
  26. await client.close()
  27. async def main():
  28. await test_mcp_tools()
  29. if __name__ == "__main__":
  30. asyncio.run(main())