# Let Claude Code control an Android emulator

Register Karagöz's MCP server with Claude Code, then let it read an Android emulator's screen, tap and type.

`karagoz mcp` serves the 18 Karagöz commands to Claude Code as tools. Claude Code reads the emulator’s screen as text, taps, types and takes screenshots, and each tool returns the JSON line the CLI prints. The server opens no port: Claude Code starts the process and talks to it over stdio.

## 1. Before you start

- Node 22 or newer.
- `adb` from Android platform-tools.
- A running emulator: `emulator -avd <name>`.

The registration below runs the published package with `npx`, without installing it. To run the commands in your own terminal as well, see [Install](https://karagozmcp.com/docs/install/).

## 2. Register the server

```console title="~/app"
$ claude mcp add karagoz -- npx -y karagoz-mcp mcp
```

The server starts in the directory `claude` started in, with the shell’s environment. A call still running after 2 minutes becomes a background task.

For more information, see [MCP server](https://karagozmcp.com/docs/mcp/#claude-code).

## 3. Check that the emulator is listed

```console title="~/app"
$ karagoz devices
Found 1 device:
  Medium_Phone_API_37.0 • emulator-5554 • emulator • Android 17 (API 37) • device

Missing a device? Run karagoz doctor.
```

Without a global install, `npx -y karagoz-mcp devices` runs the same command. Only a device in state `device` can be used.

When more than one device is listed, a tool call’s `device` argument picks one. Without it the server uses `KARAGOZ_DEVICE`, then `ANDROID_SERIAL` from its own environment, then the only device: [Device selection](https://karagozmcp.com/docs/device-selection/).

For more information, see [`devices`](https://karagozmcp.com/docs/commands/devices/).

## 4. Add the agent instructions

This step is optional. Add the block below to your `CLAUDE.md`, `AGENTS.md` or your agent’s rules, or ask your agent to add it. The agent then uses Karagöz for a request about a device without being told to.

```markdown
## Mobile devices

For anything on an Android emulator or phone, an iOS simulator or an iPhone (reading the screen, tapping, typing, screenshots, installing or launching an app, logs), use karagoz instead of calling adb, xcrun simctl, devicectl or xcodebuild yourself. Use the karagoz MCP tools when they are available; otherwise run `npx -y karagoz-mcp <command>`. Read the screen with ui-tree before taking a screenshot, tap by text or id, and branch on error.code.
```

## 5. Ask for a task

Describe the task to Claude Code. It reads the screen with `ui_tree` and calls one tool per step.

- “Open Settings on the emulator and turn on dark mode.”
- “The app closes right after launch on the emulator. Show me its last 50 log lines.”

Esc cancels the running call. What already reached the emulator still finishes: a gesture plays to the end, and a cancelled `install` may leave the app installed.

## 6. How it reads the screen

- `ui_tree` returns the screen’s accessibility tree. It is text a program can search, costs a few hundred to a few thousand tokens, and gives the bounds needed to tap.
- `tap` with `text` or `id` finds one node in the tree and taps its center.
- `screenshot` writes a full-resolution PNG to disk and returns its path, with its pixel size, logical size, scale and safe area. With `inline: true` it also returns the image, which the client scales before the model sees it. So coordinates come from the tree’s `bounds`, never from the image.

A tool’s name has `_` where its command has `-`, because Codex turns `-` into `_`: the `ui-tree` command is the `ui_tree` tool.

For more information, see [`ui-tree`](https://karagozmcp.com/docs/commands/ui-tree/) and [`tap`](https://karagozmcp.com/docs/commands/tap/).

## With Codex

Codex was run against this server too. Register it:

```console title="~/app"
$ codex mcp add karagoz -- npx -y karagoz-mcp mcp
```

Then add these two lines under `[mcp_servers.karagoz]` in `~/.codex/config.toml`. The command cannot write them, and without them Codex hides `ANDROID_HOME` from Karagöz and cuts long calls short.

```toml title="~/.codex/config.toml"
env_vars = ["ANDROID_HOME", "ANDROID_SDK_ROOT", "KARAGOZ_DEVICE", "ANDROID_SERIAL", "DEVELOPER_DIR"]
tool_timeout_sec = 600
```

- Codex passes a server only a short list of environment variables. `env_vars` forwards the ones Karagöz reads.
- `tool_timeout_sec` covers long `tap` waits and big installs.
- `install` and `uninstall` ask for approval. `codex exec` never asks, so it refuses them with `MCP tool call requires approval, but approval policy is never`. Run them in an interactive Codex session or from the CLI.

For more information, see [MCP server](https://karagozmcp.com/docs/mcp/#codex).

## When it fails

- [`ADB_NOT_FOUND`](https://karagozmcp.com/docs/error-codes/#adb-not-found): no `adb` found. [`doctor`](https://karagozmcp.com/docs/commands/doctor/) shows where Karagöz looked and which `adb` it uses. Install platform-tools, or set `ANDROID_HOME` to your Android SDK. With Codex, `env_vars` forwards `ANDROID_HOME`.
- [`NO_DEVICE`](https://karagozmcp.com/docs/error-codes/#no-device): no device connected and none named. Karagöz needs a running emulator: `emulator -avd <name>`.
- [`DEVICE_AMBIGUOUS`](https://karagozmcp.com/docs/error-codes/#device-ambiguous): more than one device and none named. The tool call needs `device`, or `KARAGOZ_DEVICE` set in the shell you start `claude` from.
- [`AUTOMATION_BUSY`](https://karagozmcp.com/docs/error-codes/#automation-busy): another UiAutomation client is connected, and only one can be at a time. After a cancelled call the emulator keeps its UiAutomation slot for about 1-2 s, so a `ui_tree` right after can get this code.

## Related pages

- [MCP server](https://karagozmcp.com/docs/mcp/): the setup for every client, and what the tools take and return.
- [Requirements](https://karagozmcp.com/docs/requirements/): what each target needs.
- [`ui-tree`](https://karagozmcp.com/docs/commands/ui-tree/) and [`tap`](https://karagozmcp.com/docs/commands/tap/): reading the screen and tapping.
- [`devices`](https://karagozmcp.com/docs/commands/devices/) and [`doctor`](https://karagozmcp.com/docs/commands/doctor/): what Karagöz sees, and which `adb` it uses.
- [Device selection](https://karagozmcp.com/docs/device-selection/): how a device is picked when several are listed.
- [Known limitations](https://karagozmcp.com/docs/known-limitations/): what a cancelled call leaves behind, and what `codex exec` refuses.
