bob-cplex-mcp
# bob-cplex-mcp
**Experimental Alpha** — an MCP (Model Context Protocol) server exposing CPLEX-backed
linear and mixed-integer programming tools, with fail-closed status reporting.
> ⚠️ **Alpha status.** This project does **not** yet claim full LP/MILP status
> reliability. It deliberately reports `unknown` when the underlying solver
> interface cannot establish the true status. See
> [Known Limitations](#known-limitations) before relying on any result.
## What it is
- An MCP server (`stdio` transport) registering two tools:
`solve_linear_program_tool` and `solve_integer_program_tool`.
- Models are built with [PuLP](https://github.com/coin-or/pulp) (4.x compatible)
and solved by an **explicitly selected** solver — CPLEX via PuLP's
`CPLEX_CMD` (file-based) interface by default.
- Every returned solution is **independently verified** against the model's
constraints and objective by plain arithmetic. Verification proves the point
is feasible — it is *not* proof of optimality.
- **Fail-closed status guards** for the two misreport modes proven in the
RC.1.1 spike (see below): results that cannot be established reliably are
returned as `unknown` with no values, never as a confidently wrong status.
## Why fail-closed
The upstream PuLP `CPLEX_CMD` interface infers status from files, which is
lossy. Two defects were proven experimentally (RC.1.1 spike, reproduced as
regression tests in `tests/integration/`):
1. **LP-unbounded → reported `infeasible`.** CPLEX writes no solution file
when it has no solution; PuLP then forces `Infeasible`.
2. **MILP-unbounded → reported `optimal` with objective 0 and all-zero
variables.** The `.sol` file written after `mipopt` describes the fixed-LP
incumbent re-solve, not the MIP outcome.
3. **RC.3 audit additions:** (a) a MIP that stopped on a **time limit with
an incumbent** also surfaces as `optimal` (same fixed-LP `.sol`
mechanism), and without an incumbent as `infeasible`; (b) the RC.2
±1e6-boxed probes could themselves misfire on models whose feasible
region lies beyond the box (wrongly confirming `infeasible` or clearing
an unbounded model as `optimal`).
This server counters with: independent verification of every returned
point; the CPLEX log termination status for MIPs (`MIP - Integer optimal`
vs time-limit/unbounded strings, parsed by PuLP 4.3+ via orloge); an
unboundedness probe that treats inconclusive outcomes as fail-closed
`unknown` (and is skipped entirely for models bounded by construction); and
a zero-objective re-solve at the original bounds to disprove `infeasible`.
Where any check contradicts the verdict or is inconclusive, the status is
downgraded to `unknown` (or `feasible` for a verified but unproven
incumbent).
## Requirements
- Python **3.11+** (3.12 tested)
- A CPLEX executable on `PATH` (e.g., IBM ILOG CPLEX Studio Community Edition).
Without it, solver requests fail with an explicit error — there is **no
silent fallback** to another solver.
- No IBM CPLEX Python binding is required or used (the file-based `CPLEX_CMD`
interface is used exclusively in this alpha).
## Installation
```bash
python -m venv .venv
.venv/Scripts/activate # Windows
# source .venv/bin/activate # macOS / Linux
pip install .
# or for development:
pip install -e ".[dev]"
```
## MCP configuration
Example for clients that launch MCP servers via stdio (adjust paths):
```json
{
"mcpServers": {
"bob-cplex-mcp": {
"command": "C:/path/to/bob-cplex-mcp/.venv/Scripts/bob-cplex-mcp.exe",
"args": []
}
}
}
```
## Usage examples
### Via an MCP client (Bob / Cline)
Ask the agent to solve an LP; it will call `solve_linear_program_tool`:
```
Solve: maximize 3x + 5y subject to x <= 4, y <= 6, 3x + 2y <= 18, x,y >= 0
```
Response fields: `status` (`optimal` / `feasible` / `infeasible` / `unbounded`
/ `time_limit` / `unknown` / `error`), `objective_value`, `variables`,
`solver_info.solver_name`, and — for verified solutions — a `verification`
note. See `examples/` for runnable scripts.
### Direct Python API
```python
from bob_cplex_mcp.tools.linear_programming import solve_linear_program
result = solve_linear_program(
objective={"sense": "maximize", "coefficients": {"x": 3, "y": 5}},
variables={"x": {"type": "continuous", "lower": 0},
"y": {"type": "continuous", "lower": 0}},
constraints=[
{"expression": {"x": 1}, "operator": "<=", "rhs": 4},
{"expression": {"y": 1}, "operator": "<=", "rhs": 6},
{"expression": {"x": 3, "y": 2}, "operator": "<=", "rhs": 18},
],
solver="CPLEX",
)
print(result["status"], result["objective_value"]) # optimal 36.0
```
## Tests
```bash
# CPLEX-free unit tests (what CI runs):
pytest tests/unit -m "not cplex"
# CPLEX-gated integration tests (need cplex on PATH):
pytest tests/integration
```
## Known limitations
1. **Status fidelity is fail-closed, not fully correct.** Through the
file-based `CPLEX_CMD` interface, genuinely unbounded models typically
surface as `unknown` (not `unbounded`) because the interface never
conveys unboundedness. `unknown` is the honest answer available from
this adapter in this alpha.
2. **CPLEX Community Edition size limits** (1000 variables / 1000
constraints) apply when using the free CPLEX.
3. **Solver selection is explicit.** If the requested solver is unavailable,
the request fails; no fallback solver is selected.
4. **The bounded re-solve probes** use an artificial box of ±1e6; models
whose true optimum lies beyond the box may be misjudged by a probe (in
the conservative direction: downgrade to `unknown`).
5. **SSE/HTTP transports are not built** in this alpha (stdio only).
6. Diagnostics from CPLEX's interactive console (the honest "Integer
unbounded" messages) are discarded by PuLP's interface and are not
captured here.
## Roadmap
- CPLEX_PY adapter (requires installing the CPLEX Python binding) to restore
true `unbounded` statuses — the recommended Path from the RC.1.1
architecture spike (Option D: multiple adapters behind one interface).
- Capture and expose CPLEX log/console diagnostics.
- SSE transport; OR-Tools-based tools for non-CPLEX environments.
## Provenance and license
Derived from [mcp-optimizer](https://github.com/dmitryanchikov/mcp-optimizer)
v0.4.1 (commit `65db2231711535033936975e7450032a3a494954`), MIT licensed.
Local modifications: PuLP 4.2.0 compatibility, explicit solver selection,
fail-closed status guards with independent verification, LP/MILP-only tool
surface. See `CHANGELOG.md` and `docs/PROVENANCE.md`.
## Contributing
This is an experimental alpha under active architecture work. Issues and
PRs are welcome; see `docs/` for the architecture notes and known-defect
history before contributing solver-layer changes.
TDQS
Scored across 2 tools
The two tools target distinct problem classes—continuous LP versus integer/MIP—and descriptions clarify the boundary. However, because MILP subsumes LP and the integer tool handles mixed-integer cases, an agent may occasionally misselect when deciding whether discreteness is required.
Both names follow the same predictable snake_case verb_noun_tool pattern: solve_linear_program_tool and solve_integer_program_tool. The redundant _tool suffix is consistent and does not impair readability.
Two tools is thin for a solver server; the surface offers only solving entry points and no auxiliary operations. The count is borderline but the tools themselves are well-scoped.
Core LP and MILP solving are covered, but notable gaps remain: no explicit solution-status retrieval, sensitivity/dual analysis, model validation, solver option configuration, or save/load of formulations. Agents can work around this if solve responses include full results, but the lifecycle is incomplete.