TSP MCP服务器
一种用于解决旅行商问题(TSP)的模型上下文协议(MCP)服务器。该服务器提供优化的算法,以找到穿过一组城市的最短路线,通过与Claude Desktop的自然语言交互,可以轻松解决TSP问题。
特性
- 🚀 多种算法:小实例(≤10个城市)的动态规划,大实例的最近邻+2位启发式
- 🎯 最优解:保证小问题的最佳解决方案,大问题的高质量解决方案
- 🏙️ 命名城市:支持仅坐标输入和命名城市输入
- 📏 距离计算:计算自定义路线的距离
- 🔄 多个起点:测试多个起点以找到更好的解决方案
- 📊 视觉表示:生成TSP路线和解决方案的漂亮SVG可视化
- 🎨 可定制的可视化:可调节的画布大小、颜色、标签和样式选项
安装
先决条件
- Node.js(版本18或更高)
- npm或纱线
- Claude桌面应用程序
方法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 linkClaude桌面集成
要将此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"
}
}
}测试连接
更新配置后:
- 重新启动克劳德桌面 完全(退出并重新打开)
- 测试连接 在新对话中:
Can you solve this TSP problem: cities at (0,0), (1,1), (2,0)?- 检查错误 如果服务器没有响应,则在Claude Desktop的开发人员控制台中
故障排除
- 找不到服务器:验证配置中的绝对路径
- 找不到Node.js:使用节点的完整路径(
/usr/local/bin/nodemacOS) - 权限问题:确保
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"
}
}算法
动态规划(赫尔德·卡普)
- 用于:≤10个城市的问题
- 时间复杂度:O(n²2ⁿ)
- 空间复杂度:O(n2ⁿ)
- 担保:最佳解决方案
最近邻居+2分
- 用于:10个以上城市的问题
- 时间复杂度:O(n³)表示提高2个百分点
- 方法:
1. 使用最近邻尝试多个起点 1. 使用2-pt本地搜索改进每个解决方案 1. 返回找到的最佳解决方案
发展
以开发模式运行
npm run dev测试服务器
您可以直接测试服务器:
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' | node dist/index.js建筑
npm run build贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
致谢
- 与 模型上下文协议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"
}
}
}