返回市场
航班搜索MCP服务器

航班搜索MCP服务器

作者:random-robbie2 星标更新:2025-06-17

项目介绍

飞机搜索MCP服务器

一个可靠的模型上下文协议(MCP)服务器,用于通过SerpAPI Google Flights引擎搜索航班。该服务器提供了一次性和往返航班的实时搜索能力。 与Claude AI桌面版兼容良好

✈️ 特性

  • 使用SerpAPI Google Flights进行实时航班搜索
  • 支持一次性和往返航班
  • 提供多个航班选项,包括价格、时间及航空公司详情
  • 实现了符合JSON-RPC 2.0标准的MCP协议
  • 与Claude和其他MCP客户端易于集成
  • 具有强大的错误处理和日志记录

🚀 快速开始

前提条件

  • Python 3.7或更高版本
  • SerpAPI账户和API密钥(在这里获取
  • 兼容MCP的客户端(如Claude等)

安装

  1. 克隆或下载服务器文件:
mkdir -p ~/tools/flightsearch
# 将flight_search_server.py复制到~/tools/flightsearch/
  1. 安装依赖项
pip install requests
  1. 获取您的SerpAPI密钥
    • SerpAPI注册
    • 从仪表板获取您的API密钥

配置

在您的MCP客户端配置中添加以下内容:

{
  "flightsearch": {
    "command": "python3",
    "args": [
      "/path/to/your/tools/flightsearch/flight_search_server.py",
      "--connection_type", 
      "stdio"
    ],
    "env": {
      "SERP_API_KEY": "your_serpapi_key_here"
    }
  }
}

对于Claude Desktop,在您的claude_desktop_config.json中添加以下内容:

{
  "mcpServers": {
    "flightsearch": {
      "command": "python3",
      "args": [
        "/Users/yourusername/tools/flightsearch/flight_search_server.py",
        "--connection_type", 
        "stdio"
      ],
      "env": {
        "SERP_API_KEY": "your_serpapi_key_here"
      }
    }
  }
}

📖 使用方法

可用工具

search_flights

搜索机场之间的航班。

参数:

  • origin(必需):出发机场代码(例如:"JFK","LAX")
  • destination(必需):到达机场代码(例如:"JFK","LAX")
  • outbound_date(必需):出发日期,格式为YYYY-MM-DD
  • return_date(可选):往返航班的返回日期,格式为YYYY-MM-DD

示例:

# 单程航班
search_flights(origin="JFK", destination="LAX", outbound_date="2_25-07-01")

# 往返航班  
search_flights(origin="JFK", destination="LAX", outbound_date="2025-07-01", return_date="2025-07-08")

server_status

检查航班搜索服务器是否正在运行。

参数:

示例响应

{
  "status": "success",
  "origin": "JFK",
  "destination": "LAX", 
  "outbound_date": "2025-07-01",
  "return_date": null,
  "trip_type": "one_way",
  "flights": [
    {
      "price": 199,
      "departure_time": "2025-07-01 08:40",
      "arrival_time": "2025-07-01 11:45", 
      "airline": "Delta",
      "duration": 365,
      "stops": 0
    },
    {
      "price": 204,
      "departure_time": "2025-07-01 09:00",
      "arrival_time": "2025-07-01 12:00",
      "airline": "JetBlue", 
      "duration": 360,
      "stops": 0
    }
  ]
}

🔧 开发

运行测试

直接测试服务器:

# 设置环境变量
export SERP_API_KEY="your_api_key"

# 运行服务器  
python3 flight_search_server.py --connection_type stdio

协议测试

服务器实现了JSON-RPC 2.0,并支持以下方法:

  • initialize - 初始化MCP连接
  • tools/list - 列出可用工具
  • tools/call - 执行工具
  • ping - 健康检查
  • notifications/initialized - 初始化通知

日志记录

服务器将日志记录到stderr以供调试:

# 运行时查看日志
python3 flight_search_server.py --connection_type stdio 2>debug.log

🐛 故障排除

常见问题

1. "API请求失败:400客户端错误"

  • 验证您的SerpAPI密钥是否有效
  • 检查机场代码是否正确(使用IATA代码如"JFK","LAX")
  • 确保日期格式为YYYY-MM-DD

2. "未设置SERP_API_KEY环境变量"

  • 确保API密钥已在您的MCP配置中正确设置
  • 验证环境变量名称是否完全为SERP_API_KEY

3. "JSON-RPC模式验证错误"

  • 重新启动您的MCP客户端以重新加载服务器
  • 检查您是否使用的是最新版本的服务器

4. 未找到航班

  • 尝试不同的机场代码或日期
  • 某些航线可能在所选日期不可用
  • 查看SerpAPI文档以了解支持的机场

调试模式

启用调试日志记录:

# 添加到flight_search_server.py顶部
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)

📝 API限制

  • SerpAPI速率限制:查看您的SerpAPI计划以了解请求限制
  • 航班数据:结果取决于Google Flights的数据可用性
  • 日期范围:仅限未来日期(不能搜索过去的航班)
  • 机场代码:必须使用有效的IATA机场代码

🤝 贡献

  1. 分叉仓库
  2. 创建功能分支
  3. 进行更改
  4. 如适用,请添加测试
  5. 提交拉取请求

开发设置

# 克隆仓库
git clone https://github.com/yourusername/flight-search-mcp.git
cd flight-search-mcp

# 安装依赖项
pip install requests

# 运行测试
python3 test_mcp_protocol.py
python3 test_flight_search.py

📄 许可

本项目根据MIT许可发布 - 详见LICENSE文件。

🙏 致谢

  • SerpAPI提供了Google Flights API
  • Anthropic提供了MCP协议规范
  • 开源社区提供了灵感和反馈

📞 支持


由❤️为MCP社区制作