Skip to content

feat(mcp): game-view screenshots with aligned overlays (#465) - #713

Merged
houko merged 4 commits into
mainfrom
feat/mcp-screenshots-overlays-465
Nov 24, 2026
Merged

houko merged 4 commits into
mainfrom
feat/mcp-screenshots-overlays-465

Conversation

@houko

@houko houko commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the game_screenshot MCP tool in packages/game/mcp so coding agents can capture the running game view as a PNG with optional pixel-aligned diagnostic overlays:

  • Frame acquisition (packages/game/mcp/screenshot.go): supports Config.CaptureFrame (compositing overlays onto a cloned *image.RGBA), Config.DrawFrame, or direct rendering of Config.Scenes / Config.Scene / Config.SceneFunc under the same Viewport / ViewportFunc and view size that game_input_pointer uses, via a self-contained deterministic software rasterizer.
  • Aligned diagnostic overlays:
    • bounds: 1-pixel bounding rectangles aligned with every visible sprite across tilemap.Camera (pan, zoom, shake, parallax, repeat), SpaceScreen HUD layers, and screen.Viewport scaling/letterbox clipping.
    • collision_shapes / physics: 2D collision shapes from Config.Physics / Config.PhysicsFunc (box bodies, circle bodies with +X orientation tick, triggers, solid and one-way tiles) and Config.RigidPhysics / Config.RigidPhysicsFunc (rotated rigid bodies), matching physics.DefaultDebugStyle() colors.
    • grid (with grid_step), camera (visible frame + center crosshair), and labels (compact bitmap font labels).
  • Safe output & strict validation: returns inline png_base64 by default or writes confined inside Config.ScreenshotDir via os.OpenRoot when path is given; rejects unknown fields, invalid dimensions, unsupported formats, and path traversal.
  • Minimal mcp.go diff: additive Config fields and appending b.screenshotTools() in Bridge.Tools() so parallel MCP PRs (MCP: control the loop #462–MCP: assets and hot reload #469) rebase cleanly.
  • Updated docs/en/mcp.md, docs/zh/mcp.md, added changelog fragment changelog.d/mcp-screenshots-overlays-465.md and engineering note .agents/notes/implemented/feature/2026-11-24-mcp-screenshots-and-overlays.md.

Changes after review (rebased onto main after #462, #463 and the asset tools)

  • Reuses main's Config.Viewport, ViewportFunc, ViewSizeFunc and Bridge.SetViewport instead of redeclaring them. The default size is the view (ViewSizeFunc, the window, then the virtual size), and Viewport.Layout runs on the view in points and is scaled to the image, so a CaptureFrame image at a 2x pixel ratio gets the game's scale.
  • Inferred sizes are bounded and must be finite; a named scene without a scene.Scene is an error; path must end in .png; grid_step is also accepted at the top level.
  • The rasterizer flattens quadratic and cubic curves, reads image source rects in the image's points when ImageSource.Size is set, and reports what it leaves out in skipped_ops and skipped_images (text, ellipses, gradients, URI / asset: / Offscreen images).
  • Docs recommend path: an MCP client receives an app tool's result as JSON text, so png_base64 is text to the agent until NSGo lets app tools return image content.

Verification

  • git ls-files -z -- '*.go' | xargs -0 -r gofmt -l (empty)
  • go run ./cmd/gameschema -check (passed)
  • go tool nsgo check -target macos,windows,linux,android,web (passed on all targets)
  • go tool nsgo test ./mcp (44 passed, including pixel-exact alignment under camera pan/zoom/parallax, screen.Viewport letterboxing and a 2x CaptureFrame)
  • go tool nsgo check -target linux,web ./mcp and the full go tool nsgo test . after the rebase
  • go tool nsgo test -run TestStabilityTable . (passed)
  • go run ./tools/changelog check (passed)
  • bun run verify-notes (passed)

Closes #465

@github-actions github-actions Bot added size/XL documentation Improvements or additions to documentation area/agents enhancement New feature or request conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
…ew size

game_screenshot read Config.Viewport directly, so a game supplying its viewport through ViewportFunc got an unscaled frame and overlays without the letterbox transform, and the PR declared its own SetViewport and Viewport field on top of the ones main already has. It now uses activeViewport and activeViewSize, the mapping game_input_pointer uses: the default size is the view (ViewSizeFunc, the window, then the virtual size), and Viewport.Layout runs on the view in points and is scaled to the image, so a CaptureFrame image in device pixels gets the scale the game used rather than the one Layout picks for the pixel size.

Inferred sizes are now bounded like explicit ones and non-finite sizes are refused, a named scene with no scene.Scene is an error instead of a blank image, paths must end in .png, grid_step is accepted at the top level too, and OverlaysDrawn uses omitempty for every flag.

The software rasterizer flattens quadratic and cubic curves, reads image source rectangles in the image's points when ImageSource.Size is set, no longer copies Data on every cache lookup, and reports what it leaves out (skipped_ops by kind, skipped_images for URI, asset: and Offscreen sources).
Adds a 2x CaptureFrame under an integer-scaled ViewportFunc, an image with Size, URI images and unsupported ops reported as skipped, a named scene without a scene.Scene, inferred sizes over the caps or not finite, a pixel-count cap within the per-side cap, a symlink leaving ScreenshotDir, non-.png paths and top-level grid_step. Existing cases now set ViewSizeFunc or read the test window, since the default size follows the view.
…e primary mode

An MCP client receives an app tool's result as JSON text, so png_base64 is text to the agent; the docs now steer agents to path inside ScreenshotDir, and describe the view size and pixel ratio, scene rendering, skipped_ops and skipped_images, and which sprites get bounds.
@houko
houko force-pushed the feat/mcp-screenshots-overlays-465 branch from 0a9f065 to c6b7af7 Compare November 24, 2026 19:52
@houko
houko merged commit 9e24b6c into main Nov 24, 2026
11 of 35 checks passed
@houko
houko deleted the feat/mcp-screenshots-overlays-465 branch November 24, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents conflict Conflicts with the target branch; resolve before merging documentation Improvements or additions to documentation enhancement New feature or request size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP: game-view screenshots with overlays

1 participant