Skip to main content
Glama
android-mcp-server

android-mcp-server

by Shekhar23
README.md
# android-mcp-server

[![CI](https://github.com/shekharpande/android-mcp-server/actions/workflows/ci.yml/badge.svg)](https://github.com/shekharpande/android-mcp-server/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

An [MCP](https://modelcontextprotocol.io) server that gives Claude Code, and any other MCP client, access to your Android device and build. It can read logcat, pull crash stack traces and ANRs, run Gradle tasks, install APKs and take screenshots.

> "The app crashes when I open settings. Find out why and fix it."

Claude can now do the whole loop itself: launch the app, read the crash, open the file, fix it, rebuild, reinstall and confirm it's gone.

<table>
  <tr>
    <td align="center"><img src="docs/images/emulator-app-running.png" width="230" alt="App launched with launch_app"><br><sub><code>launch_app</code> + <code>screenshot</code></sub></td>
    <td align="center"><img src="docs/images/emulator-crash-dialog.png" width="230" alt="Crash dialog on the emulator"><br><sub>App crashes, <code>get_crashes</code> reads it</sub></td>
    <td align="center"><img src="docs/images/emulator-installed-build.png" width="230" alt="Fresh build installed and launched"><br><sub><code>gradle_build</code> → <code>install_apk</code> → <code>launch_app</code></sub></td>
  </tr>
</table>

<sub>All screenshots were captured by the server's own <code>screenshot</code> tool on an Android emulator.</sub>

## Why

Debugging Android with an AI assistant usually means copying logcat into the chat by hand. Logcat is noisy, stack traces get cut off, ANR traces are buried in `dumpsys dropbox`, and Gradle errors sit at the bottom of 2,000 lines of output. This server turns each of those into a tool call that returns structured, trimmed results.

## Tools

| Tool | What it does |
|---|---|
| `list_devices` | Connected devices and emulators with state and model |
| `logcat` | Recent logs, filtered by app package (its PID), minimum priority, tag or text |
| `get_crashes` | One record per `FATAL EXCEPTION` from the crash buffer, with process and full stack trace |
| `list_anrs` | ANRs from the system dropbox, with subject and the `main` thread stack |
| `gradle_build` | Runs any task with the project's `gradlew`. Returns compiler errors, "What went wrong" and the output tail |
| `launch_app` | Starts an app's launcher activity |
| `screenshot` | Device screen as a PNG image |
| `install_apk` | `adb install -r` an APK |
| `clear_logcat` | Clears the main, system and crash buffers so the next read shows only new output |
| `adb_shell` | Runs any `adb shell` command |

Start the server with `--read-only` to turn off `install_apk`, `clear_logcat` and `adb_shell`.

## What the tools return

Real output from an emulator run.

**`get_crashes`**: one record per `FATAL EXCEPTION`, with the full stack trace instead of a truncated logcat paste.

![get_crashes output](docs/images/get-crashes.svg)

**`gradle_build`**: a failing build reduced to the part that matters, instead of 2,000 lines of Gradle output.

![gradle_build output](docs/images/gradle-build.svg)

**`logcat`**: only the lines from the app's running process.

![logcat output](docs/images/logcat.svg)

<details>
<summary><b><code>list_anrs</code></b> and <b><code>list_devices</code></b></summary>

![list_anrs output](docs/images/list-anrs.svg)

![list_devices output](docs/images/list-devices.svg)

</details>

## Install

Requirements: Node 20+, and `adb` (Android SDK platform-tools) on `PATH` or in `$ANDROID_HOME/platform-tools`.

```bash
git clone https://github.com/shekharpande/android-mcp-server
cd android-mcp-server
npm install && npm run build
```

### Claude Code

```bash
claude mcp add android -- node /absolute/path/to/android-mcp-server/dist/index.js
# or read-only:
claude mcp add android -- node /absolute/path/to/android-mcp-server/dist/index.js --read-only
```

### Claude Desktop, Cursor and other MCP clients

```json
{
  "mcpServers": {
    "android": {
      "command": "node",
      "args": ["/absolute/path/to/android-mcp-server/dist/index.js"]
    }
  }
}
```

## Using it with your app

### 1. Connect a device or emulator

Start an emulator from Android Studio's Device Manager, or from the command line:

```bash
emulator -list-avds          # names of your virtual devices
emulator -avd Pixel_8 &      # start one
```

A physical phone works too: turn on **Developer options → USB debugging** and plug it in, or use **Wireless debugging** and `adb pair` / `adb connect`. Check that it shows up:

```bash
adb devices
# emulator-5554   device
```

If more than one device is connected, tell Claude which one to use (e.g. "use emulator-5554"). Every tool takes an optional `serial`.

### 2. Find your app's package name

Tools that target an app need its package name (the `applicationId`), not the app's display name:

```bash
grep applicationId app/build.gradle*        # from your project
adb shell pm list packages -3               # apps you installed on the device
```

Debug builds often have a suffix such as `.debug`, so `com.example.app` installs as `com.example.app.debug`. Use the name that's actually installed.

### 3. Open Claude Code in your app's project

```bash
cd ~/AndroidStudioProjects/MyApp
claude
```

Starting Claude in the project folder lets it read and edit your source, and pass the right `projectDir` to `gradle_build`. Run `/mcp` to check that the `android` server is connected.

### 4. Ask in plain language

Claude picks the tools for you. Some prompts that work well:

| Goal | Prompt |
|---|---|
| Fix a crash | "Launch com.example.app, open the settings screen, then use get_crashes to find out why it crashes and fix it." |
| Investigate a crash that already happened | "Check get_crashes for com.example.app and explain the most recent one." |
| Build, install and run | "Build the debug APK, install it on the emulator and launch it. Take a screenshot when it's open." |
| Fix build errors | "Run assembleDebug and fix whatever fails." |
| Run tests or lint | "Run testDebugUnitTest and fix the failing tests." / "Run lint and fix the errors." |
| Debug a freeze | "The app froze earlier. Check list_anrs for com.example.app and tell me what the main thread was blocked on." |
| Read logs | "Show me warnings and errors from com.example.app's logcat since I tapped Login." |
| Check the UI | "Take a screenshot and tell me if the layout looks broken." |

### A typical fix loop

1. **Reproduce:** `clear_logcat`, then `launch_app`, then trigger the bug yourself on the device or tell Claude what to tap via `adb_shell input ...`.
2. **Diagnose:** `get_crashes` (or `list_anrs` / `logcat`) returns the exception and stack trace. Claude maps the frames to files in your project.
3. **Fix:** Claude edits the source.
4. **Verify:** `gradle_build` (`assembleDebug`), `install_apk` with `app/build/outputs/apk/debug/app-debug.apk`, then `launch_app` again. Claude repeats the steps and confirms with `get_crashes` that the crash is gone.

### Tips

- **`logcat` with `package` only sees a running app.** It filters by the app's current process ID. If the app has crashed or isn't open, launch it first, or leave out `package` and use `grep` instead.
- **Clear before reproducing.** Run `clear_logcat` first so `get_crashes` and `logcat` only show what happened during this attempt.
- **The first Gradle build is slow.** A cold build can take several minutes while Gradle downloads dependencies. Later builds use the cache. Raise `timeoutMinutes` for big projects.
- **Use `--read-only` on a personal phone.** It still lets Claude read logs, crashes and screenshots, but not install apps or run shell commands.
- **Release builds:** stack traces from minified (R8/ProGuard) builds are obfuscated. Debug builds give readable traces.

## Configuration

| Env var | Default | Purpose |
|---|---|---|
| `ADB_PATH` | `$ANDROID_HOME/platform-tools/adb`, then `adb` | adb binary to use |
| `ANDROID_MCP_READ_ONLY` | unset | Set to `1` to do the same as `--read-only` |
| `ANDROID_MCP_MAX_OUTPUT` | `40000` | Max characters returned per tool call (longer output is cut, keeping the end) |

## Architecture

![Architecture](docs/images/architecture.svg)

- **`exec.ts`** runs every command through `execFile` with an argument array. Nothing goes through `sh` on the host, so tool arguments can't inject host commands.
- **`parsers.ts`** contains pure functions that turn `adb devices -l`, the crash buffer, `dumpsys dropbox` and Gradle output into typed records. They are unit-tested without a device.
- **`tools.ts`** registers the tools, validates input with zod (package names, Gradle task names and flags are checked with regexes) and keeps output within the context budget.

## Safety notes

- `adb_shell` runs whatever command it's given on the device. Your MCP client asks for approval before each call, but use `--read-only` if you don't want that tool available at all.
- `gradle_build` only runs the `gradlew` in the directory you pass. Task names and flags are restricted to safe characters.

## Development

```bash
npm test          # parser unit tests (vitest)
npm run typecheck
npm run dev       # tsc --watch
```

## License

MIT