Bing Flights MCP 服务器
](https://pypi.org/project/bing-flights-mcp/)  
一个使用Playwright从Bing Flights抓取航班信息的模型上下文协议(MCP)服务器。此项目提供了一个独立的Python抓取模块以及一个MCP服务器封装,以便与兼容MCP的应用程序集成。
📦 箱子/包裹 在 PyPI 上查看
特点/功能
- 🔍 搜索单程和往返航班
- ✈️ 支持多名乘客(成人、儿童、婴儿)
- 所有舱位等级(经济舱、高级经济舱、商务舱、头等舱)
- 无头或有头浏览器模式
- 📊 结构化的JSON响应
- 🔧 通过MCP协议轻松集成
项目结构
bing-flights-mcp/
├── pyproject.toml # Package configuration
├── requirements.txt # Python dependencies
├── README.md # This file
├── bing_flights_scraper/ # Standalone scraper module
│ ├── __init__.py
│ └── scraper.py
├── mcp_server.py # MCP server implementation
└── tests/ # Test suite
├── __init__.py
├── test_mcp.py
└── test_e2e.py安装
快速入门(推荐)
使用这个MCP服务器最简单的方法是通过 uvx:
uvx bing-flights-mcp这将在一个隔离的环境中自动安装该软件包及其依赖项。
通过 pip 安装
你也可以从PyPI进行安装:
pip install bing-flights-mcp安装完成后,安装Playwright浏览器:
playwright install chromium开发安装
对于开发或者如果您想修改代码:
- 克隆或下载此仓库
- 创建并激活虚拟环境
在Windows上:
python -m venv venv
venv\Scripts\activate在 macOS/Linux 上:
python -m venv venv
source venv/bin/activate- 安装依赖项
pip install -r requirements.txt- 安装 Playwright 浏览器
playwright install chromium使用方法
作为MCP服务器
使用uvx(推荐)
如果你通过PyPI安装,请运行:
uvx bing-flights-mcp或者在您的MCP设置配置中添加:
{
"mcpServers": {
"bing-flights": {
"command": "uvx",
"args": ["bing-flights-mcp"]
}
}
}从源代码运行
如果你正在从源代码开发或运行:
python mcp_server.py可用工具
MCP服务器提供了两个工具:
1. search_flights
使用必应航班搜索查找航班选项。
参数:
origin(字符串,必填):出发机场代码(例如,“SEA”)destination(字符串,必填):目的地机场代码(例如,“ICN”)departure_date(字符串,必填):出发日期,格式为YYYY-MM-DDreturn_date(字符串,可选):往返搜索的返回日期adults(整数,可选,默认值=1):成人乘客人数children(整数,可选,默认值=0):儿童乘客数量infants(整数,可选,默认值=0):婴儿乘客数量cabin_class(整数,可选,默认值=0):0=经济舱,1=高级经济舱,2=商务舱,3=头等舱max_results(整数,可选,默认值=10):最大结果数量headless(布尔型,可选,默认为true):以无头模式运行浏览器
示例MCP工具调用:
{
"origin": "SEA",
"destination": "ICN",
"departure_date": "2025-11-30",
"return_date": "2025-12-02",
"adults": 1,
"cabin_class": 0,
"max_results": 10,
"headless": true
}2. get_scraper_status
检查抓取器的健康状况和配置。
示例回复:
{
"status": "healthy",
"version": "1.0.1",
"capabilities": {
"one_way_search": true,
"round_trip_search": true,
"cabin_classes": ["economy", "premium_economy", "business", "first"],
"max_results": 50,
"headless_mode": true
}
}作为一个独立的Python模块
你也可以直接在你的Python代码中使用这个抓取器:
from bing_flights_scraper import BingFlightsScraper
# Create scraper instance
scraper = BingFlightsScraper(headless=True)
try:
# Search for flights
results = scraper.search_flights(
origin="SEA",
destination="ICN",
departure_date="2025-11-30",
return_date="2025-12-02",
adults=1,
cabin_class=0,
max_results=10
)
# Process results
print(f"Found {results['results_count']} flights")
for flight in results['flights']:
print(f"Price: ${flight['price']['total']}")
print(f"Airlines: {', '.join(flight['airlines'])}")
print(f"Departure: {flight['outbound']['departure_time']}")
print(f"Arrival: {flight['outbound']['arrival_time']}")
print(f"Duration: {flight['outbound']['duration']}")
print("---")
finally:
scraper.close()响应格式
抓取器返回的结果采用以下JSON结构:
{
"search_params": {
"origin": "SEA",
"destination": "ICN",
"departure_date": "2025-11-30",
"return_date": "2025-12-02",
"trip_type": "round-trip",
"passengers": {
"adults": 1,
"children": 0,
"infants": 0
},
"cabin_class": "economy"
},
"results_count": 10,
"flights": [
{
"price": {
"total": 1200.00,
"currency": "USD",
"per_person": 1200.00
},
"airlines": ["Korean Air", "Delta"],
"outbound": {
"departure_time": "10:30",
"arrival_time": "14:45",
"duration": "13h 15m",
"stops": 1,
"layovers": [],
"flight_numbers": []
},
"booking_link": "https://www.bing.com/...",
"result_index": 1
}
],
"timestamp": "2025-10-23T02:19:00Z"
}配置选项
客舱等级
0- 经济1- 尊享经济舱2- 商业3- 头等舱
浏览器模式
headless=True- 浏览器在后台运行(速度更快,无用户界面)headless=False- 浏览器窗口可见(对调试很有用)
故障排除
常见问题
问题:未找到Playwright浏览器
Solution: Run `playwright install chromium`问题:等待航班结果超时
Solution:
- Check your internet connection
- Try with headless=False to see what's happening
- Verify the airport codes are valid
- Ensure the dates are in the future问题:未返回结果
Solution:
- Verify airport codes are correct (use IATA codes like "SEA", "ICN")
- Check that dates are in YYYY-MM-DD format
- Try different date ranges
- Some routes may not have available flights问题:导入错误
Solution:
- Ensure virtual environment is activated
- Run `pip install -r requirements.txt` again
- Verify Python version is 3.10 or higher调试
为了调试抓取问题,请运行 headless=False:
scraper = BingFlightsScraper(headless=False)这将显示浏览器窗口,以便您查看正在加载的内容。
运行测试
基本测试
运行基本单元测试,以验证模块导入和URL构建:
python tests/test_mcp.py端到端测试
进行全面的端到端测试,执行实际的航班搜索操作:
python tests/test_e2e.py注: 端到端(E2E)测试会对Bing Flights发出真实的网页请求,完成这些测试可能需要几分钟时间。测试内容包括:
- 无效参数测试 - 验证错误处理
- 单向搜索测试 - 从东南亚(SEA)到仁川(ICN)的真实搜索
- 往返搜索测试 - 从洛杉矶(LAX)到纽约肯尼迪机场(JFK)的实际搜索
- 多位乘客测试 - 搜索2名成人和1名儿童的信息
测试使用无头浏览器模式和未来日期来确保搜索的有效性。
技术细节
网页抓取方法
- 使用 Playwright 进行浏览器自动化
- 等待动态内容加载
- 从Bing航班搜索结果卡片中提取数据
- 处理多种选择器模式以增强鲁棒性
错误处理
该抓取程序使用了最少的错误处理,并允许异常传播:
- 网络错误 → 抛出异常
- 超时错误 → 抛出
PlaywrightTimeoutError - 无效参数 → 抛出异常
ValueError - 解析错误 → 返回部分结果或空列表
此设计使MCP客户端能够实现适当的重试逻辑和错误恢复。
局限性
- 仅返回去程航班详情(Bing结果页面上不提供返程航班信息)
- 初始页面加载时可见的内容限制了最大结果数
- 不支持分页(仅显示第一页结果)
- 抓取依赖于Bing的页面结构(如果他们更改了HTML,可能会导致抓取失败)
依赖项
fastmcp>=0.1.0- MCP服务器框架playwright>=1.40.0- 浏览器自动化python-dateutil>=2.8.2- 日期解析工具
做出贡献
在贡献时,请:
- 在无头模式和有头模式下测试更改
- 验证与MCP协议的兼容性
- 更新文档以包含新功能
- 遵循现有的代码风格和模式
许可证
MIT 许可证 - 详见 LICENSE 文件
免责声明
这个工具从Bing航班中抓取公开可用的数据。请:
- 负责任地使用,并遵守速率限制
- 查看Bing的服务条款
- 未经适当授权,不得用于商业用途
- 请注意,如果网站发生变化,网页抓取可能会失效
支持
如需报告问题、提出疑问或做出贡献,请在项目仓库中提交一个议题。
