关于 TSP MCP Server
TSP MCP Server 用于求解旅行商问题并计算路线距离,也可生成路线图像供查看。它面向需要处理坐标或具名城市路线问题的用户,并通过 MCP 客户端提供功能。
主要功能
- 01求解最多 10 个城市的最优路线
- 02用启发式算法搜索较大路线
- 03支持坐标和具名城市输入
- 04计算指定路线的距离
- 05生成可自定义的路线图
适用场景
- 01规划经过多个配送点的短路线
- 02比较指定路线的总距离
- 03查看生成的路线图像
MCP 相关
它向 MCP 客户端提供五种工具,用于求解 TSP、计算路线距离和生成可视化。客户端可通过自然语言交互调用这些工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
TSP MCP 服务
用于解决旅行商问题(TSP)的模型上下文协议(MCP)服务。此服务提供经过优化的算法,用于查找经过一组城市的最短路线,让你可以通过与 Claude Desktop 的自然语言交互轻松解决 TSP 问题。
功能
- 🚀 多种算法:对小规模实例(≤10 个城市)使用动态规划,对更大规模的实例使用最近邻 + 2-opt 启发式算法
- 🎯 最优解:保证小规模问题的解为最优解,并为更大规模的问题提供高质量解
- 🏙️ 命名城市:支持仅输入坐标或输入命名城市
- 📏 距离计算:计算自定义路线的距离
- 🔄 多个起点:测试多个起点,以找到更好的解
- 📊 可视化呈现:生成精美的 TSP 路线和解的 SVG 可视化图
- 🎨 可自定义的可视化效果:可调整画布大小、颜色、标签和样式选项
安装
前置条件
- Node.js(版本 18 或更高)
- npm 或 yarn
- Claude Desktop 应用程序
方法 1:从 npm 安装(即将推出)
npm install -g tsp-mcp-server
方法 2:从 GitHub 安装
- 克隆此仓库:
git clone https://github.com/yourusername/tsp-mcp-server.git
cd tsp-mcp-server
- 安装依赖项:
npm install
- 构建项目:
npm run build
- 全局链接(可选):
npm link
集成 Claude Desktop
若要将此 MCP 服务与 Claude Desktop 一起使用,请将以下配置添加到 Claude Desktop 设置中:
macOS
编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"tsp-mcp-server": {
"command": "/usr/local/bin/node",
"args": ["/absolute/path/to/tsp-mcp-server/dist/index.js"],
"cwd": "/absolute/path/to/tsp-mcp-server",
"env": {
"PATH": "/Users/yourusername/Library/Python/3.8/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
}
}
}
}
重要:将 /absolute/path/to/tsp-mcp-server 替换为你克隆仓库的实际路径(例如,/Users/yourusername/Cursor/tsp-mcp-server)。
Windows
编辑 %APPDATA%/Claude/claude_desktop_config.json:
{
"mcpServers": {
"tsp-mcp-server": {
"command": "node",
"args": ["C:\\absolute\\path\\to\\tsp-mcp-server\\dist\\index.js"],
"cwd": "C:\\absolute\\path\\to\\tsp-mcp-server"
}
}
}
测试连接
更新配置后:
- 完全重启 Claude Desktop(退出后重新打开)
- 在新对话中测试连接:
Can you solve this TSP problem: cities at (0,0), (1,1), (2,0)? - 如果服务没有响应,请在 Claude Desktop 的开发者控制台中检查错误
故障排除
- 找不到服务:检查配置中的绝对路径
- 找不到 Node.js:使用 node 的完整路径(macOS 上为
/usr/local/bin/node) - 权限问题:确保
dist/index.js文件可执行 - 构建未更新:修改源代码后运行
npm run build
使用示例
与 Claude Desktop 集成后,你可以使用自然语言解决 TSP 问题:
基本 TSP 问题
"I have 5 cities at these coordinates: (0,0), (1,2), (3,1), (2,4), (4,3).
What's the shortest route to visit all cities and return to the starting point?"
命名城市
"Find the optimal delivery route for these locations:
- Seoul: (37.5665, 126.9780)
- Busan: (35.1796, 129.0756)
- Incheon: (37.4563, 126.7052)
- Daegu: (35.8714, 128.6014)
- Daejeon: (36.3504, 127.3845)"
路线距离计算
"Calculate the total distance if I visit cities in this order: 0 → 2 → 1 → 3 → 4
Cities are at: (0,0), (1,2), (3,1), (2,4), (4,3)"
🎨 TSP 解的可视化
"Solve the TSP for these 4 cities and create an image visualization:
(0,0), (5,3), (2,7), (8,1)"
"Create a dark-themed visualization of this route through Seoul, Busan, and Incheon"
"Show me a 1000x800 pixel colorful visualization of the optimal route through these delivery points"
可用工具
此服务提供五种强大的工具:
1. solve_tsp
根据具有 x、y 坐标的城市求解 TSP。
输入:
{
"cities": [
{"x": 0, "y": 0},
{"x": 1, "y": 2},
{"x": 3, "y": 1}
]
}
2. solve_tsp_with_names
根据具有坐标的命名城市求解 TSP。
输入:
{
"cities": [
{"name": "Seoul", "x": 37.5665, "y": 126.9780},
{"name": "Busan", "x": 35.1796, "y": 129.0756},
{"name": "Incheon", "x": 37.4563, "y": 126.7052}
]
}
3. calculate_route_distance
计算指定路线的总距离。
输入:
{
"cities": [
{"x": 0, "y": 0},
{"x": 1, "y": 2},
{"x": 3, "y": 1}
],
"route": [0, 2, 1]
}
4. 🖼️ visualize_tsp_route
生成 TSP 路线的高质量 PNG 图像。
功能:
- 🔴 使用带索引编号的红色圆圈表示城市
- 🔵 使用蓝色线条表示路线连接
- 📝 显示城市标签及访问顺序
- 📏 显示总距离
- 🎨 多种样式主题
- ⚡ 即时生成图像文件
输入:
{
"cities": [
{"x": 0, "y": 0, "name": "Start"},
{"x": 5, "y": 3, "name": "Middle"},
{"x": 2, "y": 7, "name": "End"}
],
"route": [0, 1, 2],
"options": {
"width": 1000,
"height": 800,
"showLabels": true,
"showDistance": true,
"style": "dark"
}
}
5. 🚀 solve_and_visualize_tsp
一步完成 TSP 求解并生成精美的图像可视化效果,是终极 TSP 工具!
功能:
- ⚡ 一步完成求解和可视化
- 🧠 自动选择算法(DP 或启发式算法)
- 📊 详细的解分析
- 🖼️ 生成专业的 PNG 图像
- 📋 全面的路线信息
- 🎨 可自定义的样式主题
输入:
{
"cities": [
{"x": 0, "y": 0, "name": "Warehouse"},
{"x": 5, "y": 3, "name": "Store A"},
{"x": 2, "y": 7, "name": "Store B"},
{"x": 8, "y": 1, "name": "Store C"}
],
"options": {
"width": 800,
"height": 600,
"showLabels": true,
"showDistance": true,
"style": "modern"
}
}
算法
动态规划(Held-Karp)
- 适用范围:城市数 ≤10 的问题
- 时间复杂度:O(n²2ⁿ)
- 空间复杂度:O(n2ⁿ)
- 保证:最优解
最近邻 + 2-opt
- 适用范围:城市数 >10 的问题
- 时间复杂度:2-opt 改进为 O(n³)
- 方法:
- 使用最近邻算法尝试多个起点
- 使用 2-opt 局部搜索改进每个解
- 返回找到的最佳解
开发
在开发模式下运行
npm run dev
测试服务
你可以直接测试服务:
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' | node dist/index.js
构建
npm run build
贡献
- Fork 此代码库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交更改(
git commit -m 'Add some amazing feature') - 推送到分支(
git push origin feature/amazing-feature) - 发起 Pull Request
许可证
本项目采用 MIT 许可证,详情请参阅 LICENSE 文件。
致谢
- 使用 Model Context Protocol SDK 构建
- 实现了经过调整、适用于实际场景的经典 TSP 算法
- 灵感来自对易用优化工具的需求
🎨 TSP MCP 服务的可视化
TSP MCP 服务提供高质量 PNG 图像可视化:
🖼️ 基于图像的可视化
- 技术:HTML5 Canvas + Node.js Canvas 库
- 实现:
ImageTSPVisualizer类(src/image-visualizer.ts) - 输出:保存为 PNG 图像文件后自动打开
- 优点:
- 高质量光栅图像
- 支持多种样式主题
- 支持发光效果等高级图形效果
- 无需外部依赖即可独立运行
- 无需 Claude artifacts,即可立即生成系统文件
支持的样式主题:
'modern' // 기본 - 흰 배경, 빨간 도시, 파란 경로
'dark' // 어두운 테마 - 검은 배경, 밝은 색상
'minimal' // 미니멀 - 회색톤 단순한 디자인
'colorful' // 컬러풀 - 다채로운 색상 조합
🎯 可视化使用方法
求解 TSP + 可视化(推荐)
{
"name": "solve_and_visualize_tsp",
"arguments": {
"cities": [{"x": 0, "y": 0}, {"x": 5, "y": 3}],
"options": {
"style": "dark",
"width": 1200,
"height": 800
}
}
}
可视化现有路径
{
"name": "visualize_tsp_route",
"arguments": {
"cities": [{"x": 0, "y": 0}, {"x": 5, "y": 3}],
"route": [0, 1],
"options": {
"style": "minimal"
}
}
}
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。