from fastmcp import FastMCP # 创建一个 MCP Server。 # 你可以把它理解成:创建一个“旅行工具箱”。 mcp = FastMCP(name="travel-tools") @mcp.tool() def search_hotels(city: str, nights: int = 2, budget_per_night: int = 400) -> str: """ 查询目的地住宿区域建议。 Args: city: 目的地城市,比如 重庆、杭州、西安。 nights: 入住晚数,默认 2 晚。 budget_per_night: 每晚住宿预算,单位元,默认 400 元。 """ return f"{city} 住 {nights} 晚,建议预算 {budget_per_night} 元/晚。" @mcp.tool() def search_transport(origin: str, destination: str, travel_date: str = "未指定") -> str: """ 查询两个城市之间的交通建议。 Args: origin: 出发城市,比如 成都、上海、北京。 destination: 目的地城市,比如 重庆、杭州、西安。 travel_date: 出行日期,比如 2026-08-15。如果用户没说,可以用默认值。 """ if origin == "成都" and destination == "重庆": return ( f"{travel_date} 从成都到重庆,推荐优先选择高铁:" "成都东站到重庆北站约 1.5-2 小时,二等座约 150 元左右。" "如果多人同行也可以考虑自驾,但重庆市区停车和路况会更麻烦。" ) if origin == "上海" and destination == "杭州": return ( f"{travel_date} 从上海到杭州,推荐高铁:" "上海虹桥到杭州东约 45-70 分钟,班次多,适合周末短途游。" ) return ( f"暂时没有收录 {origin} 到 {destination} 的固定交通数据。" "建议优先比较高铁、飞机和自驾三种方式,再根据时间、预算和同行人数选择。" ) @mcp.tool() def recommend_attractions(city: str, preferences: str = "美食、夜景、城市漫步") -> str: """ 根据目的地城市和用户偏好推荐景点。 Args: city: 目的地城市,比如 重庆、杭州、西安。 preferences: 用户偏好,比如 美食、夜景、历史、亲子、拍照。 """ if city == "重庆": return ( f"根据你的偏好「{preferences}」,重庆推荐这些安排:" "洪崖洞适合看夜景,但人多,建议晚上错峰;" "李子坝轻轨站适合体验山城立体交通;" "山城步道适合城市漫步;" "解放碑和八一路好吃街适合美食打卡;" "鹅岭二厂适合拍照和咖啡休息。" ) if city == "杭州": return ( f"根据你的偏好「{preferences}」,杭州推荐西湖、灵隐寺、龙井村、京杭大运河。" "整体节奏可以放慢,适合自然风景和城市漫步。" ) return ( f"暂时没有收录 {city} 的详细景点数据。" f"建议围绕「{preferences}」选择 3-5 个核心景点,每天不要超过 3 个。" ) @mcp.tool() def estimate_budget( destination: str, days: int = 3, people: int = 1, hotel_budget_per_night: int = 400, round_trip_transport_budget: int = 300, ) -> str: """ 估算旅行预算。 Args: destination: 目的地城市,比如 重庆。 days: 旅行天数。 people: 出行人数。 hotel_budget_per_night: 每晚住宿预算,单位元。 round_trip_transport_budget: 每人往返大交通预算,单位元。 """ nights = max(days - 1, 1) hotel_total = nights * hotel_budget_per_night transport_total = people * round_trip_transport_budget food_total = people * days * 120 local_transport_total = people * days * 50 ticket_total = people * 100 total = hotel_total + transport_total + food_total + local_transport_total + ticket_total return ( f"{destination} {days} 天 {people} 人预算估算:" f"往返交通约 {transport_total} 元;" f"住宿约 {hotel_total} 元;" f"餐饮约 {food_total} 元;" f"市内交通约 {local_transport_total} 元;" f"门票/体验预留约 {ticket_total} 元;" f"合计约 {total} 元。" ) if __name__ == "__main__": mcp.run(transport="stdio")