my_mcp_demo.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from fastmcp import FastMCP
  2. # 创建一个 MCP Server。
  3. # 你可以把它理解成:创建一个“旅行工具箱”。
  4. mcp = FastMCP(name="travel-tools")
  5. @mcp.tool()
  6. def search_hotels(city: str, nights: int = 2, budget_per_night: int = 400) -> str:
  7. """
  8. 查询目的地住宿区域建议。
  9. Args:
  10. city: 目的地城市,比如 重庆、杭州、西安。
  11. nights: 入住晚数,默认 2 晚。
  12. budget_per_night: 每晚住宿预算,单位元,默认 400 元。
  13. """
  14. return f"{city} 住 {nights} 晚,建议预算 {budget_per_night} 元/晚。"
  15. @mcp.tool()
  16. def search_transport(origin: str, destination: str, travel_date: str = "未指定") -> str:
  17. """
  18. 查询两个城市之间的交通建议。
  19. Args:
  20. origin: 出发城市,比如 成都、上海、北京。
  21. destination: 目的地城市,比如 重庆、杭州、西安。
  22. travel_date: 出行日期,比如 2026-08-15。如果用户没说,可以用默认值。
  23. """
  24. if origin == "成都" and destination == "重庆":
  25. return (
  26. f"{travel_date} 从成都到重庆,推荐优先选择高铁:"
  27. "成都东站到重庆北站约 1.5-2 小时,二等座约 150 元左右。"
  28. "如果多人同行也可以考虑自驾,但重庆市区停车和路况会更麻烦。"
  29. )
  30. if origin == "上海" and destination == "杭州":
  31. return (
  32. f"{travel_date} 从上海到杭州,推荐高铁:"
  33. "上海虹桥到杭州东约 45-70 分钟,班次多,适合周末短途游。"
  34. )
  35. return (
  36. f"暂时没有收录 {origin} 到 {destination} 的固定交通数据。"
  37. "建议优先比较高铁、飞机和自驾三种方式,再根据时间、预算和同行人数选择。"
  38. )
  39. @mcp.tool()
  40. def recommend_attractions(city: str, preferences: str = "美食、夜景、城市漫步") -> str:
  41. """
  42. 根据目的地城市和用户偏好推荐景点。
  43. Args:
  44. city: 目的地城市,比如 重庆、杭州、西安。
  45. preferences: 用户偏好,比如 美食、夜景、历史、亲子、拍照。
  46. """
  47. if city == "重庆":
  48. return (
  49. f"根据你的偏好「{preferences}」,重庆推荐这些安排:"
  50. "洪崖洞适合看夜景,但人多,建议晚上错峰;"
  51. "李子坝轻轨站适合体验山城立体交通;"
  52. "山城步道适合城市漫步;"
  53. "解放碑和八一路好吃街适合美食打卡;"
  54. "鹅岭二厂适合拍照和咖啡休息。"
  55. )
  56. if city == "杭州":
  57. return (
  58. f"根据你的偏好「{preferences}」,杭州推荐西湖、灵隐寺、龙井村、京杭大运河。"
  59. "整体节奏可以放慢,适合自然风景和城市漫步。"
  60. )
  61. return (
  62. f"暂时没有收录 {city} 的详细景点数据。"
  63. f"建议围绕「{preferences}」选择 3-5 个核心景点,每天不要超过 3 个。"
  64. )
  65. @mcp.tool()
  66. def estimate_budget(
  67. destination: str,
  68. days: int = 3,
  69. people: int = 1,
  70. hotel_budget_per_night: int = 400,
  71. round_trip_transport_budget: int = 300,
  72. ) -> str:
  73. """
  74. 估算旅行预算。
  75. Args:
  76. destination: 目的地城市,比如 重庆。
  77. days: 旅行天数。
  78. people: 出行人数。
  79. hotel_budget_per_night: 每晚住宿预算,单位元。
  80. round_trip_transport_budget: 每人往返大交通预算,单位元。
  81. """
  82. nights = max(days - 1, 1)
  83. hotel_total = nights * hotel_budget_per_night
  84. transport_total = people * round_trip_transport_budget
  85. food_total = people * days * 120
  86. local_transport_total = people * days * 50
  87. ticket_total = people * 100
  88. total = hotel_total + transport_total + food_total + local_transport_total + ticket_total
  89. return (
  90. f"{destination} {days} 天 {people} 人预算估算:"
  91. f"往返交通约 {transport_total} 元;"
  92. f"住宿约 {hotel_total} 元;"
  93. f"餐饮约 {food_total} 元;"
  94. f"市内交通约 {local_transport_total} 元;"
  95. f"门票/体验预留约 {ticket_total} 元;"
  96. f"合计约 {total} 元。"
  97. )
  98. if __name__ == "__main__":
  99. mcp.run(transport="stdio")