unity-ui-agent-mcp
v0.3.0MCP server that lets AI agents build and inspect uGUI layouts in the Unity Editor.
Let AI agents such as Claude Code build, inspect and visually check uGUI layouts inside the Unity Editor, through the Model Context Protocol. The goal is a design-to-UI loop: the agent reads a mockup, builds the layout with real Unity components, renders it, compares the render with the design and fixes the differences until they match. Anchor presets: center, top, bottom, left, right, top-left, top-right, bottom-left, bottom-right, stretch, stretch-top, stretch-bottom, stretch-left, stretch-right, stretch-horizontal, stretch-vertical.
Summary extracted from the README.
Unity UI Agent (MCP)
Let AI agents such as Claude Code build, inspect and visually check uGUI layouts inside the Unity Editor, through the Model Context Protocol.
The goal is a design-to-UI loop: the agent reads a mockup, builds the layout with real Unity components, renders it, compares the render with the design and fixes the differences until they match.
Status: early release (v0.3). The tools for the design loop (mockup → build → compare → fix) are in place and measured by the evaluation suite; Figma input is on the roadmap below.
How it works
flowchart LR
A[Claude Code<br/>or any MCP client] -- MCP over stdio --> B[Python MCP server<br/>Server~/]
B -- HTTP on 127.0.0.1:6400 --> C[Editor bridge<br/>Editor/UiAgentBridge.cs]
C -- main-thread queue --> D[Unity Editor<br/>scene or prefab]
| Part | What it does |
|---|---|
Editor/ (C#, Unity package) |
A local HTTP bridge inside the Editor. It queues each request and runs it on the main thread, because most of the Unity API is main-thread only. Commands create and edit uGUI elements, describe the hierarchy, render the UI to PNG and read the Console. |
Server~/ (Python) |
The MCP server. It exposes typed tools to the agent, maps them to bridge commands and retries automatically while Unity recompiles scripts. |
Design decisions
- Main-thread queue. HTTP requests arrive on background threads; they are queued and executed from
EditorApplication.update, a few per frame so the Editor stays responsive. - Domain reloads. The bridge stops before every script recompilation or Play Mode entry and restarts afterwards. The Python server retries for up to 20 s, so the agent never sees the gap.
- Everything is undoable. Every change goes through Unity's
Undosystem, so anything the agent does can be reverted with Ctrl+Z. A wholebuild_ui_treecall is one Undo step, and a call that fails leaves nothing half-built. - Edit-Mode screenshots. Screen Space - Overlay canvases are not drawn by any camera. For the capture, they are switched temporarily to Screen Space - Camera with a hidden camera, rendered at the Canvas Scaler resolution, then restored. The agent can check its work without entering Play Mode.
- Prefab Mode aware. When a prefab is open in Prefab Mode, paths resolve inside that prefab, new elements go under the prefab root by default and captures render the prefab, so the agent can build UI prefabs as well as scene UI.
- Local only. The bridge listens on 127.0.0.1 and rejects requests that a web page could send (any request with an
Originheader or withoutContent-Type: application/json). The MCP server only retries requests that never reached Unity, so a timeout cannot apply a change twice. - Agent-friendly errors. Errors explain what to do next, for example "No GameObject found at path 'X'. Use get_hierarchy to see the available paths."
Tools
| Tool | Purpose |
|---|---|
unity_status |
Check the connection, Unity version, Play Mode and open prefab. |
get_hierarchy |
Scene or prefab tree with paths, RectTransforms, colours, texts and components. |
create_ui_element |
Create a panel, image, text, button or empty container with anchors, size, colour, sprite, text and layout group. Creates the Canvas (with Canvas Scaler) and EventSystem if missing. |
build_ui_tree |
Create a whole screen in one call: a tree of elements with the same properties as create_ui_element plus children. Up to 500 elements, one Undo step, all-or-nothing. |
update_ui_element |
Change any of the above, rename, re-parent, reorder or enable/disable. |
delete_object |
Delete a GameObject (undoable). |
set_target_design |
Set the mockup the UI must match (absolute PNG or JPG path). The Canvas Scalers adopt its resolution. |
capture_game_view |
Render the UI to a PNG and return it to the agent as an image. Optional max_side shrinks the longer side; region crops [x, y, width, height]. |
compare_to_target |
Capture the UI and compare it with the mockup: ssim (1 = identical) and pixel_diff (0 = identical). |
diff_to_target |
Capture the UI and return the five regions with the most error plus a heat map (black = match, yellow = largest difference). |
read_console |
Recent Console messages, including compiler errors. |
Anchor presets: center, top, bottom, left, right, top-left, top-right, bottom-left, bottom-right, stretch, stretch-top, stretch-bottom, stretch-left, stretch-right, stretch-horizontal, stretch-vertical.
Installation
Requirements: Unity 2021.3 or newer, uv (it installs Python 3.10+ for you) and an MCP client such as Claude Code.
1. Add the Unity package
Pick one of these options:
-
From OpenUPM (once the package is listed there), with the openupm-cli:
openupm add com.marcaguera.unity-ui-agent -
From Git: in Unity, open Window > Package Manager > + > Add package from git URL… and enter:
https://github.com/maguera93/unity-ui-agent-mcp.git
Then open Window > UI Agent (MCP). The status should read Running on http://127.0.0.1:6400/.
2. Register the MCP server in Claude Code
The server is published on PyPI as unity-ui-agent-mcp. uv downloads and runs it, so there is nothing else to install. From your Unity project folder:
claude mcp add unity-ui-agent -- uvx unity-ui-agent-mcp
If you changed the port in the Unity window, add -e UNITY_UI_AGENT_PORT=<port> after the server name. Other MCP clients can launch the same command over stdio.
Developing the server? Clone this repository and run the server from its source instead:
git clone https://github.com/maguera93/unity-ui-agent-mcp.git
claude mcp add unity-ui-agent -- uv run --directory "/path/to/unity-ui-agent-mcp/Server~" unity-ui-agent-mcp
Tip: in Unity, set Preferences > General > Interaction Mode to No Throttling, so commands run quickly while the Editor is in the background.
Design loop
Give the agent a mockup and it reproduces the screen, checking itself until the UI resembles the design:
set_target_designwith the absolute path of the mockup (PNG or JPG). The Canvas Scalers adopt its resolution, and every later capture renders at that size.- Build the screen with
build_ui_tree, usingget_hierarchyto see what exists. compare_to_targetwith the same path. The agent stops whenssimreaches 0.95, or after 8 correction rounds without reaching it; then it reports the result instead of looping.diff_to_targetlists the five worst regions and a heat map. The agent fixes the first region withupdate_ui_elementand compares again.capture_game_viewwithmax_sideandregionchecks one part of a large screen.read_consoleafter each round, to catch errors.
Both thresholds are constants in Server~/src/unity_ui_agent_mcp/server.py (SIMILARITY_THRESHOLD, MAX_ITERATIONS). Comparison uses numpy, scikit-image and Pillow, which uv installs with the server. The mockup and the render must have at least 7 px on each side for SSIM.
Demo
Claude Code builds a main menu from a mockup in Unity 6, compares the render with the design and reads the Console. In this run the first build reached SSIM 0.962, above the 0.95 target, so no correction rounds were needed. The GIF plays at twice the real speed. The recording script (prompt, mockup and steps) is in Documentation~/demo/guion.md, and the mockup is Documentation~/demo/mockup-menu-principal.png.

Example prompt
Build a mobile main menu at 1080x1920: a dark background, the title "SKY RUNNER" at the top,
and three stacked buttons (Play, Shop, Settings) in a vertical layout group, centred with
40 px spacing. Use #FFB300 for the Play button. Capture the result, check that it looks
right and fix anything that doesn't.
Evaluation results
The evaluation suite (Evals~/) gives the agent 10 reference mockups and runs it on each one until the SSIM reaches 0.95 or it gives up. Results come from the committed baseline Evals~/baseline.json, also exported to Evals~/results.csv. Each row is one case: SSIM is the measured score (1 = identical), rounds are correction rounds after the first build, and tokens are the total used by the run.
| Case | Result | SSIM | Rounds | Tool calls | Time (s) | Tokens |
|---|---|---|---|---|---|---|
| case-01-login-movil | passed | 0.957 | 0 | 8 | 45.6 | 212,023 |
| case-02-ajustes-escritorio | passed | 0.972 | 0 | 6 | 48.7 | 146,942 |
| case-03-inventario-tableta | passed | 0.952 | 2 | 17 | 118.3 | 474,139 |
| case-04-hud-escritorio | passed | 0.976 | 0 | 9 | 55.5 | 249,330 |
| case-05-tienda-movil | passed | 0.971 | 0 | 6 | 40.9 | 210,331 |
| case-06-menu-principal-escritorio | passed | 0.958 | 0 | 6 | 33.4 | 133,821 |
| case-07-dialogo-tableta-apaisada | passed | 0.952 | 0 | 7 | 40.3 | 179,915 |
| case-08-mensajes-movil-compacto | passed | 0.960 | 0 | 6 | 59.7 | 149,543 |
| case-09-perfil-movil-alto | passed | 0.958 | 0 | 9 | 73.5 | 269,765 |
| case-10-pausa-hd | passed | 0.959 | 0 | 7 | 30.2 | 194,840 |
| Mean (10/10 passed) | 0.962 | 0.2 | 8.1 | 54.6 | 222,065 |
Roadmap
- Design input: read a Figma frame or a mockup image as the target.
- Visual diff: score each capture against the target (pixel and structural similarity) so the agent knows when to stop.
- Evaluation suite: about 10 reference designs with recorded scores, time and tool calls per run, to measure each change to prompts and tools.
- TextMeshPro support.
- Save the result as a prefab.
- Unit tests for the Editor commands (Unity Test Framework).
Project layout
package.json Unity package manifest
Editor/
UiAgentBridge.cs HTTP bridge and main-thread queue
CommandRegistry.cs Command name -> handler
SceneQuery.cs Path lookup in scenes and Prefab Mode
JsonParams.cs Parameter parsing helpers
UiAgentWindow.cs Window > UI Agent (MCP)
Commands/ Hierarchy, UI, capture and console commands
Server~/ Python MCP server (ignored by Unity)
src/unity_ui_agent_mcp/ server.py (tools), compare.py (SSIM and heat map)
Documentation~/ Demo recording, its script and the release checklist (ignored by Unity)
Development and CI
GitHub Actions workflows:
- Python server (
.github/workflows/python.yml):ruffandpytestinServer~/, on Python 3.10 and 3.12, on pushes and pull requests. It needs no secrets. - Publish to PyPI (
.github/workflows/pypi.yml): when a GitHub release is published, builds the server and uploads it to PyPI with trusted publishing. The release tag must match the version inServer~/pyproject.toml. It needs no stored token. - Unity Editor tests (
.github/workflows/unity.yml): EditMode tests inTestHost~/, on Unity 2021.3 and Unity 6. It needs these repository secrets, which the repository owner creates in Settings > Secrets and variables > Actions:
| Secret | Value |
|---|---|
UNITY_LICENSE |
The license file contents for the Unity activation. |
UNITY_EMAIL |
The email of the Unity account that owns the license. |
UNITY_PASSWORD |
The password of that Unity account. |
Without UNITY_LICENSE the Unity job is skipped and the rest of the workflow still passes. Never write these values in the workflow files; the workflows read them only through secrets.*.
License
MIT © Marc Agüera Garcia