OR工具MCP服务器
MCP服务器使用Google OR tools为车辆路线问题(VRP)和旅行商问题(TSP)提供优化工具。
特性
- TSP求解器:优化一次访问所有地点的单车路线
- VRP求解器:在容量和距离限制下优化多车路线
- 从坐标自动计算欧几里德距离
- 支持自定义距离矩阵
- 车辆容量限制
- 每辆车的最大距离限制
安装
- 创建虚拟环境:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- 安装依赖项:
pip install -r requirements.txt运行服务器
python server.py服务器以stdio模式运行,适用于MCP客户端集成。
可用工具
1.solve_tsp
解决了单辆车的旅行推销员问题。
参数:
locations:位置对象列表lat,lon,可选namedepot_index:起始/结束位置索引(默认值:0)distance_matrix:可选的预先计算的距离矩阵
请求示例:
{
"locations": [
{"lat": 40.7128, "lon": -74.0060, "name": "New York"},
{"lat": 34.0522, "lon": -118.2437, "name": "Los Angeles"},
{"lat": 41.8781, "lon": -87.6298, "name": "Chicago"},
{"lat": 29.7604, "lon": -95.3698, "name": "Houston"}
],
"depot_index": 0
}答复:
{
"success": true,
"route": [0, 2, 3, 1, 0],
"total_distance": 123.45,
"num_locations": 4,
"route_details": [
{
"index": 0,
"location": {"lat": 40.7128, "lon": -74.0060, "name": "New York"}
},
...
]
}2.solve_vrp
解决具有约束的多辆车的车辆路线问题。
参数:
locations:位置对象列表num_vehicles:可用车辆数量depot_index:仓库位置索引(默认值:0)demands:每个地点的可选需求列表vehicle_capacities:车辆容量可选列表distance_matrix:可选的预先计算的距离矩阵max_distance:每条车辆路线的可选最大距离
请求示例(有容量限制):
{
"locations": [
{"lat": 40.7128, "lon": -74.0060, "name": "Warehouse"},
{"lat": 40.7580, "lon": -73.9855, "name": "Stop 1"},
{"lat": 40.7489, "lon": -73.9680, "name": "Stop 2"},
{"lat": 40.7614, "lon": -73.9776, "name": "Stop 3"},
{"lat": 40.7549, "lon": -73.9840, "name": "Stop 4"}
],
"num_vehicles": 2,
"depot_index": 0,
"demands": [0, 15, 20, 10, 25],
"vehicle_capacities": [40, 40]
}答复:
{
"success": true,
"routes": [
{
"vehicle_id": 0,
"route": [0, 1, 2, 0],
"distance": 5.67,
"load": 35,
"route_details": [...]
},
{
"vehicle_id": 1,
"route": [0, 3, 4, 0],
"distance": 4.23,
"load": 35,
"route_details": [...]
}
],
"total_distance": 9.90,
"total_load": 70,
"num_vehicles_used": 2
}与n8n集成
配置
- 启动OR Tools MCP服务器:
cd ortools_mcp
source venv/bin/activate
python server.py- 配置n8n以连接到服务器:
MCP服务器URL: http://localhost:3002/mcp
或者,如果同时运行两台服务器,请使用不同的端口:
- TypeScript MCP(OSRM,瓦尔哈拉,n8n):
http://localhost:3001/mcp - OR工具MCP:
http://localhost:3002/mcp
- 在n8n工作流程中,将提供OR工具MCP工具:
- solve_tsp -TSP优化 - solve_vrp -VRP优化
示例n8n工作流
- 获取交货地点 来自数据库
- 致电OSRM 建立所有位置之间的距离矩阵
- 呼叫
solve_vrp具有位置、距离矩阵和车辆约束 - 对于每条优化路线,再次呼叫OSRM以获取转弯方向
- 发送路线 通过API/电子邮件发送给送货司机
运行两台服务器
要在现有路由MCP服务器旁边使用OR工具,请执行以下操作:
# Terminal 1: Start TypeScript MCP server
cd /Users/nixos/workspace/n8n/mcp_server
npm run build && npm start # Runs on port 3001
# Terminal 2: Start OR-Tools MCP server
cd /Users/nixos/workspace/n8n/mcp_server/ortools_mcp
source venv/bin/activate
python server.py # Runs on port 3002现在n8n可以连接到两个服务器并一起使用所有工具!
高级用法
使用自定义距离矩阵
您可以提供预先计算的距离矩阵(例如,来自OSRM或谷歌地图):
{
"locations": [...],
"num_vehicles": 2,
"distance_matrix": [
[0, 10.5, 15.2, 8.7],
[10.5, 0, 12.3, 18.1],
[15.2, 12.3, 0, 9.4],
[8.7, 18.1, 9.4, 0]
]
}容量受限的VRP
对于每个站点都有特定需求的交付计划很有用:
{
"locations": [...],
"num_vehicles": 3,
"demands": [0, 10, 20, 15, 30, 25],
"vehicle_capacities": [50, 50, 50]
}距离约束VRP
限制每辆车的最大距离/时间:
{
"locations": [...],
"num_vehicles": 2,
"max_distance": 100.0
}算法详细信息
- 旅行商问题:使用引导式局部搜索元启发式和路径最便宜的圆弧初始解决方案
- 车辆路径问题:具有额外容量和距离维度约束的相同算法
- 时间限制:每次优化30秒(可在代码中配置)
- 距离缩放:用于精确计算的内部整数缩放(×1000)
备注
- 默认距离计算使用lat/lon坐标上的简单欧几里德距离
- 对于真实世界的路由,提供来自路由引擎的距离矩阵
- 仓库必须包含在指定位置的位置列表中
depot_index - 从车辆计数中过滤空路线(仅限仓库访问)
许可证
麻省理工学院
