航班搜索MCP服务器
一个可靠的模型上下文协议(MCP)服务器,用于使用SerpAPI Google flights引擎搜索航班。该服务器为单程和往返航班提供实时航班搜索功能。 与claude ai桌面配合良好
✈️ 特性
- 实时航班搜索 使用SerpAPI谷歌航班
- 单程和往返 飞行支援
- 多种航班选择 包括定价、时间和航空公司详细信息
- 符合JSON-RPC 2.0标准 MCP协议实现
- 易于集成 与Claude和其他MCP客户
- 稳健的错误处理 以及日志记录
🚀 快速开始
先决条件
- Python 3.7或更高版本
- SerpAPI帐户和API密钥(在这里买一个)
- MCP兼容客户端(Claude等)
安装
- 克隆或下载 服务器文件:
mkdir -p ~/tools/flightsearch
# Copy flight_search_server.py to ~/tools/flightsearch/- 安装依赖项:
pip install requests- 获取您的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_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
示例:
# One-way flight
search_flights(origin="JFK", destination="LAX", outbound_date="2025-07-01")
# Round-trip flight
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
}
]
}🔧 发展
运行测试
直接测试服务器:
# Set environment variable
export SERP_API_KEY="your_api_key"
# Run the server
python3 flight_search_server.py --connection_type stdio协议测试
服务器实现JSON-RPC 2.0并支持以下方法:
initialize-初始化MCP连接tools/list-列出可用工具tools/call-执行工具ping-健康检查notifications/initialized-初始化通知
日志记录
服务器登录到stderr进行调试:
# View logs while running
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环境变量”
- 确保在MCP配置中正确设置了API密钥
- 验证环境变量名称是否准确
SERP_API_KEY
3.“JSON-RPC模式验证错误”
- 重新启动MCP客户端以重新加载服务器
- 检查您是否使用的是最新版本的服务器
4.未找到航班
- 尝试不同的机场代码或日期
- 某些路线可能在所选日期不可用
- 查看SerpAPI文档以了解支持的机场
调试模式
启用调试日志记录:
# Add to the top of flight_search_server.py
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)📝 API限制
- SerpAPI速率限制:检查您的SerpAPI计划以了解请求限制
- 飞行数据:结果取决于谷歌航班数据的可用性
- 日期范围:仅限未来日期(无法搜索过去的航班)
- 机场代码:必须使用有效的IATA机场代码
🤝 贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 如果适用,添加测试
- 提交拉取请求
开发设置
# Clone the repo
git clone https://github.com/yourusername/flight-search-mcp.git
cd flight-search-mcp
# Install dependencies
pip install requests
# Run tests
python3 test_mcp_protocol.py
python3 test_flight_search.py📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🙏 致谢
📞 支持
- 问题:在GitHub上打开一个问题
- 问题:检查 Serpapi文档
- MCP协议:参见 Anthropic的MCP文档
______________________________________________________________________
由以下材料制成❤️ 对于MCP社区
