ui-diff-mcp
README.md
# ui-diff-mcp
Compares an expected screen (a mockup, or an earlier screenshot) with an actual screenshot, for
coding agents that can see images. It does the parts a model is bad at, deterministically, and
hands the pictures back so the agent judges them itself:
1. **Even out:** both screens are scaled to one width; the aspect-ratio difference is reported.
A status bar at the top is found and skipped.
2. **Line up:** each horizontal strip of the expected screen is matched to where it sits in the
actual one (one smooth path over all strips, then a sideways correction per strip), so content
that only moved is reported as a *shift*, not as dozens of changes.
3. **Match size:** when offsets grow steadily down the screen, the device draws everything larger
(it is narrower in dp than the mockup, e.g. 360 dp against 402 dp). The actual is shrunk by the
measured factor and compared again, ignoring the one-pixel fringe resizing leaves on edges.
The better of the two comparisons is kept.
4. **Box what still differs:** a pixel diff after lining up, grouped into numbered boxes. A box
whose content reappears a little to the side or below is reported under `moved`, not as a change.
5. **Show it:** `compare.png` (expected | actual | changes, boxes numbered) and a full-resolution
zoom per box (`zoom-N.png`, expected left, actual right).
No models, no GPU, no network. A typical phone screen takes about 5 seconds on the Pi.
## Tools
| Tool | What it does |
| --- | --- |
| `compare_screens` | `expectedPath`, `actualPath`, optional `outDir`, `ignoreTop` (fraction, default `"auto"` = skip a detected status bar, `0` = compare everything), `ignoreBottom`, `threshold` (colour tolerance, default `0.12`), `maxShift` (default `0.12`), `scale` (how much larger the actual draws content, e.g. `402/360`; default `"auto"`, `"width"` turns it off), `inlineImages` (default 4). Returns the JSON below plus the overview and zooms as JPEG images (the PNGs on disk stay full quality). |
| `capture_android_screen` | Screenshot of the phone over adb → PNG, with `widthDp` and a warning for black (off) screens. Uses `UI_DIFF_ADB_EXECUTABLE`, else `~/.local/bin/phone-adb` (pins the Samsung), else `adb`. |
Result JSON: `expected`/`actual` sizes, `aspectMismatchPct`, `changedPct` (after lining up),
`unalignedChangedPct`, `scaleApplied`, `ignoredTop`, `shifts[]` (`fromY`, `toY`, `shiftPx`,
`shiftPct`, `shiftXPx`, in expected pixels), `regions[]` (`id`, `x`, `y`, `w`, `h`, `changedPct`),
`moved[]` (`x`, `y`, `w`, `h`, `dx`, `dy`), `images`, and plain-language `notes`.
## Use
```bash
npm install && npm run build
# MCP (stdio):
claude mcp add ui-diff -- node /home/agent-runner/projects/ui-diff-mcp/dist/src/index.js
# Or straight from a shell:
npx tsx src/cli.ts compare expected.png actual.png [outDir] --ignore-top=0.04
npx tsx src/cli.ts capture out.png
```
How to read a result:
- Start with `notes`. When the reference was made for a different screen width the actual is
compared at matching size (`scaleApplied`); the strip the narrower screen doesn't reach shows grey
and isn't checked. A reference at the device width (360 dp on the Samsung) is still cleanest.
- Then look at `compare.png` and the zooms, and judge each box against the project's rubric
(`/home/agent-runner/projects/_ui-review/RUBRIC.md`).
- For catching regressions in CI, prefer golden tests (Flutter `matchesGoldenFile`, Playwright
`toHaveScreenshot`). This tool is for looking at a difference, not for gating builds.
## How well it works
`eval/run.ts <screenshots> <out>` plants known changes into copies of real screenshots and scores
the comparisons; `eval/widths.ts <out> <url>...` renders pages at 402 px and as a 360 dp phone at 3x
and scores those. The screenshots stay outside the repo. On 19 screens of the owner's apps
(November 2026), every planted colour change, removal, nudge, addition and resized patch was found,
with at most one false box on average. Known weak spots: a section pushed down near the bottom
(15 of 19 clean), JPEG noise on mockups (a stray box on 3 of 19), and a whole screen drawn larger,
where the change is found but about 5 more boxes come from text drawn at a different size. When a
web page reflows at a narrower width, the boxes mark the reflow (wrapped chips, cut-off text),
which is a real difference but can crowd out a small mistake.
## Why it is this small
Until November 2026 this repo was an 18,700-line pipeline: a 3B element locator on a remote GPU,
per-element crop audits by many free vision models, and a large finding-consolidation layer.
Runs took 8–20 minutes and around 68 model calls, and produced dozens of findings per screen
(47 on Ravlo's Today screen). A single look by a current frontier model at a side-by-side found the
same designer-relevant differences in about a minute. What models still get wrong is scale,
alignment and small shifts, so that is all this tool does. The old pipeline is kept under the tag
`archive/full-pipeline-2026-11`.
TDQS
B3.2/5.0
Scored across 7 tools
Disambiguation4/5
Tools are mostly distinct but compare_ui_images and discover_ui_diffs both compare screenshots; descriptions clarify deterministic vs model-based, but could cause temporary confusion.
Naming Consistency2/5
Naming is inconsistent: some tools start with verbs (capture, compare, discover, get, read, start) while ui_diff_model_health is a noun phrase. Also mixing ui_diff and ui_diffs, and get vs read.
Tool Count5/5
7 tools is well-scoped for UI diffing: capture, two compare modes, background run management (start, status, report), and health check. No unnecessary tools.
Completeness4/5
Covers core workflow: capture, compare (sync/async), background run, report retrieval, model health. Minor gap: no tool to list or cancel runs, but not critical.