ortools-mcp

ortools-mcp

An MCP server that uses Google OR-Tools CP-SAT to find minimal corrections to variable values to satisfy a set of equations, useful for fixing misread data from OCR or forms.

Category
访问服务器

README

ortools-mcp

An MCP server that exposes Google OR-Tools CP-SAT as tools for constraint-based value correction. Given a set of equations and observed variable values, it finds the minimal corrections needed to make everything consistent.

Use case: you have values extracted from a document (OCR, form parsing, etc.) that should satisfy known equations (e.g. total = quantity × rate + tax), but one or more values were misread. The solver finds which values to adjust and by how much, weighted by how much you trust each observation.


Tools

solve

Runs CP-SAT. Returns corrected values for free (non-fixed) variables that best satisfy the given equations.

Input

{
  "variables": {
    "gross_amount": {
      "obs": 500,
      "confidence": 0.3,
      "mult_factor": 100,
      "min": 0,
      "max": 100000,
      "fixed": false
    },
    "quantity": {
      "obs": 10,
      "mult_factor": 1,
      "fixed": true
    },
    "rate_per_pc": {
      "obs": 52,
      "mult_factor": 100,
      "fixed": true
    }
  },
  "equations": [
    {
      "lhs": "gross_amount",
      "rhs": "quantity * rate_per_pc",
      "relation": "==",
      "weight": 1000,
      "tolerance": 0
    }
  ],
  "timeout_seconds": 5,
  "num_workers": 8
}

Output

{
  "status": "OPTIMAL",
  "corrected": {
    "gross_amount": 520.0
  }
}

Only free (non-fixed) variables appear in corrected. Status values: OPTIMAL, FEASIBLE, INFEASIBLE, UNKNOWN, INVALID_EQUATION.

Variable spec fields

Field Type Default Description
obs number — Observed (extracted) value in real-world units
confidence 0–1 0.0 How much to trust obs. Higher → stronger pull toward the observed value
mult_factor int 1 Scales float values to integers for the solver (obs × mult_factor), result is divided back on return
min / max number 0 / 2e9 Domain bounds in real-world units
fixed bool false Pin this variable to obs — it won't appear in corrected

Equation fields

Field Type Default Description
lhs string required Left-hand side expression
rhs string required Right-hand side expression
relation == | <= | >= required Relation between lhs and rhs
weight int 1000 Penalty per unit of violation — higher means the solver tries harder to satisfy this equation
tolerance int 0 Allowed slack (in scaled integer units) before penalty kicks in

Solver params

Field Default Description
timeout_seconds 5 Wall-clock limit for the solver
num_workers 8 Parallel search workers

validate_equations

Pure AST check — no solver invoked. Call this before solve to catch unsupported constructs early.

Input

{
  "equations": [
    { "lhs": "total", "rhs": "taxable + tax" },
    { "lhs": "discount", "rhs": "-base * 0.1" }
  ]
}

Output

[
  { "lhs": "total", "rhs": "taxable + tax", "errors": [] },
  {
    "lhs": "discount",
    "rhs": "-base * 0.1",
    "errors": [
      "Unary operator 'USub' not supported in '-base * 0.1'. Rewrite e.g. '-x' as '(0 - x)'."
    ]
  }
]

Empty errors list means the equation is valid. Catches:

  • Unary negation (-x) and unary plus (+x) — rewrite as (0 - x)
  • Non-constant or zero/negative exponents in ** — only x ** 2, x ** 3, etc. are supported
  • Syntax errors

evaluate_equations

Evaluates equations against known values and returns pass/fail per equation. No OR-Tools involved — useful to check whether values already satisfy the system before deciding to call solve.

Input

{
  "equations": [
    { "lhs": "gross_amount", "rhs": "quantity * rate_per_pc", "relation": "==", "tolerance": 0.5 }
  ],
  "values": {
    "gross_amount": 520,
    "quantity": 10,
    "rate_per_pc": 52
  }
}

Output

[
  {
    "lhs": "gross_amount",
    "rhs": "quantity * rate_per_pc",
    "status": "passed",
    "actual": 520,
    "computed": 520,
    "error": 0.0
  }
]

Status values: passed, failed, missing_values (variable not in values), error (evaluation exception).


optimize

Maximize or minimize an objective expression subject to constraints. Unlike solve, there are no observed values — this is pure optimization: find the best possible assignment of variables within their domains.

Example use cases:

  • Maximize margin (revenue - cost) subject to budget and inventory limits
  • Minimize waste subject to production constraints
  • Find the best allocation of a fixed total across items

Input

{
  "variables": {
    "units": { "min": 0, "max": 1000, "mult_factor": 1 },
    "price": { "min": 0, "max": 500,  "mult_factor": 100 }
  },
  "objective": {
    "expr": "units * price",
    "direction": "maximize"
  },
  "hard_constraints": [
    { "lhs": "units + price", "rhs": "120", "relation": "<=" }
  ],
  "soft_constraints": [
    { "lhs": "price", "rhs": "50", "relation": "<=", "weight": 500, "tolerance": 0 }
  ],
  "timeout_seconds": 5,
  "num_workers": 8
}

Output

{
  "status": "OPTIMAL",
  "values": { "units": 100, "price": 20.0 },
  "objective_value": 2000.0
}
  • values contains all variables (real-world units, divided by mult_factor)
  • objective_value is computed from the real-world values after solving
  • Status values: OPTIMAL, FEASIBLE, INFEASIBLE, UNKNOWN, INVALID_EQUATION

Constraint types

Type Behaviour
hard_constraints Must be satisfied. If any are contradictory the solver returns INFEASIBLE.
soft_constraints Violations are penalised in the objective (for maximize: subtracted; for minimize: added). Same weight/tolerance fields as solve.

Variable spec (simpler than solve — no obs, confidence, or fixed):

Field Default Note
min 0 Domain lower bound (real-world units)
max 2e9 // mult_factor Domain upper bound (real-world units)
mult_factor 1 Same integer-scaling as solve

Supported expression syntax

Both lhs and rhs accept arithmetic expressions over variable names and numeric constants:

Operator Example Notes
+ a + b
- a - b Binary only — -a (unary) is not supported
* qty * rate
/ total / qty RHS divisions are automatically moved to LHS to avoid integer-division loss
% a % b Modulo
** x ** 2 Exponent must be a constant positive integer
Parentheses (a + b) * c

mult_factor and integer scaling

OR-Tools CP-SAT works only with integers. mult_factor scales real-world float values to integers before solving and divides back on return.

  • Amount fields (prices, totals): mult_factor: 100 — values like 52.30 become 5230 internally
  • Quantity fields: mult_factor: 1 — must stay at 1 for multiplicative equations

Why quantities must use mult_factor: 1: for gross = qty × rate, scaling all three by 100 gives gross×100 = (qty×100) × (rate×100), which is off by 100×. With qty at ×1: gross×100 = (qty×1) × (rate×100) — correct.

tolerance is expressed in scaled integer units (after mult_factor is applied). For mult_factor: 100, a tolerance of 200 means ±2.00 in real units.


Setup

pip install -r requirements.txt

Add to Claude Code (~/.claude/settings.json):

{
  "mcpServers": {
    "ortools-solver": {
      "command": "python3",
      "args": ["/path/to/ortools-mcp/server.py"]
    }
  }
}

Running tests

pip install pytest
pytest test_solver.py test_server.py -v

test_server.py mocks the mcp package so it runs without needing mcp installed. 50 tests total covering all three tools, expression validation, scaling, confidence weighting, tolerance, and inequality constraints.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选