Skip to content

MCP screenshot: on-device OCR and a normalized grid (#393) - #402

Open
marcoslor wants to merge 1 commit into
mainfrom
t3/mcp-117-screenshot-ocr-grid
Open

marcoslor wants to merge 1 commit into
mainfrom
t3/mcp-117-screenshot-ocr-grid

Conversation

@marcoslor

Copy link
Copy Markdown
Owner

Fixes #393 (MCP-117)

Outcome

screenshot gains two optional booleans, both off unless asked, with no wire or Android change and no new dependency:

  • ocr: true runs Apple's on-device Vision (VNRecognizeTextRequest, .accurate) on the clean decoded frame after any ref crop and before any grid overlay. The answer carries ocr, one item per line in reading order, each {text, box, at}: box in the window's display pixels (the space of ui_tree's box, a ref crop's offset added back and clamped to size) and at the box's centre in 0..1 of the window, rounded to 3 decimals — directly usable in tap. Vision's bottom-left normalized boxes are converted to the top-left display space, pinned by a synthetic card with words at known top and bottom rows. The key is present only when asked ([] when nothing was read). Recognized text lives in the tool result only; no log line carries it (MCP-14).
  • grid: true draws a subtle, labelled 0.1…0.9 grid of both axes onto the PNG in the image content block, in the same 0..1 space tap takes; a ref crop draws only the steps it covers, still labelled with their whole-window value. It runs after OCR, so a line never reaches a word, and the PNG's pixel size is unchanged.

Schemas and docs move in the same commit: input/output schemas (strict additionalProperties: false, every ocr field described), the tool description matches docs/design/mcp.md word for word (MCP-7), docs/specs/mcp.md gains MCP-117 with proofs, and docs/MAP.md lists the new file.

Checks

$ scripts/check-rules.sh --base $(git merge-base HEAD main)
check-rules: PASS (base fd321e20828562dbd4ffefa6e48d398d8dc6cd06, 12 files checked)

$ scripts/check-ratchets.sh --base $(git merge-base HEAD main)
check-ratchets: PASS (base fd321e20828562dbd4ffefa6e48d398d8dc6cd06)

$ scripts/check-format.sh --base $(git merge-base HEAD main)
check-format: PASS (base fd321e20828562dbd4ffefa6e48d398d8dc6cd06, 8 Swift files, 0 Kotlin files)

$ cd macos && swift build && swift test --filter "ContiguaCoreTests|MCPCoreTests|ScreenshotSourceTests|MCPScreenshotOCRTests|MCPToolHostTests"
Build complete! (2,31 secs)
Test Suite 'ContiguaCoreTests.xctest' passed at 2026-12-03 19:05:46.702.
	 Executed 433 tests, with 0 failures (0 unexpected) in 30.322 (30.360) seconds
Test Suite 'ContiguaTests.xctest' passed at 2026-12-03 19:05:15.508.
	 Executed 117 tests, with 0 failures (0 unexpected) in 66.685 (66.702) seconds

$ python3 -m unittest discover -s tools/mcp-eval -p 'test_*.py'
..................................
----------------------------------------------------------------------
Ran 34 tests in 1.038s

OK

New tests are tagged [MCP-117]: MCPCoreTests.testScreenshotTakesOcrAndGrid_MCP117, testScreenshotOutputSchemaDeclaresOcr_MCP117; ScreenshotSourceTests.testOcrIsAnsweredOnlyWhenAsked_MCP117; MCPScreenshotOCRTests (box/at conversion top vs bottom, ref-crop offset and clamp, reading order, a synthetic card's known rows, crop-fraction grid mapping, grid pixels/PNG size, grid drawn after OCR); plus suite_screen.py's flag/schema checks.

The unfiltered swift test pre-existing teardown stall (#161) is why the filter above is used; no test processes were left running.

MCP-117: `screenshot` takes two optional booleans, both off unless asked.

ocr: true runs Apple Vision (`VNRecognizeTextRequest`, accurate) on the
clean cropped frame — before any overlay — and answers `ocr`, one item per
line in reading order with `text`, `box` in the window's display pixels
(the space of `ui_tree`'s box, a ref crop's offset added back and clamped to
`size`) and `at`, the box's centre in 0..1 of the window rounded to three
decimals, straight into `tap`. Vision normalizes its boxes bottom-left; the
conversion to the top-left space is pinned by tests with words drawn at
known top and bottom rows. The key is present only when asked (empty when
nothing was read), and recognized text lives in the tool result only: no
log line carries it (MCP-14).

grid: true draws a subtle labelled 0.1…0.9 grid of both axes on the PNG the
`image` block carries, in the same 0..1 space `tap` takes; a ref crop draws
only the steps it covers, still labelled with their whole-window value. It
is drawn after OCR, so a line never reaches a word, and the PNG's pixel
size is unchanged.

Schemas: the screenshot inputSchema declares both flags with descriptions;
the outputSchema names `ocr` (`additionalProperties: false`, every field
described) so answers validate (MCP-5), and the tool description matches
docs/design/mcp.md word for word (MCP-7).

Docs: docs/specs/mcp.md gains MCP-117 with its proofs; docs/design/mcp.md's
tools row and screenshot answer shape document both; docs/MAP.md lists the
new file. No wire or Android change, no new dependency.

Tests: MCPCoreTests (arguments, schema strictness, sample answer,
description parity), ScreenshotSourceTests (ocr present only when asked),
MCPScreenshotOCRTests (synthetic card at known rows, crop offset, reading
order, grid pixels and PNG size, grid after OCR, crop fractions), and the
mcp e2e suite's flag/schema checks.

Fixes #393

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

MCP: add macOS Vision OCR and 0..1 coordinate grid options to screenshot (MCP-117)

1 participant