MCP-OR工具
使用 Google OR-Tools 进行约束求解的模型上下文协议 (MCP) 服务器实现。该实现旨在通过标准化约束模型规范与大型语言模型配合使用。
概述
MCP-ORTools 通过模型上下文协议将 Google 的 OR-Tools 约束编程求解器与大型语言模型相集成,使 AI 模型能够:
- 提交并验证约束模型
- 设置模型参数
- 解决约束满足和优化问题
- 检索和分析解决方案
安装
- 安装软件包:
pip install git+https://github.com/Jacck/mcp-ortools.git
- 配置 Claude Desktop 在
%APPDATA%\Claude\claude_desktop_config.json
(Windows) 或~/Library/Application Support/Claude/claude_desktop_config.json
(macOS) 创建配置文件:
{
"mcpServers": {
"ortools": {
"command": "python",
"args": ["-m", "mcp_ortools.server"]
}
}
}
型号规格
模型以 JSON 格式指定,包含三个主要部分:
variables
:定义变量及其域constraints
:使用 OR-Tools 方法的约束列表objective
:可选优化目标
约束语法
约束必须使用 OR-Tools 方法语法:
.__le__()
表示小于或等于 (<=).__ge__()
表示大于或等于 (>=).__eq__()
表示相等 (==).__ne__()
表示不等于 (!=)
使用示例
简单优化模型
{
"variables": [
{"name": "x", "domain": [0, 10]},
{"name": "y", "domain": [0, 10]}
],
"constraints": [
"(x + y).__le__(15)",
"x.__ge__(2 * y)"
],
"objective": {
"expression": "40 * x + 100 * y",
"maximize": true
}
}
背包问题
示例:选择值为 [3,1,2,1]、权重为 [2,2,1,1] 的项目,总权重限制为 2。
{
"variables": [
{"name": "p0", "domain": [0, 1]},
{"name": "p1", "domain": [0, 1]},
{"name": "p2", "domain": [0, 1]},
{"name": "p3", "domain": [0, 1]}
],
"constraints": [
"(2*p0 + 2*p1 + p2 + p3).__le__(2)"
],
"objective": {
"expression": "3*p0 + p1 + 2*p2 + p3",
"maximize": true
}
}
附加约束示例:
{
"constraints": [
"p0.__eq__(1)", // Item p0 must be selected
"p1.__ne__(p2)", // Can't select both p1 and p2
"(p2 + p3).__ge__(1)" // Must select at least one of p2 or p3
]
}
特征
- 全面支持 OR-Tools CP-SAT 求解器
- 基于 JSON 的模型规范
- 支持:
- 整数和布尔变量(域:[最小值,最大值])
- 使用 OR-Tools 方法语法的线性约束
- 线性优化目标
- 超时和求解器参数
- 二元约束和关系
- 投资组合选择问题
- 背包问题
约束中支持的操作
- 基本算术:+、-、*
- 比较: .乐(),.格(),.等式(), .讷()
- 变量的线性组合
- 通过约束组合的二进制逻辑
发展
开发设置:
git clone https://github.com/Jacck/mcp-ortools.git
cd mcp-ortools
pip install -e .
模型响应格式
求解器以 JSON 格式返回解决方案:
{
"status": "OPTIMAL",
"solve_time": 0.045,
"variables": {
"p0": 0,
"p1": 0,
"p2": 1,
"p3": 1
},
"objective_value": 3.0
}
状态值:
- 最佳:找到最佳解决方案
- 可行:找到可行的解决方案
- 不可行:不存在解决方案
- 未知:���法确定解决方案
执照
MIT 许可证 - 详情请参阅许可证文件