You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
F-mcp-screenshot — expose the window screenshot as an MCP tool (pixel companion to snapshot) #215
Expose termherd's window screenshot (the F-capture rung-1 PNG, #108) as an MCP screenshot tool, so a driving Claude session can see the rendered UI — the
pixel companion to the text snapshot (#212). Child of #90.
Why it is its own slice
Unlike snapshot/list_sessions, a screenshot is not a pure read of core::App state. The pixels come from an async iced window::screenshot
Task (see Shell::perform_capture), so answering the tool needs a different
bridge shape than the timeout-bounded state read the current bridge provides:
the shell must trigger the window screenshot, await it, encode a PNG, and return
bytes. That async round-trip (and how MCP carries the image content) is the real
work here, kept out of #212 so the snapshot slice stayed a clean state read.
Scope
A bridge path that requests a window screenshot and returns encoded PNG
bytes (or a saved path), bounded by a timeout like the state read.
screenshot MCP tool returning image content.
Best-effort: a window-less/headless run degrades gracefully (no panic),
the text snapshot staying the reliable path.
Depends on
#212 (perception surface) and #193 (live bridge). Reuses the F-capture PNG
pipeline (#108).
Related: #229 (F-mcp-keys — drive the UI by key chords over MCP) and #196 (composed prompt→wait→read, any session kind). The three together are what make a UI gesture verifiable by an agent: press the chord → see the pixels here → read the terminal. Bumped to P1 for that reason.
Why it is not a plain bridge read. Every existing tool answers inside one update: respond(&App, …) reads owned state, perform_action applies and
returns. A screenshot cannot — the pixels arrive from an async iced window::screenshot Task. Precedent exists though: wait_for_status (#195)
already established that a reply may land in a later turn, because ReplyPort
is a shared take-once slot. That is the seam this reuses — but where a wait
parks the port in Shell::waiters, a screenshot moves the port into the
screenshot Task itself and answers from there. No waiter list, no extra Message hop, and a caller that timed out finds the slot already dead
(ReplyPort::answer no-ops).
Payload is the real design constraint. A retina window is ~3000×2000 → a
multi-MB PNG → ~4 MB of base64 in the model's context. Unbounded, this tool
would be unusable for the loop it exists to serve. So the tool takes max_width (default 1200, source width as the ceiling — never upscales) and
downscales before encoding. max_width rather than a scale factor because a
caller does not know the window's pixel size, but does know how much detail it
needs.
Tidy-first, before the feature. Two RGBA helpers are about to have a second
call site: target_dims + resample_nearest are private in record.rs (GIF),
and the PNG encode is welded into capture::write_png (file-only). Rather than
duplicate either — the "an invariant expressed twice will drift" rule in
AGENTS.md — they move first into a pure app::image module: fit_width, resample_nearest, encode_png(rgba, dims) -> Vec<u8>. capture::write_png and the recorder then both call it, and the new tool is a
third reader of the same code, not a second copy.
Shape
tool screenshot { max_width? }
→ Request::Screenshot { max_width }
→ Shell::serve ──► window::latest().then(window::screenshot)
│ (reply port travels with the task)
▼
shot_reply(Option<Screenshot>, max_width) ← pure
│
← Reply::Shot { png, width, height, error }
← ContentBlock::image(base64, "image/png") + { width, height } meta
shot_reply is where the whole decision lives (no window → an explanatory
error; pixels → fit, resample, encode), and it is a pure function of Option<Screenshot> — so the part that needs a real window is reduced to the
two lines that fetch it.
Degradation
A window-less / headless run must not panic and must not look like a protocol
failure. window::latest() yields None, shot_reply produces the error case,
and the tool returns CallToolResult::error — a tool-level error the caller
actually reads (rmcp renders Err(ErrorData) opaquely), pointing at snapshot
as the reliable path. The bound is its own (15 s, not the shared 5 s): a
window round-trip plus a multi-megapixel encode is legitimately slower than a
state read.
Tests to be written first
Pure, no window needed — that is the point of the split:
image::fit_width — proportional height; never upscales past the source;
floors at 1 px.
image::resample_nearest / target_dims — the moved record tests still pass
from their new home (regression on the extraction).
image::encode_png — bytes decode back to the expected dimensions.
shot_reply(None, …) — an error naming the missing window, no panic.
shot_reply(Some(2×1), max_width 1) — resampled to 1×1, PNG decodes to 1×1.
screenshot tool over the test-shell double: image content block, mime image/png, base64 round-trips to the bridge's bytes; max_width default and
explicit value both reach the Request; an error reply becomes an is_error
result mentioning snapshot; a wrong reply kind is an internal error.
Activity
bastien-gallay commentedon Jul 26, 2026
Related: #229 (
F-mcp-keys— drive the UI by key chords over MCP) and #196 (composed prompt→wait→read, any session kind). The three together are what make a UI gesture verifiable by an agent: press the chord → see the pixels here → read the terminal. Bumped to P1 for that reason.bastien-gallay commentedon Jul 26, 2026
Design pass before coding
Why it is not a plain bridge read. Every existing tool answers inside one
update:respond(&App, …)reads owned state,perform_actionapplies andreturns. A screenshot cannot — the pixels arrive from an async iced
window::screenshotTask. Precedent exists though:wait_for_status(#195)already established that a reply may land in a later turn, because
ReplyPortis a shared take-once slot. That is the seam this reuses — but where a wait
parks the port in
Shell::waiters, a screenshot moves the port into thescreenshot Task itself and answers from there. No waiter list, no extra
Messagehop, and a caller that timed out finds the slot already dead(
ReplyPort::answerno-ops).Payload is the real design constraint. A retina window is ~3000×2000 → a
multi-MB PNG → ~4 MB of base64 in the model's context. Unbounded, this tool
would be unusable for the loop it exists to serve. So the tool takes
max_width(default 1200, source width as the ceiling — never upscales) anddownscales before encoding.
max_widthrather than ascalefactor because acaller does not know the window's pixel size, but does know how much detail it
needs.
Tidy-first, before the feature. Two RGBA helpers are about to have a second
call site:
target_dims+resample_nearestare private inrecord.rs(GIF),and the PNG encode is welded into
capture::write_png(file-only). Rather thanduplicate either — the "an invariant expressed twice will drift" rule in
AGENTS.md — they move first into a pure
app::imagemodule:fit_width,resample_nearest,encode_png(rgba, dims) -> Vec<u8>.capture::write_pngand the recorder then both call it, and the new tool is athird reader of the same code, not a second copy.
Shape
shot_replyis where the whole decision lives (no window → an explanatoryerror; pixels → fit, resample, encode), and it is a pure function of
Option<Screenshot>— so the part that needs a real window is reduced to thetwo lines that fetch it.
Degradation
A window-less / headless run must not panic and must not look like a protocol
failure.
window::latest()yieldsNone,shot_replyproduces the error case,and the tool returns
CallToolResult::error— a tool-level error the calleractually reads (rmcp renders
Err(ErrorData)opaquely), pointing atsnapshotas the reliable path. The bound is its own (
15 s, not the shared 5 s): awindow round-trip plus a multi-megapixel encode is legitimately slower than a
state read.
Tests to be written first
Pure, no window needed — that is the point of the split:
image::fit_width— proportional height; never upscales past the source;floors at 1 px.
image::resample_nearest/target_dims— the moved record tests still passfrom their new home (regression on the extraction).
image::encode_png— bytes decode back to the expected dimensions.shot_reply(None, …)— an error naming the missing window, no panic.shot_reply(Some(2×1), max_width 1)— resampled to 1×1, PNG decodes to 1×1.screenshottool over the test-shell double: image content block, mimeimage/png, base64 round-trips to the bridge's bytes;max_widthdefault andexplicit value both reach the
Request; an error reply becomes anis_errorresult mentioning
snapshot; a wrong reply kind is an internal error.feat(app): expose the window screenshot as an MCP tool (F-mcp-screens…
feat(app): average the screenshot when shrinking, and bound pixels no…
fix(app): the frame kit reached two of its three readers (#215 wrap) (#…
chore(planning): close the two gaps a merged board left open (#235)
feat(app): drive the app's own keyboard over MCP (F-mcp-keys, #229) (#…
docs: the README and the book's tool count catch up with the pointer …