ocr-consensus-mcp
by ryanhyma
README.md
# ocr-consensus-mcp
One MCP server that runs two OCR engines and lets a third model pick the better reading.
```
┌──────────────────────────────┐
image/PDF ─►│ ocr_consensus │
├──────────────┬───────────────┤
│ Indra OCR │ MiniCPM-OCR │
│ CPU, ONNX │ llama.cpp │
└──────┬───────┴───────┬───────┘
│ candidates │
▼ ▼
┌──────────────────────────────┐
│ MiniCPM-OCR-Jev (2B scorer) │
│ score 0–1 per candidate │
└──────────────┬───────────────┘
▼
chosen text + both scores
```
Jev is a **selector**, not a text merger. It scores `(original image, candidate text,
rendered candidate)` and the higher score wins. The tool also returns line-level
agreement and the lines unique to each engine so disagreements stay visible.
## Tools
| Tool | Engine | Notes |
|---|---|---|
| `ocr_document` | **Indra OCR** (Datalab) | CPU/ONNX. Markdown, plain text, line boxes, layout blocks, reading order. |
| `ocr_document_minicpm` | **MiniCPM-OCR 1.0** (OpenBMB) | Image → markdown via a llama.cpp server. |
| `ocr_consensus` | Indra + MiniCPM + **MiniCPM-OCR-Jev** | Runs both on the whole page, renders both candidates, scores with Jev, returns the winner. |
| `ocr_regions` | Indra blocks + MiniCPM + **MiniCPM-OCR-Jev** | **Region-level**: crops each layout block, gets Indra + MiniCPM candidates, scores each pair with Jev, assembles the winners in reading order. |
All tools take an **absolute path on the MCP host**. PDFs are supported by
`ocr_document` and `ocr_consensus` (first page is scored).
## Requirements
* Python 3.10–3.13
* [llama.cpp](https://github.com/ggml-org/llama.cpp) with multimodal and
MiniCPM-OCR-Jev decision-model support (the `decision model type:
openbmb-minicpm-ocr-jev` log line must appear). `b12628` works.
* Models:
* `openbmb/MiniCPM-OCR-1.0-gguf` → `MiniCPM-OCR-1_0-Q4_K_M.gguf` + `mmproj-model-f16-4x.gguf`
* `openbmb/MiniCPM-OCR-Jev-gguf` → `MiniCPM-OCR-Jev-Q4_K_M.gguf` + `mmproj-model-f16.gguf`
* Indra models download automatically on first use (~76 MB)
* The official Jev score service from
[OpenBMB/MiniCPM-OCR](https://github.com/OpenBMB/MiniCPM-OCR)
## Install the MCP server
```bash
python3 -m venv .venv
.venv/bin/pip install -e .
```
## Start the model servers
### 1. MiniCPM-OCR
```bash
llama-server \
-m MiniCPM-OCR-1_0-Q4_K_M.gguf \
--mmproj mmproj-model-f16-4x.gguf \
--host 127.0.0.1 --port 8091 \
-c 4096 -ngl 99
```
Use `-ngl 0` for CPU-only.
### 2. Jev forward engine
```bash
llama-server \
-m MiniCPM-OCR-Jev-Q4_K_M.gguf \
--mmproj mmproj-model-f16.gguf \
--host 127.0.0.1 --port 18499 \
-c 32768 -ngl 99
```
Startup must log `decision model type: openbmb-minicpm-ocr-jev`.
### 3. Jev score service
```bash
git clone https://github.com/OpenBMB/MiniCPM-OCR
cd MiniCPM-OCR/scripts/minicpm-ocr-jev
python3 -m venv .venv-score
.venv-score/bin/pip install -r sdk/requirements-score.txt
JEV_BACKEND_CONFIG=sdk/backends/llamacpp.yaml \
PATH=$PWD/.venv-score/bin:$PATH \
bash sdk/start_score.sh 18501 http://127.0.0.1:18499
```
`curl http://127.0.0.1:18501/healthz` must report `"status": "ok"`.
### 4. This MCP server
```bash
MCP_HOST=127.0.0.1 MCP_PORT=8936 .venv/bin/python server.py
```
For a LibreChat Docker container, bind to the Docker bridge and point MCP at it:
```bash
MCP_HOST=172.17.0.1 MCP_PORT=8936 .venv/bin/python server.py
```
## Environment variables
| Variable | Default | Purpose |
|---|---|---|
| `MCP_HOST` | `127.0.0.1` | MCP bind address |
| `MCP_PORT` | `8936` | MCP port |
| `MINICPM_OCR_URL` | `http://127.0.0.1:8091/v1/chat/completions` | MiniCPM llama.cpp endpoint |
| `MINICPM_OCR_MODEL` | `MiniCPM-OCR` | model id sent to llama.cpp |
| `JEV_URL` | `http://127.0.0.1:18501/v1/score` | Jev score service |
| `INDRA_THREADS` | all cores | Indra worker threads |
| `OCR_RENDER_FONT` | Liberation Sans | font used to render candidates for Jev |
## LibreChat
```yaml
mcpServers:
ocr:
type: streamable-http
url: http://host.docker.internal:8936/mcp
requiresOAuth: false
timeout: 120000
serverInstructions: |
Multi-engine OCR. `ocr_consensus` runs Indra + MiniCPM-OCR and uses
MiniCPM-OCR-Jev to choose the better candidate. Paths are on the host.
```
Tools appear as `ocr_document_mcp_ocr`, `ocr_document_minicpm_mcp_ocr`, and
`ocr_consensus_mcp_ocr`.
## Measured on an RX 9070 XT (invoice image)
| Stage | GPU | CPU (`-ngl 0`) |
|---|---|---|
| Indra | — | ~0.1 s |
| MiniCPM-OCR | 0.3 s | 1.7 s |
| Jev (2 candidates) | 0.8 s | 2.9 s |
| `ocr_consensus` | ~2.6 s | ~5 s |
The Jev scores in that test were **Indra 0.055, MiniCPM 0.869**, so MiniCPM was
chosen — matching the visibly cleaner output.
## Region-level consensus
`ocr_regions` uses Indra's layout blocks as the region set:
1. Indra returns blocks with `label` and `bbox` in reading order.
2. Each block is cropped from the page image.
3. Indra's block text and a MiniCPM-OCR reading of the crop are the two candidates.
4. Both candidates are rendered and scored against the crop with Jev.
5. The higher-scoring candidate is kept; winners are assembled in block order.
Example on the invoice probe: the table block went to MiniCPM (`0.051` vs Indra
`0.019`), the date block went to Indra, and the assembled markdown has a clean
HTML table.
## Limitations
* Region-level scoring depends on Indra's block segmentation. Different layout
detectors may produce different regions, and candidates must share the crop.
* `ocr_regions` currently uses the first page of a PDF.
* Candidate rendering uses a plain white page and one font. Jev's scores depend
on the render, so a different font stack changes scores.
* The MCP server reads host paths. LibreChat uploads live inside its container;
add a shared mount or a copy step before pointing these tools at attachments.
* Indra is weak on handwriting and complex merged-cell tables; MiniCPM-OCR’s
full pipeline normally uses a separate layout stage.
## License and credits
Apache-2.0 for this wrapper. The engines keep their own licenses:
* [Datalab Indra](https://github.com/datalab-to/indra) — Apache-2.0
* [OpenBMB MiniCPM-OCR and Jev](https://github.com/OpenBMB/MiniCPM-OCR) — Apache-2.0
* [llama.cpp](https://github.com/ggml-org/llama.cpp) — MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues