Skip to main content
Glama

reflect-mcp

An MCP server for LLM tasks that verify themselves: Generator → second-model Critic → human-in-the-loop gate. Never a silent wrong answer.

tests coverage python license


1. TL;DR

reflect-mcp is a small multi-agent pipeline that checks its own work before surfacing it, exposed as an MCP server so it plugs into Claude Desktop, Cursor, or any MCP client. A Generator extracts a typed object from messy text; a second-model Critic scores every field and the whole extraction; a confidence-thresholded gate either returns the result or, if it isn't confident enough, returns a needs_review payload naming the exact fields a human should confirm.

                 ┌───────────┐      ┌──────────────┐      ┌────────┐      ┌──────────────┐
  messy text ──▶ │ Generator │ ───▶ │ heuristics + │ ───▶ │  Gate  │ ───▶ │  ok  /       │
                 │ (model A) │      │ Critic (B)   │      │ (HITL) │      │  needs_review│
                 └───────────┘      └──────────────┘      └────────┘      └──────────────┘
                   typed JSON        per-field + overall     threshold      flagged fields
                                       confidence            decision       + rationale

Related MCP server: StructureAI MCP Server

2. Why — the trust problem

LLMs are confidently wrong. The failure mode that actually hurts in production isn't a model that says "I don't know" — it's a model that returns a clean, plausible, wrong value with no signal that anything is off. reflect-mcp makes verification a first-class step: a different model critiques the output, deterministic structural checks run alongside it, and anything below a confidence threshold is escalated instead of emitted.

The pattern is well established: naive extraction pipelines look great in a demo and then quietly degrade on real inputs, and adding a multi-model verification + human-review gate is a proven way to claw accuracy back. The point reflect-mcp makes concrete: the verification discipline is the product, not the extraction call.

In research terms — an evaluation harness with a verifiable reward

Stripped of the invoice example, reflect-mcp is a small evaluation harness with a verifiable reward, wired into the inference path instead of bolted on after the fact. The Critic is a decorrelated grader — a different model from the Generator, because same-model self-critique rubber-stamps its own errors. The deterministic heuristics are the programmatic pass criterion: grounding, amount-role grounding (a total must match the number the source labels as the total), party-role grounding (a vendor must not appear only on a bill-to/ship-to line), identifier-role grounding (an invoice number must not appear only on a PO/order/customer/account line), currency-code shape (a currency that is not 3 uppercase letters, e.g. $ or US Dollars, is penalized), arithmetic reconciliation, the tax-rate check (a single stated rate such as VAT 20% must explain the extracted tax), date validity, date grounding, date-role grounding (an invoice date must not appear only as a due or ship date) and the omitted-total check (an explicit null total on a source that labels a total is penalized) are checkable without a model, and they can only lower the score, so a structurally impossible value can't be talked through on a grader's confidence alone. The gate is the escalation path for the cases the reward signal can't settle — which is what turns a low-confidence result into useful output instead of a failure. And make eval grades the harness itself: a labeled golden set, field-level accuracy, a catch rate for wrong fields, and a non-zero exit on low accuracy or any silent-wrong field, so the whole loop is measurable offline in CI with no secrets.

3. Demo

Messy input → typed JSON with per-field confidence → one low-confidence field flagged for review. This is golden row inv-004, replayed offline through the real pipeline (make eval runs the same replay). The source says the amount due is 583.20. The scripted Generator wrote 683.2, an invented total. The gate caught it three ways: the arithmetic check (540.00 + 43.20 ≠ 683.2), the heuristic source search, and the Critic's grounded: false verdict.

Input:

Hi team, forwarding the bill from Globex Ltd below.
Ref: GLX/2026/778
Billing date 2026-02-28.
Items came to 540.00 before tax, tax was 43.20, and the amount due is 583.20. Please pay in GBP. Thanks!

Output (trace_id is per-run and shown here as a placeholder):

{
  "status": "needs_review",
  "data": {
    "vendor_name": "Globex Ltd",
    "invoice_number": "GLX/2026/778",
    "invoice_date": "2026-02-28",
    "subtotal": 540.0,
    "tax": 43.2,
    "total": 683.2,
    "currency": "GBP"
  },
  "field_confidence": {
    "vendor_name": 0.95,
    "invoice_number": 0.95,
    "invoice_date": 0.95,
    "subtotal": 0.95,
    "tax": 0.95,
    "total": 0.12,
    "currency": 0.95
  },
  "overall_confidence": 0.7,
  "flagged_fields": [
    {
      "field": "total",
      "value": 683.2,
      "confidence": 0.12,
      "reason": "683.20 does not reconcile with 540.00 + 43.20 and is not in the source; critic marked the value not grounded in the source; amount 683.2 not found in source text; subtotal(540.0) + tax(43.2) != total(683.2)"
    }
  ],
  "field_provenance": {
    "vendor_name": [
      {
        "start": 34,
        "end": 44
      }
    ],
    "invoice_number": [
      {
        "start": 57,
        "end": 69
      }
    ],
    "invoice_date": [
      {
        "start": 83,
        "end": 93
      }
    ],
    "subtotal": [
      {
        "start": 109,
        "end": 115
      }
    ],
    "tax": [
      {
        "start": 136,
        "end": 141
      }
    ],
    "total": [],
    "currency": [
      {
        "start": 187,
        "end": 190
      }
    ]
  },
  "trace_id": "trace-…",
  "critic_summary": "Total appears hallucinated; escalate.",
  "notes": "1 field(s) below the 70% confidence threshold require human review before use."
}

(A ~20s GIF of this in Claude Desktop goes here in the published repo.)

4. Use it in Claude Desktop

Install and point Claude Desktop at the server. Run reflect-mcp --mcp-config (or .venv/bin/python -m reflect_mcp --mcp-config) and paste its output into your claude_desktop_config.json (macOS: ~/Library/Application Support/Claude/claude_desktop_config.json). It prints the block below with command set to this install's absolute interpreter (hosts do not inherit the venv's PATH), the key variable for your REFLECT_PROVIDER (none for bedrock, which uses the AWS credential chain), and any REFLECT_* model, threshold or log-level variable you have set. The key is always a placeholder: replace <your-api-key> with your own; the command never prints a secret. An unknown REFLECT_PROVIDER exits 2.

{
  "mcpServers": {
    "reflect-mcp": {
      "command": "/ABSOLUTE/PATH/reflect-mcp/.venv/bin/python",
      "args": ["-m", "reflect_mcp.server"],
      "env": {
        "REFLECT_PROVIDER": "anthropic",
        "REFLECT_GENERATOR_MODEL": "claude-3-5-sonnet-latest",
        "REFLECT_CRITIC_MODEL": "claude-3-5-haiku-latest",
        "ANTHROPIC_API_KEY": "<your-api-key>"
      }
    }
  }
}

Then restart Claude Desktop and ask it to "extract the invoice fields from this text …" — it will call the extract tool. You can also ask it to "validate this data against the source" to run verification without re-extracting.

Tools exposed

Tool

Args

Returns

extract

text: str, field_threshold=null, overall_threshold=null, heuristics_only=False

{ status, data, field_confidence, overall_confidence, flagged_fields, field_provenance, critic_summary, trace_id, notes }

validate

text: str, data: dict, field_threshold=null, overall_threshold=null, heuristics_only=False

Same ExtractionResult envelope as extract

When status == "needs_review", do not use data blindly — confirm the fields in flagged_fields first.

Typed results. Both tools publish an outputSchema in tools/list whose only required key is a status enum (ok / needs_review / error), so a host knows the envelope before the first call. Every result carries structuredContent (the envelope above, with the same JSON as the text content), and a status: "error" result sets isError: true, so a host that branches on the MCP error flag never treats a failed extraction as a result.

Host hints. The initialize handshake returns server instructions stating the ok / needs_review / error contract (confirm flagged_fields with a human before using data), which many hosts add to the model's context. Both tools also list a human title and annotations: readOnlyHint: true and destructiveHint: false (they only read their input), idempotentHint: true (repeating a call has no extra effect), and openWorldHint: true, because extract calls an external model (the configured provider), so a host can auto-approve them as safe reads.

The tools never raise: failures (empty input, out-of-range thresholds, data that fails InvoiceData validation, provider or parse errors) return { status: "error", error_message } instead of raising, mirroring the CLI's error envelope. When extract fails in the Critic stage (an unparseable critique), the error envelope also carries the Generator's extraction as data. It is unverified (no Critic verdict, status stays "error"), so pass the whole envelope back to validate to re-verify it without another Generator call. validate also accepts a stored reflect-mcp result envelope as data (e.g. an earlier extract result) and verifies its data object; an envelope with no data object (a stored error row from an input, provider or Generator failure) is a schema error, returned as an error envelope. A data key that is not an InvoiceData field (a typo such as vendr_name) is likewise a schema error naming the key (status:"error"), never a silently skipped field; a stored envelope's own keys (status, notes, ...) are exempt, since only its data is checked. heuristics_only=True skips the Critic LLM call, like the CLI's --heuristics-only (an ok result's notes then begin "No Critic ran"); for validate it builds no provider client at all, so it needs no provider SDK or API key.

Parallel calls. Both tools are async and run the pipeline on a worker thread, so parallel tool calls from one client overlap instead of queuing, and the server keeps answering other requests (pings, cancellations, tools/list) while an extraction runs. A cancelled call's pipeline still runs to completion on its worker thread (including any model calls); only its result is discarded.

Operator threshold floor. Over MCP, the threshold arguments are chosen by the calling agent, so they can only be raised above the operator's floor, never lowered below it. Set the floor in the server's env block with REFLECT_FIELD_THRESHOLD and REFLECT_OVERALL_THRESHOLD (defaults 0.70 / 0.75 when unset). An omitted (or null) threshold means 0.70 / 0.75, or the floor if higher, and adds no note. An explicit argument below the floor is raised to it, and notes records it (e.g. field_threshold raised from 0.0 to 0.7.). An agent may always ask for stricter thresholds; only the operator can lower them, e.g. "REFLECT_FIELD_THRESHOLD": "0". (The agent-controlled heuristics_only=True argument still drops the Critic, so it is not covered by this floor.) A malformed value (not a number, including nan/inf, or outside [0, 1]) makes every call return { status: "error", error_message } rather than falling back to a looser gate. The CLI does not read these variables: its --field-threshold / --overall-threshold flags stay authoritative.

Honest ok notes. An ok result's notes never let a heuristic-penalized field pass silently: under a lowered threshold, they name each heuristic-penalized field that still cleared it, with its factor (e.g. Heuristics penalized total (factor 30%); each penalized field still cleared the 30% field threshold.), and when every value is null they say nothing was verified.

5. CLI

For scripting, CI pipelines, or batch processing without an MCP client:

# discover subcommands and flags
reflect-mcp --help
reflect-mcp --version
reflect-mcp extract --help

# extract: generate + verify
echo "ACME CORP, Invoice #INV-1001, Total: \$108.50" | reflect-mcp extract
reflect-mcp extract --file invoice.txt
reflect-mcp extract --field-threshold 0.85 --overall-threshold 0.80 --file invoice.txt

# batch extract: process a corpus as JSONL (one {"text": "..."} per line)
cat invoices.jsonl | reflect-mcp extract --batch
reflect-mcp extract --batch --file invoices.jsonl
reflect-mcp extract --batch --field-threshold 0.85 --overall-threshold 0.80

# concurrent batch: overlap LLM calls across documents (4 workers)
cat invoices.jsonl | reflect-mcp extract --batch --batch-concurrency 4

# heuristics-only: skip the Critic LLM call, use only deterministic checks
echo "ACME CORP, Invoice #INV-1001, Total: \$108.50" | reflect-mcp extract --heuristics-only

# validate: verify pre-extracted data (no Generator, Critic + heuristics + gate only)
reflect-mcp validate --file invoice.txt --data '{"vendor_name":"ACME CORP","total":108.5}'
reflect-mcp validate --file invoice.txt --data '{"vendor_name":"ACME CORP"}' --field-threshold 0.5
reflect-mcp validate --file invoice.txt --data '{"vendor_name":"ACME CORP"}' --heuristics-only

# batch validate: verify your own extractor's output over a JSONL corpus in one process
# (one {"text": "...", "data": {...}} per line; no SDK or key needed with --heuristics-only)
reflect-mcp validate --batch --heuristics-only --file extracted.jsonl
cat extracted.jsonl | reflect-mcp validate --batch --batch-concurrency 4

reflect-mcp --version prints the same version an MCP host sees as serverInfo.version in the initialize handshake.

--file - reads stdin, exactly as omitting --file does, so a wrapper or Makefile can pass - for piped input; a file literally named - is passed as ./-.

validate --data - reads the fields JSON (a bare object or a stored result envelope) from stdin, so the two verbs join in one pipe: reflect-mcp extract --file invoice.txt | reflect-mcp validate --file invoice.txt --data -. --file PATH is then required, since stdin cannot carry both the fields and the source text.

Exit codes (both verbs): 0 = ok, 1 = needs_review, 2 = usage/input error, 3 = pipeline error (JSON error envelope on stdout). When extract fails in the Critic stage, that envelope (and a --batch error row, after its id) also carries the Generator's extraction as data: it is unverified (no Critic verdict, still exit 3), so re-verify it without another Generator call: reflect-mcp extract --file DOC | reflect-mcp validate --file DOC --data -. If stdout's reader closes early (e.g. | head -1), the CLI stops reading input and exits with the worst code among the rows it produced before noticing the close (rows already written to the pipe that the reader never read may count, so the code is never more lenient), with no traceback (never 120), so a set -o pipefail job sees the same 0/1/2/3 contract.

Heuristics-only mode (--heuristics-only): skips the Critic LLM call entirely. Verification relies solely on deterministic structural checks (text grounding, amount-role grounding, party-role grounding, identifier-role grounding, currency-code shape (not 3 uppercase letters → penalized), arithmetic reconciliation, tax rate (a single stated Tax/VAT/GST rate that no base reproduces → tax penalized), date validation, date grounding, date-role grounding, omitted total (an explicit null total on a source that labels a total → penalized)). Useful for CI pipelines, pre-screening, or zero-cost structural-only validation. Works with both extract and validate, including batch mode. validate --heuristics-only builds no provider client at all, so it needs no provider SDK or API key (extract still builds the Generator client, so it still needs them). An ok from this mode says so: its notes begin "No Critic ran" (field confidences are heuristic factors, not a Critic's judgement) and critic_summary stays empty, and the log records critic.skipped (reason: heuristics_only) instead of critique.done (the same event, with reason: all_fields_doomed, marks a run where the Critic is configured but skipped because every field already failed its heuristics).

Batch mode (--batch): reads JSONL from stdin or --file, emits one JSON result per line to stdout, reuses the pipeline across all lines. extract --batch lines are {"text": "..."}; validate --batch lines are {"text": "...", "data": {...}}, where data holds the fields to verify (pass --batch instead of --data). A line missing "data" or whose data fails schema validation gets its own error line. --data and a line's data may also be a stored reflect-mcp result envelope (e.g. a saved extract row): its data object is what gets verified, and an envelope with no data object (a stored error row from an input, provider or Generator failure) is a schema error (exit 2 for --data, an error row in --batch). A data key that is not an InvoiceData field (e.g. a typo such as vendr_name) is also a schema error naming the key and listing the known fields: exit 2 for --data, an error row in --batch, status:"error" over MCP. A stored envelope's own keys are exempt; only its inner data is checked. Amounts (subtotal, tax, total) must be JSON numbers (or numeric strings); a boolean amount such as "total": true is a schema error rather than being read as 1.0 (exit 2 for --data, an error row in --batch, status:"error" over MCP). A line's "id" (any JSON value) is echoed verbatim as the first key of its output row, error rows included, so output joins to input by id; lines that are not JSON objects carry no id. A UTF-8 BOM at the start of the input is ignored, and blank or whitespace-only lines are skipped (they emit no row). Exit code is the worst severity across all lines (error 3 > needs_review 1 > ok 0). Malformed lines and per-line extraction failures emit a {"status":"error","error_message":"..."} row (led by id when the line was an object carrying one) without aborting the batch. Add --batch-concurrency N (N > 1) to process documents concurrently via a thread pool; each worker builds its own pipeline instance so SDK clients are never shared across threads. Output order is always preserved. Each output row is flushed as it is written, at any --batch-concurrency, so a piped consumer (... --batch | consumer) sees it immediately rather than after an 8 KB buffer fills, and a killed batch never loses a row it already printed. Concurrent mode keeps at most 2N lines in flight, so output streams in input order and memory stays bounded. The pipeline is built once up front, so a pipeline/provider build failure or an invalid threshold flag yields a single status:"error" line and exit 3 at any --batch-concurrency.

The bare reflect-mcp command (no subcommand) still starts the MCP server.

6. Use as a Python library

Install reflect-mcp as a dependency and import directly — the package ships a py.typed marker for full type-checker support.

Full extraction pipeline

from reflect_mcp import build_default_pipeline, GateConfig, ExtractionResult

pipeline = build_default_pipeline(
    gate_config=GateConfig(field_threshold=0.80, overall_threshold=0.85),
)
result: ExtractionResult = pipeline.extract("ACME Corp, Invoice #A-99, total $250.00")

if result.status == "needs_review":
    for flag in result.flagged_fields:        # list[FlaggedField]
        print(f"{flag.field}: {flag.reason}")
else:
    print(result.data.vendor_name)            # InvoiceData fields

build_default_pipeline reads REFLECT_PROVIDER / REFLECT_GENERATOR_MODEL / REFLECT_CRITIC_MODEL from the environment and returns a ReflectivePipeline. Pass critic_enabled=False to skip the Critic LLM call and use only deterministic heuristics for verification. Pass generator_enabled=False for a validate-only pipeline (validate-only: builds no Generator client); calling .extract() on it raises RuntimeError. You can also construct one manually with your own Generator and Critic instances (useful for testing with ScriptedLLMClient).

Gate-only verification

If you already have extracted data and just need the confidence gate:

from reflect_mcp import (
    decide,
    GateConfig,
    InvoiceData,
    CritiqueResult,
    FieldVerdict,
)
from reflect_mcp.heuristics import run_heuristics

data = InvoiceData(vendor_name="ACME Corp", total=250.0, currency="USD")
critique = CritiqueResult(
    field_verdicts=[
        FieldVerdict(field="vendor_name", confidence=0.95, grounded=True),
        FieldVerdict(field="total", confidence=0.95, grounded=True),
        FieldVerdict(field="currency", confidence=0.95, grounded=True),
    ],
    overall_confidence=0.92,
)
heuristics = run_heuristics("ACME Corp, total $250.00 USD", data)
result = decide(data, critique, heuristics, trace_id="demo")
# result.status == "ok"

Exported symbols

The public API (reflect_mcp.__all__) exposes:

Category

Symbols

Schema models

InvoiceData, CritiqueResult, FieldVerdict, FlaggedField, ProvenanceSpan, ExtractionResult, EXTRACTION_FIELDS

Pipeline

ReflectivePipeline, build_default_pipeline

Generator

Generator, GeneratorError

Critic

Critic, CriticError

Gate

GateConfig, decide

All models are Pydantic BaseModel subclasses. Errors (GeneratorError, CriticError) are raised on parse failures after retries are exhausted.

Error contract. Unlike the MCP tools and the CLI (which return or print an error, e.g. CLI exit 2 with Error: ... on stderr), the library raises. pipeline.extract() / .validate() raise GeneratorError / CriticError once a stage's reply still fails to parse or validate after max_retries retries, so each stage makes at most max_retries + 1 model calls (build_default_pipeline(max_retries=1) by default, so at most 2 model calls per stage). A provider or SDK error (network, auth, rate limit) is not retried and propagates unwrapped, as the SDK's own exception type after 1 call, so catch it separately: except (GeneratorError, CriticError) alone does not catch it. .extract() on a pipeline built without a Generator raises RuntimeError. When the Critic fails inside .extract(), the CriticError keeps the Generator's extraction as exc.data (a dict; None from .validate(), whose caller already holds the data): it is unverified (no Critic verdict), so re-verify it with pipeline.validate(text, InvoiceData.model_validate(exc.data)) instead of paying for another Generator call.

7. Architecture

Three roles, one shared trace id, clean layering (resolver-free by design — the pipeline has no infra dependency):

  • Generator (generator.py) — LLM call (with configurable retry on parse failure; the retry tells the model what was rejected) → validated InvoiceData (Pydantic). Absent fields come back null; the model is instructed never to invent a value.

  • Critic (critic.py) — a different model (REFLECT_CRITIC_MODEL) is shown the source and the candidate extraction and must ground each field, returning per-field + overall confidence (with configurable retry on parse failure). A different model matters: self-critique with the same model tends to rubber-stamp its own errors.

  • Deterministic heuristics (heuristics.py) — a free, non-LLM signal: grounding (is a committed value actually present in the source? A currency code also grounds on an unambiguous written name for it, so US Dollars, pounds sterling or Japanese yen grounds USD, GBP or JPY, and a name vouches only for its own code; a bare dollars grounds nothing), amount-role grounding (a grounded subtotal/tax/total must match a number the source labels for that role, e.g. Total: 75.00, so a swapped amount or a stray item-line number is penalized), party-role grounding (a grounded vendor_name that appears only on counterparty lines such as Bill to:, Ship to:, Sold to: or Deliver to: names the buyer, not the seller, so it is penalized), identifier-role grounding (a grounded invoice_number that appears only on lines starting with a PO, order, customer, client or account label, e.g. PO Number: or Customer ID:, is another document's identifier, so it is penalized when some other line carries an invoice-number label such as Invoice No: TH-5120; with no such label, or one unlabelled occurrence, it abstains), currency-code shape (a currency that is not exactly 3 uppercase letters, the ISO 4217 shape, such as $, usd or US Dollars, is penalized even when the source contains it), arithmetic reconciliation (subtotal + tax == total), ISO-date validation, and date grounding (an ISO date must appear in the source as ISO, MM/DD/YYYY/DD/MM/YYYY/DD.MM.YYYY, or an English month-name form such as March 14, 2026 / 14 Mar 2026, otherwise it is penalized), and date-role grounding (a grounded invoice_date whose every occurrence is labelled as a due, payment, ship or delivery date, e.g. Due date:, Pay by, Shipped: or Delivery date:, is a different date the invoice states, not its own date, so it is penalized; one unlabelled occurrence is enough to abstain), and the omitted-total check (an explicit "total": null on a source that labels a total, e.g. Total: 583.20, is penalized with the reason source labels total as 583.20, extraction left it null; an omitted total key is not checked, so a partial-data validate call keeps its result, and total is the only field this check covers), and the tax-rate check (when the source states exactly one rate on a Tax/VAT/GST label, e.g. VAT 20%, the extracted tax must equal that rate applied to subtotal, to total - tax, or as the tax-inclusive share of total, within 0.02; otherwise tax is penalized with a reason such as source states a 10% tax rate; no base it applies to gives tax(10.0), so a rate misread as the tax amount is caught; no stated rate, or two different rates, abstains). These cap the Critic — a structurally impossible value can't be waved through on the Critic's word alone.

  • Gate (gate.py) — combines the two signals multiplicatively (combined = critic_confidence × heuristic_factor), flags any non-null field below field_threshold (plus a null total the omitted-total check penalized), and escalates the whole result if any field is flagged or overall confidence is below overall_threshold. It also enforces the Critic's contract: a committed (non-null) field the Critic marks grounded:false, or returns no verdict for, has its critic confidence capped at 0.5, and duplicate verdicts for a field use the minimum confidence (on a tie, the grounded:false verdict wins, so verdict order never matters).

The extract tool (server.py) is the only module that imports the mcp SDK, and providers.py is the only module that imports a provider SDK — so the core pipeline and the entire test suite have zero hard dependency on either.

8. Evaluation

A tiny golden-file eval treats LLM quality as measurable, not vibes. eval/golden/dataset.jsonl holds labeled messy-text → expected-fields examples; eval/run_eval.py runs the full pipeline over them and reports field accuracy, exact-match accuracy, needs-review rate, and exactly where it fails. It also grades the trust contract itself: catch rate (wrong fields the gate named in flagged_fields), silent-wrong (a wrong field not named in flagged_fields — returned as if it were fine), and false alarms (correct fields flagged anyway; reported, never gated). Each failure line is tagged (flagged) or (SILENT).

make eval          # OFFLINE: deterministic, uses recorded model outputs (no API keys)
make eval-live     # LIVE: runs against REFLECT_PROVIDER (needs credentials)

Offline run (recorded fixtures; eight examples carry an injected hallucination — a wrong total in inv-004, in inv-006 a made-up date that the date-grounding heuristic catches, in inv-007 the bill-to customer extracted as the vendor, which the Critic marks grounded:false and party-role grounding catches too, also in heuristics-only mode, in inv-008 a stray item-line amount taken as the total, which amount-role grounding catches even though the Critic is fooled, and in inv-009 a due date extracted as the invoice date, which date-role grounding catches even though the Critic is fooled, and in inv-010 the PO number extracted as the invoice number, which identifier-role grounding catches even though the Critic is fooled, and in inv-011 a currency written out as US Dollars instead of its ISO code, which the currency-code shape check catches even though the Critic is fooled, and in inv-013 an explicit null total on a source that labels Total: 583.20, which the omitted-total check catches even though the Critic raises no doubt about it — to prove the harness catches misses):

==============================================================
reflect-mcp eval  |  mode: OFFLINE (replay fixtures)
==============================================================
examples          : 13
field accuracy    : 91.2%  (83/91)
exact-match acc   : 38.5%  (5/13)
needs-review rate : 61.5%  (8/13)
catch rate        : 100.0%  (8/8)
silent-wrong      : 0
false alarms      : 0
--------------------------------------------------------------
field-level failures (where it breaks):
  [inv-004] total: expected 583.2 but got 683.2  (flagged)
  [inv-006] invoice_date: expected '2026-03-14' but got '2026-04-14'  (flagged)
  [inv-007] vendor_name: expected 'NORTHWIND TRADERS' but got 'Contoso Ltd'  (flagged)
  [inv-008] total: expected 75.0 but got 45.0  (flagged)
  [inv-009] invoice_date: expected '2026-07-01' but got '2026-07-31'  (flagged)
  [inv-010] invoice_number: expected 'TH-5120' but got 'PO-88213'  (flagged)
  [inv-011] currency: expected 'USD' but got 'US Dollars'  (flagged)
  [inv-013] total: expected 583.2 but got None  (flagged)
==============================================================
PASS: field accuracy 91.2% >= required 90.0%
PASS: silent-wrong fields 0 <= allowed 0

make eval exits non-zero when field accuracy is below --min-accuracy (default 0.90) or silent-wrong is above --max-silent-wrong (default 0), so it works as an offline eval-CI gate with no secrets — and a regression that drops a field without flagging it fails the gate even while accuracy stays above threshold.

9. Testing

Deterministic tests around a stochastic core. The single LLM boundary (LLMClient) is mocked with a ScriptedLLMClient that returns canned JSON and raises if it's ever asked for more than expected — so pytest runs fully offline, with no API keys, and can't leak a real call.

make install       # .venv + package + dev/server deps
make test          # deterministic offline suite
make cov           # with coverage

CI (.github/workflows/ci.yml) runs this offline suite and make eval on Python 3.10 (the requires-python floor) and 3.13 on every push to main and every pull request.

The flagship test is tests/test_critic_catches_error.py: the Generator is fed a deliberately wrong value (vendor_name = "Invoice" — a word that does appear in the source, so the deterministic grounding check can't catch it), and the test asserts the second-model Critic catches it and the gate routes to needs_review. It also flips only the Critic's verdict and shows the decision flip to ok — proving the verification step is doing real work, not the plumbing.

10. Design decisions

  • Second-model Critic (multi-model reflection), not self-critique. The Critic defaults to a different, cheaper model than the Generator (sonnet generates, haiku critiques). Cross-model disagreement is a real signal; same-model self-review is mostly agreement theater.

  • Provider switching via env var. REFLECT_PROVIDER selects anthropic | openai | bedrock, so a reviewer runs it against whatever key they have. The Bedrock (Claude) path is there for AWS users; the public-API paths (Anthropic/OpenAI) let anyone try it without AWS. Provider SDKs are optional extras (pip install 'reflect-mcp[anthropic]') and imported lazily.

  • Confidence is multiplicative. critic_confidence × heuristic_factor — evidence can only lower confidence. A structurally impossible value (bad arithmetic, ungrounded amount) is capped regardless of how sure the Critic sounded.

  • Nulls are honest. A field absent from the source returns null and is never flagged — abstention is correct behavior, not a low-confidence guess. The one exception is total: an explicit null total on a source that labels a total is an omission, not an abstention, so it is flagged with the labelled value(s); an omitted total key is not checked.

  • Prompt versioning + injection awareness. Prompts live in versioned files (src/reflect_mcp/prompts/). Both instruct the model to treat document content as data, not commands — the source text is untrusted input, a basic prompt-injection guard.

  • Observability. Every stage emits a structured JSON log line carrying a shared trace_id (unique per call, even across concurrent MCP calls), so one extraction is greppable end to end.

11. What I'd productionize next

  • Caching of Generator/Critic calls; per-request cost/latency budgets.

  • A real human-review queue (persist needs_review payloads, route to a reviewer UI, feed corrections back as new golden examples).

  • Live eval on a schedule — the offline make eval gate already runs in CI on every push to main and every PR; add periodic live eval to catch model drift.

  • Confidence calibration — replace hand-set thresholds with thresholds tuned on the labeled set to hit a target precision.

  • Streaming + partial results, and a richer schema library (invoices, receipts, resumes, log lines) behind the same verify-then-gate loop.


Repository layout

reflect-mcp/
├── src/reflect_mcp/
│   ├── schema.py          # Pydantic extraction + verdict/decision models
│   ├── providers.py       # provider switching (Anthropic/OpenAI/Bedrock), lazy SDKs
│   ├── scripted.py        # ScriptedLLMClient — the mock at the LLM boundary
│   ├── generator.py       # Generator stage
│   ├── critic.py          # second-model Critic stage
│   ├── heuristics.py      # deterministic structural checks
│   ├── gate.py            # confidence gate / HITL decision
│   ├── pipeline.py        # orchestration (generate → heuristics → critique → decide)
│   ├── logging_config.py  # structured JSON-line logging
│   ├── parsing.py         # robust JSON extraction, prompt loader, shared retry loop
│   ├── prompts/           # versioned generator.txt / critic.txt
│   ├── server.py          # FastMCP server exposing `extract` and `validate` tools
│   ├── cli.py             # CLI entry points: `reflect-mcp extract` / `validate`
│   └── __main__.py        # argparse dispatch: --help/--version/subcommands → cli, bare → server
├── eval/
│   ├── golden/dataset.jsonl   # labeled golden examples
│   ├── fixtures/replay.json   # recorded model outputs for offline eval
│   └── run_eval.py            # eval harness
├── tests/                 # deterministic, fully offline
├── Makefile               # install / test / cov / eval targets
├── pyproject.toml
└── LICENSE

License

MIT — see LICENSE.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers