utm-mcp
# utm-mcp
MCP server that lets an LLM drive [UTM](https://mac.getutm.app/) virtual machines on macOS:
lifecycle, guest commands with real output, fast file transfer, screenshots, keyboard and mouse.
| Tool | What it does | Needs |
|---|---|---|
| `vm_list`, `vm_start`, `vm_stop`, `vm_ip` | lifecycle, wait for the guest agent | — |
| `vm_exec` | run a program in the guest, get `exit_code`, `stdout`, `stderr` | guest agent |
| `vm_push`, `vm_pull` | copy files over HTTP on the VM network (~30 MB/s+) | guest agent, `curl` in guest |
| `vm_snapshot` | list / create / restore / delete | — |
| `vm_screenshot` | guest screen as PNG; with the helper also one window (`hwnd`) and `max_width` | Windows helper, or QMP + Full Disk Access + a non-GL display |
| `vm_key`, `vm_type`, `vm_click`, `vm_scroll` | keyboard and absolute mouse; `vm_type` follows the guest's layout | QMP |
| `vm_layout` | detect the guest's keyboard layout | guest agent |
| `vm_paste`, `vm_clipboard` | put any text on the user's clipboard (and Ctrl+V), or read it | Windows helper; QMP for paste |
| `vm_run_user` | start a program as the user, an elevated admin (`as_="admin"`, no prompt) or SYSTEM (GUI apps too), optionally wait for its output | Windows helper |
| `vm_uac` | inspect / allow / deny a UAC prompt on the secure desktop; QMP keys as fallback | Windows helper (QMP fallback) |
| `vm_windows`, `vm_window` | list top-level windows; focus / minimize / maximize / restore / close one | Windows helper |
| `vm_ui_tree`, `vm_ui_act` | read a window's UI Automation tree; invoke / set value / toggle / select a control by name | Windows helper |
| `vm_wait` | wait for a window, its absence, foreground, a process exit, a UAC prompt or the screen to settle | Windows helper |
| `vm_ready` | wait until the agent answers and, on Windows, the helper reports an unlocked, logged-in desktop; `ready:false` + `missing` on timeout | guest agent (+ Windows helper) |
| `vm_job_start`, `vm_job_log`, `vm_job_stop`, `vm_jobs` | long commands that outlive a call: output kept in a log you stream with `next_offset` (`wait=N` blocks for new text), stop kills the tree | Windows helper |
| `vm_ps`, `vm_kill` | process list (name / user filter, memory order, optional CPU sampling), kill one or its tree | Windows helper |
| `vm_events` | Windows event log as JSON (log, provider, level, time window, text filter) | guest agent |
| `vm_tail` | last lines of a guest text file (Windows or Linux), capped at 256 KB | guest agent |
| `vm_close` | close windows by hwnd / pid / process, answering "save changes?" prompts ("Don't save" by default), killing what stays open | Windows helper |
| `vm_info` | user, session (active/locked/uac), desktop, screens and scale, keyboard layout, foreground window | Windows helper |
| `vm_helper_install` | install (or update) of the in-session helper | Windows guest agent, `scripts/build_helper.sh` run once |
| `vm_enable_qmp` | add the QMP argument to a stopped VM | — |
Everything past lifecycle and snapshots needs a QEMU-backend VM. Apple Virtualization VMs have no
guest agent or QEMU monitor.
## How it works
- **UTM's AppleScript API** handles lifecycle, snapshots and configuration, and runs guest
commands with `execute … with output capturing` plus `get result`. `utmctl exec` throws the
output and exit code away.
- **QMP over loopback TCP** handles screenshots and input. UTM keeps its own QMP connection
inside SPICE, so each VM gets an extra `-qmp tcp:127.0.0.1:<port>,server,nowait`.
- The port is derived from the VM's UUID.
- QEMU's sandbox refuses Unix sockets outside its container, which is why it's TCP.
- QMP has no authentication, so it never listens on anything but 127.0.0.1.
- **Screenshots** use `screendump`. QEMU can only write inside the QEMUHelper container
(`~/Library/Containers/com.utmapp.QEMUHelper/Data/Documents/utm-mcp`), and macOS protects
that folder from other apps. The app hosting this server (terminal, Claude, T3 Code…) therefore
needs **Full Disk Access**.
- UTM's QEMU has no libpng, so `screendump` is asked for PPM and re-encoded to PNG on the host.
- With a GPU-accelerated display (`virtio-ramfb-gl`, what the Windows 11 VM here uses) QEMU can't
read the scanout and the dump is **solid black**. Use the helper's screenshots there; the QMP
path (and so the `vm_uac` fallback's screenshot) only works with a non-GL display device
(untested here).
- **File transfer** runs a one-shot HTTP server on the host's VM-facing address with a random
path; the guest runs `curl` through `vm_exec`. The guest-agent file channel manages only
a few hundred KB/s.
- **The Windows helper** (`helper/`, Rust, `utm-helper.exe`) runs as SYSTEM *inside the logged-in
user's session* (the SYSTEM guest agent sits in session 0 and can't see the desktop) and serves
JSON over HTTP on port 47123 of the VM network. `vm_helper_install` pushes it, writes a random
token, starts it from a boot-time SYSTEM scheduled task and opens the firewall for the local
subnet. The host calls it with `Authorization: Bearer <token>`. Endpoints, process model and
security notes: [`helper/README.md`](helper/README.md). The OS work is the public crate
[desktop-kit](https://github.com/AnisAbdellatif/desktop-kit) (git tag `v0.2.0`), which `helper/`
fetches from GitHub at build time.
- The task runs `utm-helper.exe --supervise` (session 0), which keeps a `--session` child alive
in the console user's session and respawns it when it dies or the session changes.
- Each request thread attaches to the current input desktop, so the helper also sees the lock
screen and the UAC prompt (both on the secure "Winlogon" desktop), and elevated windows.
- Coordinates are physical pixels, the same space as QEMU's framebuffer.
- Screenshots use GDI, so they need no QMP and no Full Disk Access.
- Build both architectures from macOS with `scripts/build_helper.sh` (needs `cargo-xwin`, the
two `*-pc-windows-msvc` rustup targets, brew `llvm` and `lld`).
The QMP and QEMUHelper-container findings come from
[vintage-utm-mcp](https://github.com/cesarse/vintage-utm-mcp).
## Gotchas
- `vm_exec` runs as the guest agent's user: SYSTEM on Windows (session 0, no desktop), root
on Linux. To start a GUI app as the logged-in Windows user, use `vm_run_user` (after
`vm_helper_install`).
- `vm_window focus` can fail while a shell flyout (Start, Search) is open; it reports `ok: false`.
- Admin apps: `vm_run_user(as_="admin")` runs them elevated with no UAC prompt. To test the real
prompt, start `Start-Process <exe> -Verb RunAs` as the user, `vm_wait(for_="uac")`, then
`vm_uac` (inspect, then allow / deny). Credential prompts (a non-admin user) are refused.
- Lock screen: `vm_info` reports session `locked` and screenshots show it. To unlock, invoke the
`Lock Screen` pane (`id=InvokableLockContentControl`), then `set_value` the `Password` edit
(`id=PasswordField_2`) and invoke `SubmitButton` with `vm_ui_act`.
- `vm_ui_act` / `vm_ui_tree` only see what the app exposes through UI Automation; some apps
(games, custom-drawn UIs) expose nothing, then use `vm_screenshot` + `vm_click`.
- A screenshot with `hwnd` or `max_width` is not in screen pixels: the tool's text note gives
origin and scale, `screen = origin + image / scale`. `vm_click` takes screen pixels.
- `vm_key` / `vm_type` return once the keys are queued in QEMU, then wait a fixed ~60 ms per key
press for the guest to take them (it lags behind otherwise), so a slower guest may still trail.
- `vm_type` detects the guest layout (cached for a minute) and presses the keys that produce each
character there, dead keys included. Tables cover 27 standard layouts, generated from Unicode
CLDR by `scripts/gen_layouts.py` (adapted from cometdesk). Variants like Dvorak fall back to
en-us, and the result says so.
- Windows: the focused window's layout via the helper, else the user's default from the registry.
- Linux: `localectl` / `/etc/default/keyboard`.
- Jobs (`vm_job_start`) are owned by the helper's `--session` child. If the supervisor restarts it
(session change, crash), running jobs lose their pipes (the program may die on its next write)
and report `running: false` with no exit code. Logs live in `C:\ProgramData\utm-mcp\jobs`, capped at
64 MB each, and are deleted after 7 days; `vm_tail` reads them too.
- `vm_close` finds a save prompt through UI Automation: a prompt drawn inside the window (WinUI
`ContentDialog`, Win11 Notepad) or a window of its own (Win32 MessageBox / TaskDialog), and
clicks "Don't save" by AutomationId (`SecondaryButton`, `CommandButton_7`), then by name
(`Don't save`, `Nicht speichern`, `No`). Windows 11 Notepad by default restores unsaved tabs and
closes with no prompt; to get the prompt, switch Settings → "When Notepad starts" to "Start new
session and discard unsaved changes". A killed Notepad restores its tabs on the next start.
- `vm_events` / `vm_tail` pass every value to PowerShell as a single-quoted literal inside
`-EncodedCommand`, and `log` / `provider` only accept letters, digits, space and `_ . - /`.
- The helper's folder `C:\ProgramData\utm-mcp` is readable and writable only by SYSTEM and
Administrators. The helper itself is SYSTEM: anyone on the VM subnet who has the token can run
programs as SYSTEM, or as the user's elevated token, with no prompt. Meant for test VMs only.
Clipboard on Linux guests isn't supported.
- On Windows, use `curl.exe` rather than `Invoke-WebRequest`. In Windows PowerShell 5.1 the
latter is around 10× slower and can hang.
## Setup
Needs macOS with [UTM](https://mac.getutm.app/), [uv](https://docs.astral.sh/uv/), and the QEMU
guest agent in each guest (UTM guest tools on Windows, `qemu-guest-agent` on Linux).
```sh
git clone https://github.com/AnisAbdellatif/utm-mcp.git ~/dev/utm-mcp
cd ~/dev/utm-mcp && uv sync
claude mcp add --scope user utm -- uv --directory ~/dev/utm-mcp run utm-mcp
```
Then, once per VM: stop it, call `vm_enable_qmp`, and start it again.
For Windows guests, build the helper once (needs Rust and
[cargo-xwin](https://github.com/rust-cross/cargo-xwin); it depends on
[desktop-kit](https://github.com/AnisAbdellatif/desktop-kit)): `scripts/build_helper.sh`, then
call `vm_helper_install`.
## Security
The helper runs as **SYSTEM** in the guest: it can start elevated programs and answer UAC
prompts, and so can anyone holding its token. The token is random per install, readable only by
SYSTEM and Administrators (`C:\ProgramData\utm-mcp`), and the firewall rule admits only the local
subnet (UTM's VM network). Use it on test VMs you control. Credential prompts (UAC asking for a
password) can only be denied; a password is never typed.
## Develop
```sh
uv run pytest
(cd helper && cargo test) # pure parts run on the Mac
scripts/build_helper.sh # cross-builds src/utm_mcp/bin/utm-helper-{arm64,x64}.exe
```
## License
MIT, see [LICENSE](LICENSE). Keyboard layout data in `src/utm_mcp/layouts.json` is generated from
[Unicode CLDR](https://cldr.unicode.org/) (see `src/utm_mcp/CLDR-LICENSE`).
TDQS
Scored across 36 tools
Most tools are sharply distinguished by target resource and action, and the descriptions explicitly call out the tricky boundaries (vm_exec has no desktop vs vm_run_user in the user session vs vm_job_start for long-running work; vm_window close vs vm_close's prompt handling; vm_kill vs vm_job_stop vs vm_stop). A few clusters still need careful reading to pick correctly—the three exec-family tools, and vm_wait vs vm_ready—but the descriptions do carry the weight.
Everything is uniformly vm_-prefixed snake_case, so the family is predictable. The only deviation is that some tools are nouns describing a resource read (vm_ip, vm_info, vm_jobs, vm_events, vm_layout, vm_clipboard, vm_screenshot) while others are verb_noun (vm_run_user, vm_job_log, vm_helper_install), giving a slightly mixed verb/noun convention within an otherwise consistent scheme.
36 tools is well above the comfortable 3-15 band and forces the agent to scan a large surface before acting. The breadth is defensible—the domain genuinely spans QEMU control, guest agent, and a Windows helper—but several capabilities are split into many small tools (job_start/job_log/job_stop/jobs, windows/window/ui_tree/ui_ui_act/close) that could be consolidated.
Coverage is unusually deep: VM lifecycle, exec, file push/pull, input, screenshots, UI Automation, snapshots, QMP setup, jobs, process inspection, event logs and file tailing are all present with no dead ends in the driving workflow. The notable gap is VM provisioning—there is no create/clone/delete/configure tool, so a VM must pre-exist and be created elsewhere.