tools.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from langchain_mcp_adapters.client import MultiServerMCPClient
  2. import asyncio
  3. mcp_client=MultiServerMCPClient(
  4. {
  5. "amap": {
  6. "transport": "streamable_http",
  7. "url": "https://mcp.api-inference.modelscope.net/75ce1803fffa45/mcp"
  8. },
  9. "hotel-recommend": {
  10. "transport": "streamable_http",
  11. "url": "https://mcp.api-inference.modelscope.net/0c1ea913c4ef4f/mcp"
  12. }
  13. }
  14. )
  15. _mcp_all_tools=None
  16. _tools_lock = asyncio.Lock()
  17. async def get_tools():
  18. global _mcp_all_tools
  19. if _mcp_all_tools is None:
  20. async with _tools_lock:
  21. if _mcp_all_tools is None:
  22. _mcp_all_tools = await mcp_client.get_tools()
  23. return _mcp_all_tools
  24. async def get_transport_tools():
  25. """获取地图交通工具"""
  26. all_mcp_tools=await get_tools()
  27. transport_tools_names=["maps_geo","maps_direction_transit_integrated","maps_direction_driving","maps_direction_walking","maps_bicycling","maps_distance"]
  28. transport_tools=[tool for tool in all_mcp_tools if tool.name in transport_tools_names]
  29. return transport_tools
  30. async def get_hotel_tools():
  31. """获取酒店工具"""
  32. all_mcp_tools=await get_tools()
  33. hotel_tools_names=["hotel_search_and_recommend"]
  34. hotel_tools=[tool for tool in all_mcp_tools if tool.name in hotel_tools_names]
  35. return hotel_tools
  36. async def get_attraction_tools():
  37. """获取POI信息工具"""
  38. all_mcp_tools=await get_tools()
  39. attraction_tools_names=["maps_geo","maps_text_search","maps_around_search","maps_search_detail","maps_weather"]
  40. attraction_tools=[tool for tool in all_mcp_tools if tool.name in attraction_tools_names]
  41. return attraction_tools
  42. async def main():
  43. tools = await get_tools()
  44. print(f"可用工具:{[tool.name for tool in tools]}\n")
  45. if __name__ == "__main__":
  46. asyncio.run(main())