M

MCP-优工

@Jacck/mcp-ortools
1 Stars 442 次浏览 Jacck 更新于 2026-08-23

MCP-ORTools 通过 MCP 将谷歌的 OR-Tools 约束编程求解器与大型语言模型集成在一起,使人工智能模型能够: 提交和验证约束模型 设置模型参数 求解约束满足和优化问题 检索和分析解决方案

MCP 服务配置

复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用

{
  "mcpServers": {
    "ortools": {
      "args": [
        "-m",
        "mcp_ortools.server"
      ],
      "command": "python"
    }
  }
}

服务介绍

MCP-ORTools

使用 Google OR-Tools 进行约束求解的 Model Context Protocol (MCP) 服务器实现。通过标准化的约束模型规范,设计用于与大型语言模型配合使用。

概述

MCP-ORTools 通过 Model Context Protocol 将 Google 的 OR-Tools 约束编程求解器与大型语言模型集成,使 AI 模型能够:

  • 提交和验证约束模型
  • 设置模型参数
  • 解决约束满足和优化问题
  • 检索和分析解决方案

安装

  1. 安装包:
pip install git+https://github.com/Jacck/mcp-ortools.git
  1. 配置 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 的模型规范
  • 支持:
    • 整数和布尔变量(域:[min, max])
    • 使用 OR-Tools 方法语法的线性约束
    • 线性优化目标
    • 超时和求解器参数
    • 二进制约束和关系
    • 投资组合选择问题
    • 背包问题

约束中支持的操作

  • 基本算术:+、-、*
  • 比较:.le(), .ge(), .eq(), .ne()
  • 变量的线性组合
  • 通过约束组合实现的二进制逻辑

开发

设置开发环境:

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
}

状态值:

  • OPTIMAL:找到最优解
  • FEASIBLE:找到可行解
  • INFEASIBLE:无解
  • UNKNOWN:无法确定解

许可证

MIT 许可证 - 详见 LICENSE 文件

相关 MCP 服务