Skip to content

ig-prep mcp: convert photographs where they already are - #1

Merged
oddharsh merged 5 commits into
mainfrom
feat/mcp-server
Aug 24, 2026
Merged

oddharsh merged 5 commits into
mainfrom
feat/mcp-server

Conversation

@oddharsh

@oddharsh oddharsh commented Aug 24, 2026 •

Copy link
Copy Markdown
Owner

ig-prep mcp speaks MCP over stdin and stdout, so an assistant with file access can convert photographs on the machine they already sit on. Nothing is uploaded and no pixels enter the conversation.

{
  "mcpServers": {
    "ig-prep": { "command": "ig-prep", "args": ["mcp", "--root", "/Users/you/Pictures"] }
  }
}

Three tools

tool writes what it answers
ig_plan no what a conversion would do, per file
ig_convert yes does it, reports paths and sizes
ig_check_rotation no files whose two rotations disagree

All three go through the same one() the CLI calls, and Opts::defaults() is the CLI's defaults. A tool server that reimplements its own CLI grows a second set of defaults and then disagrees with the documentation.

Decisions worth reviewing

Results carry paths, never image bytes. A converted frame is roughly a megabyte of base64 and a model cannot look at it, so forty of them would spend forty megabytes of context on nothing. The caller can already open the file it named.

--root is checked after symlinks resolve. Comparing the path string a caller wrote would let root/link-to-elsewhere through. Inputs and out_dir are both confined, and the re-check runs after directory expansion, since a directory inside the root can hold a symlink pointing out of it.

A batch stops at 200 files and says how many it left. A result listing 200 conversions of a 900-file directory otherwise reads as a complete run.

Dual-era protocol, 2026-07-28 plus the three legacy revisions. A legacy client pointed at a modern-only server does not negotiate down, it fails, and today every stdio client in the wild opens with initialize.

serde_json is the one new dependency, derive off. Hand-rolling JSON to avoid it means hand-rolling string escapes, which is where a hand-rolled parser goes quietly wrong on a path containing a quote.

The bug this turned up

Every grayscale JPEG had been failing since the tool was written, with source image: Size of buffer is smaller than required raised two layers from the cause and naming neither the file nor its colour.

jpeg_set_out_colorspace(RGB) is a request rather than an instruction. For a single-channel source zune-jpeg declines it and returns Luma anyway, silently, so the buffer came back at one byte per pixel while Rgb::new labelled it three. Fixed by reading the colourspace the decoder actually used, plus a length check against the dimensions at the decode boundary where the file name is still in hand.

Measured on a 160-file library: 1 file failed before, 0 after.

Verification

  • 26 tests pass, up from 9. New coverage: grayscale and colour decode, protocol era selection, version gating, the annotation split between the read tools and the write tool, argument refusal, and root confinement including a symlink escape.
  • cargo clippy --all-targets -- -D warnings and cargo fmt --check both clean.
  • Exercised end to end over real stdio against real photographs: 8 forty-megapixel HEIFs through one call in 2.41s wall at 354% CPU.
  • Confinement refusals confirmed live for an input path (/etc/hosts) and an out_dir (../escape).

Update: clippy 1.98 fix folded in

The first CI run failed lint, and it was not this branch. Rust 1.98 shipped 2026-08-18 with clippy::chunks_exact_to_as_chunks, and CI pins dtolnay/rust-toolchain@stable, so pristine main fails with 6 errors under it. Four of the seven sites predate this branch.

Fixed all seven. as_chunks::<N>().0.iter() is what chunks_exact(N) already meant, so the change is mechanical, and it is checked as mechanical rather than assumed: converting a HEIF and a grayscale JPEG produces byte-identical output to the previous build. Verified on 1.93.0 and 1.98.0 both, since as_chunks stabilised in 1.88 and the older toolchain has to keep building.


Update: chroma split out

The three-way chroma change is now its own commit at the base of the branch (feat: three-way chroma, since 4:2:2 is the honest middle), ahead of everything else, since the MCP tool schema depends on the Chroma type.

The split was verified by content rather than by eye: the final tree is byte-identical to the pre-split branch tip, encode.rs appears in the chroma commit and nowhere else, and the only Chroma:: references left in the MCP commit are its own tool-argument parsing. Every one of the five commits builds and passes its tests on its own (9 → 11 → 26 → 26 → 27), so the series bisects.

`--420` was a boolean against a 4:4:4 default, which left no way to say
the thing a 4:2:2 camera actually shoots. `--444`, `--422` and `--420`
now name the JPEG ratio directly.

The default stays 4:4:4, and the reasoning is worth being precise about,
because the source ratio does not settle it on its own. A Fujifilm HIF
halves chroma horizontally in its own landscape orientation, so a
7728x5152 frame carries a 3864x5152 chroma plane; held vertically that
becomes 5152x3864 and the halved axis lands on the LONG edge of the
portrait. Against a 1440x2160 output that is still 3.58x horizontally and
1.79x vertically of real chroma to average down, so full-resolution
output chroma is measured rather than invented.

That reasoning holds only while the downscale stays large. Anyone
delivering at or near native size has a 4:2:2 source that cannot fill
4:4:4, and should say `--422` and mean it.

Measured on one 1440x2160 frame: 1885 KB at 4:4:4, 1699 at 4:2:2, 1585
at 4:2:0, on an upload nobody sees.

Which of the three survives a round trip best is still unverified, like
the rest of the Instagram-facing assumptions here.
zune-jpeg's `jpeg_set_out_colorspace` is a request rather than an
instruction, and for a single-channel source it declines and returns Luma
anyway. Nothing said so. The buffer came back at one byte per pixel,
`Rgb::new` labelled it as three, and the mismatch surfaced two layers
later inside the resizer as "source image: Size of buffer is smaller than
required", naming neither the file nor its colour.

Expand from the colourspace the decoder actually USED, and check the
length against the dimensions at the decode boundary, where the file name
is still in hand. LumaA and RGBA are handled the same way the PNG path
already handled them; anything else is refused by name rather than
guessed at.

Measured on a 160-file library: 1 file failed before, 0 after. Both tests
build their own input rather than committing a fixture.
`ig-prep mcp` speaks MCP over stdin and stdout. An assistant with file
access names paths on the machine the photographs are already on and gets
back the files it asked for, so nothing is uploaded and no pixels enter
the conversation.

Three tools: ig_plan reports what a conversion would do and writes
nothing, ig_convert does it, ig_check_rotation is --check. All three go
through the same `one()` the CLI calls, and `Opts::defaults()` IS the
CLI's defaults, because a tool server that reimplements its own CLI grows
a second set of defaults and then disagrees with the documentation. The
chroma argument is the previous commit's three-way option exposed as a
tool enum, for the same reason.

Results carry paths, sizes and dimensions rather than image bytes. A
converted frame is roughly a megabyte of base64 and a model cannot look
at it, so forty of them would be forty megabytes spent on nothing.

`--root` confines every path argument, inputs and out_dir alike, resolved
through its symlinks first: comparing the string a caller wrote would let
root/link-to-elsewhere through. A batch stops at 200 files and reports
what it left, since a result listing 200 conversions of a 900-file
directory otherwise reads as a complete run.

Dual-era, like the site's own server: 2026-07-28 plus the three legacy
revisions, because a legacy client pointed at a modern-only server does
not negotiate down, it fails. Today every stdio client in the wild opens
with `initialize`.

`one()` and `run_check()` now return structured Reports that the CLI
renders to the exact lines it printed before. serde_json is the one new
dependency, with derive off; hand-rolling JSON to avoid it means
hand-rolling string escapes, which is where a hand-rolled parser goes
quietly wrong on a path containing a quote.
Rust 1.98 shipped on 2026-08-18 with a new lint, and CI pins
dtolnay/rust-toolchain@stable, so `cargo clippy -- -D warnings` started
failing on code nobody had touched. Measured on pristine main with
clippy 1.98: 6 errors. Four of the seven sites here predate this branch.

`as_chunks::<N>()` returns the complete chunks and the remainder as a
tuple, so `.as_chunks::<N>().0.iter()` is what `chunks_exact(N)` already
meant. Purely mechanical, and checked as such rather than assumed:
converting a HEIF and a grayscale JPEG produces byte-identical output to
the previous build.

Verified on both 1.93.0 and 1.98.0, since `as_chunks` stabilised in 1.88
and the older toolchain has to keep building. 26 tests pass on each.
Three tests used /etc and /etc/hosts as "a path that exists outside the
root". That is true on Unix and false on Windows, so CI's windows-latest
job failed on the test rather than on the boundary it was checking. macOS
and ubuntu passed throughout, which is exactly how a platform assumption
hides.

They build their own directory pair now, one inside the root and one
outside, so the thing under test is the boundary rather than the host's
filesystem layout.

One of the three found a real wart while failing. `call()` resolved the
argument paths BEFORE it looked at the tool name, so an unknown tool with
an unreadable path was answered with the path problem, which sends the
caller to fix the wrong thing. The name is checked first now, and a new
test walks the catalogue to assert every advertised name reaches a
dispatch arm, since the guard and the match have to list the same three.

27 tests, clippy 1.98 and fmt clean.
@oddharsh
oddharsh merged commit c6fa802 into main Aug 24, 2026
4 checks passed
@oddharsh
oddharsh deleted the feat/mcp-server branch August 24, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant