Skip to main content
Glama
Android Control MCP Server

Android Control MCP Server

Android Control MCP Server

一个基于 Node.js 构建的生产级**模型上下文协议(MCP)**服务器,使 AI 助手(如 Claude Desktop、Cursor 以及自定义智能体工作流)能够观察、推理并控制已连接的 Android 设备。


🌟 功能特性

  • 👀 计算机使用观察:高分辨率 PNG 屏幕截图直接以 MCP 图像块形式返回,并附带设备元数据。

  • 🔍 UIAutomator 层级解析:智能 XML 转储解析器,将冗长的 Android UI 树转换为紧凑、结构化的 JSON。

  • 🎯 语义元素点击:通过可见文本、无障碍描述或资源 ID 点击按钮和控件,无需手动计算像素坐标。

  • ⚡ 快速手势与输入:像素级精确点击、多坐标滑动、方向滚动、硬件按键模拟以及经过 shell 转义处理的文本输入。

  • 📱 应用生命周期管理:通过包名启动应用,并检查当前聚焦的前台 Activity。

  • 🛡️ 安全与沙箱:输入坐标根据实际屏幕边界进行验证、严格的白名单按键码、参数清理、命令超时以及严格的 stderr 日志记录,确保 stdio MCP 流完整性。

  • 🔄 多设备支持:自动检测已连接的设备,或通过 ANDROID_DEVICE_ID 定位特定设备。


Related MCP server: autoglm-mcp-server

🏗️ 架构

AI Client (Claude Desktop, Cursor, Agent)
   │
   │ stdio transport (JSON-RPC)
   ▼
Node.js MCP Server
   │
   ├── Stderr Structured Logger
   ├── Zod Schema Validation
   │
   ├── ADB Controller Layer
   │   ├── Device Resolver (auto-detect or target serial)
   │   ├── Input Engine (tap, swipe, keyevent, text, scroll)
   │   ├── Screenshot Manager (exec-out binary stream)
   │   └── App Manager (launch, foreground inspection)
   │
   └── UIAutomator Engine
       ├── XML Hierarchy Dump & Normalizer
       ├── Bounds & Center Coordinate Extractor
       └── Semantic Element Finder & Click Resolver
   │
   ▼
Android Device / Emulator

📋 前置要求

  1. Node.js:v20.0.0 或更高版本(node -v)

  2. Android SDK Platform-Tools:已安装 ADB(adb)并可在系统 PATH 中访问(或通过 ADB_PATH 配置)。

  3. Android 设备或模拟器:

    • 物理设备:通过 USB 连接,启用开发者选项和USB 调试。

    • 模拟器:Android Studio AVD、Genymotion 或无头模拟器。

验证设备连接

adb devices -l

您应该看到设备被列为 device:

List of devices attached
emulator-5554          device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64

🚀 安装与快速开始

# Clone or navigate to the repository
cd c:/Users/Abhishek/Code/mcp

# Install dependencies
npm install

# Run unit tests
npm test

# Start the MCP server
npm start

⚙️ 配置

创建 .env 文件或传入环境变量:

# Target device ID (serial number). If omitted and 1 device is connected, it auto-selects.
ANDROID_DEVICE_ID=

# Custom path to ADB executable if not in PATH
# Windows: C:\Users\<user>\AppData\Local\Android\Sdk\platform-tools\adb.exe
# macOS: /Users/<user>/Library/Android/sdk/platform-tools/adb
ADB_PATH=adb

# Log level: debug | info | warn | error
LOG_LEVEL=info

# Default ADB timeout in milliseconds
ADB_TIMEOUT_MS=15000

🔌 连接到 MCP 客户端

1. Claude Desktop 配置

将以下内容添加到您的 claude_desktop_config.json:

  • Windows:%APPDATA%\Claude\claude_desktop_config.json

  • macOS:~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "android-control": {
      "command": "node",
      "args": ["C:/Users/Abhishek/Code/mcp/src/index.js"],
      "env": {
        "ANDROID_DEVICE_ID": "",
        "ADB_PATH": "adb",
        "LOG_LEVEL": "info"
      }
    }
  }
}

2. Cursor / Windsurf MCP 配置

在 Cursor 的 Features > MCP Servers 或 Windsurf 设置中:

  • 名称:android-control

  • 类型:command

  • 命令:node C:/Users/Abhishek/Code/mcp/src/index.js


🛠️ MCP 工具参考

1. android_device_info

获取全面的设备硬件和操作系统元数据。

  • 参数:

    • deviceId (可选字符串):目标设备序列号。

  • 示例响应:

    {
      "deviceId": "emulator-5554",
      "model": "Pixel 8",
      "manufacturer": "Google",
      "androidVersion": "15",
      "sdk": 35,
      "resolution": {
        "width": 1080,
        "height": 2400
      },
      "connectionState": "connected"
    }

2. android_screenshot

将 Android 屏幕捕获为 MCP 图像内容块(image/png)。

  • 参数:

    • deviceId (可选字符串):目标设备序列号。

  • 返回:PNG base64 图像数据块 + 尺寸元数据。


3. android_ui_dump

将当前屏幕的 UIAutomator 层级转储为紧凑、AI 友好的 JSON 格式。

  • 参数:

    • deviceId (可选字符串):目标设备序列号。

  • 示例输出:

    {
      "package": "com.android.settings",
      "activity": "com.android.settings.Settings",
      "totalElements": 24,
      "interactiveElementsCount": 8,
      "elements": [
        {
          "index": 0,
          "text": "Network & internet",
          "resourceId": "android:id/title",
          "className": "TextView",
          "clickable": true,
          "bounds": [196, 340, 1016, 400],
          "center": [606, 370]
        }
      ]
    }

4. android_find_element

在当前屏幕上搜索符合特定条件的 UI 元素。

  • 参数:

    • text (可选字符串):可见文本(精确或部分匹配)。

    • contentDescription (可选字符串):无障碍标签。

    • resourceId (可选字符串):资源 ID。

    • className (可选字符串):控件类名。

    • clickable (可选布尔值):按可点击性过滤。

    • exactMatch (可选布尔值,默认 false):精确字符串匹配。


5. android_click_element

一步完成查找元素并点击其中心点。

  • 参数:

    • text (可选字符串):元素文本。

    • contentDescription (可选字符串):元素内容描述。

    • resourceId (可选字符串):元素资源 ID。

    • className (可选字符串):元素类名。


6. android_tap

在精确的 (x, y) 坐标处点击,并带有屏幕边界验证。

  • 参数:

    • x (数字):X 坐标。

    • y (数字):Y 坐标。


7. android_swipe

在两点之间执行拖拽/滑动手势。

  • 参数:

    • x1、y1 (数字):起始坐标。

    • x2、y2 (数字):结束坐标。

    • duration (可选数字,默认:300):持续时间(毫秒)。


8. android_type_text

在当前聚焦的输入框中输入文本。处理空格编码(%s)和 shell 字符转义。

  • 参数:

    • text (字符串):要输入的文本。


9. android_press_key

按下硬件键或导航键。

  • 支持的按键:HOME、BACK、ENTER、TAB、ESC、DELETE、SPACE、VOLUME_UP、VOLUME_DOWN、POWER、APP_SWITCH、CAMERA 等。

  • 参数:

    • key (字符串):按键名称或数字按键码。


10. android_scroll

根据实际屏幕尺寸计算的方向滚动。

  • 参数:

    • direction (字符串:up | down | left | right)

    • amount (可选数字):滚动距离(像素)。


11. android_launch_app

通过包名启动应用程序。

  • 参数:

    • packageName (字符串):例如 com.android.settings、com.google.android.youtube。

    • activity (可选字符串):特定的 Activity 名称。


12. android_current_app

检查当前聚焦的前台包和 Activity。


13. android_execute_action(统一计算机使用工具)

支持所有操作(tap、swipe、type、press_key、click_element、scroll、launch_app)的单一统一操作分发器。

{
  "action": "click_element",
  "text": "Wi-Fi"
}

🤖 推荐的 AI 工作流:观察 → 推理 → 行动 → 验证

1. OBSERVE:
   AI calls `android_screenshot` and `android_ui_dump`.

2. REASON:
   AI inspects visual and UI structure to identify target elements.

3. ACT:
   AI calls `android_click_element`, `android_type_text`, or `android_scroll`.

4. VERIFY:
   AI captures another screenshot/dump to confirm desired state change.

🧪 测试

运行自动化测试套件:

npm test

测试使用 Vitest 并模拟 ADB 执行层,允许在 CI/CD 期间无需连接物理 Android 设备即可进行完整的单元验证。


🔒 安全最佳实践

  • 无任意 Shell 执行:服务器不暴露原始的 adb shell 执行工具。

  • 安全进程调用:所有命令均使用 child_process.execFile 并带有显式参数数组,以防止 shell 注入。

  • 输入清理:包名、按键码和坐标参数均通过 Zod 模式和边界检查进行严格验证。

  • Stderr 隔离日志:所有日志仅定向到 stderr,以保持 stdout 上严格的 JSON-RPC 协议合规性。


📄 许可证

MIT

Available Tools

13 tools
android_click_elementB

Find and click a UI element on the current Android screen by its visible text, content description, or resource ID. Computes the center of the element and executes a precise tap.

Inputs: text, deviceId, className, exactMatch, +2 more.

android_current_appA

Get the package name and activity of the currently focused foreground Android application.

Inputs: deviceId.

android_device_infoA

Get detailed information about the connected Android device, including device ID, model, manufacturer, Android OS version, SDK level, and screen resolution.

Inputs: deviceId.

android_execute_actionC

Execute a high-level Android action in a single tool call. Supports tap, swipe, type, press_key, click_element, scroll, and launch_app.

Inputs: action (required), x, y, x1, +13 more.

android_find_elementA

Search for UI elements on the current Android screen by visible text, content description, resource ID, or class name. Returns matching elements with their computed center tap coordinates.

Inputs: text, deviceId, className, clickable, +3 more.

android_launch_appB

Launch an Android application by its package name (e.g. "com.android.settings", "com.google.android.youtube", "com.android.chrome").

Inputs: packageName (required), activity, deviceId.

android_press_keyC

Press an Android hardware or navigation key. Supported keys include: HOME, BACK, CALL, ENDCALL, VOLUME_UP, VOLUME_DOWN, POWER, CAMERA, CLEAR, COMMA, PERIOD, TAB, SPACE, ENTER, DELETE, etc.

Inputs: key (required), deviceId.

android_screenshotA

Capture the current screen of the Android device as a PNG image for visual inspection and AI computer-use reasoning.

Inputs: deviceId.

android_scrollB

Scroll the screen in a specified direction (up, down, left, right).

Inputs: direction (required), amount, deviceId.

android_swipeA

Perform a swipe / drag gesture between two points (x1, y1) and (x2, y2) with a configurable duration.

Inputs: x1 (required), x2 (required), y1 (required), y2 (required), +2 more.

android_tapB

Tap at specific (x, y) pixel coordinates on the Android device screen.

Inputs: x (required), y (required), deviceId.

android_type_textA

Type text into the currently focused Android input field. Handles escaping for spaces and special characters.

Inputs: text (required), deviceId.

android_ui_dumpA

Dump the current Android screen UI hierarchy using UIAutomator. Returns a structured, compact list of visible and interactive UI elements with their text, resource IDs, and screen bounds.

Inputs: deviceId.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.2/5.0

    Scored across 13 tools

    Disambiguation4/5

    Most tools have clearly distinct purposes, but android_tap, android_click_element, and android_execute_action overlap in interaction, differing only by abstraction level (raw coordinates, element-based, and high-level bundling). Descriptions help disambiguate these, so the overlap is minor.

    Naming Consistency3/5

    All tools share the 'android_' prefix, which is good, but the naming pattern is inconsistent: some are bare nouns (screenshot, scroll), some are verb phrases (type_text, launch_app), and some are noun phrases (current_app, device_info). This mix of conventions is still readable but not fully predictable.

    Tool Count5/5

    With 13 tools, the server is well-scoped for a mobile automation domain, offering both low-level gestures and high-level actions without redundancy. This count is appropriate and each tool earns its place.

    Completeness4/5

    The tool surface covers core UI automation: screen capture, interaction (tap, swipe, type, press, scroll), element discovery (ui_dump, find_element, click_element), and app control (launch_app, current_app). Missing advanced gestures like pinch or long-press, but these are not essential for typical agent tasks.

    Maintenance

    ActivityInactive
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers