Skip to main content
Glama

agent-mobile-auto

AI-Native 跨平台移动端真机自动化引擎(Android uiautomator2/ADB + 非越狱 iOS pymobiledevice3/Loopback WDA)
面向 Claude Code / Cursor / Antigravity / OpenCode 的统一移动端控制底座:L1/L2 紧凑 UI 快照、Set-of-Mark (SoM) 视觉标注截图、跨进程独占租约与人工接管锁、batch --stdin 单会话流水线、零依赖原生 MCP Server。

与 agent-browser-bridge(浏览器 CDP + 桌面桥接)和 agent-desktop(桌面 OS 可访问性树)一脉相承,共同组成 AI-Native 全端自动化三件套(Browser + Desktop + Mobile)。


为什么做 agent-mobile-auto?

传统移动端自动化脚本直接套给 AI Agent 往往会遇到四个硬伤:

  1. 原始 XML 极其费 Token:单屏 Android/iOS 无障碍树动辄 10k–15k Tokens,塞满上下文还拖慢推理。

  2. 多 Agent / 人机抢手机导致误点:页面异步弹窗、列表滚动或人工介入后,旧坐标和旧编号直接点偏。

  3. 每步冷启动开销大:传统 CLI 每步重启进程、重连设备,多步操作延迟极高。

  4. 缺少原生 MCP 与视觉融合:纯无障碍树在 Flutter / Canvas / 动态 H5 上容易漏节点,纯截图又缺结构化坐标。

agent-mobile-auto 用一套干净的协议层系统性解决以上问题:

核心能力

设计与效果

L1 紧凑快照 + L2 语义差分

过滤空容器、统一 Android/iOS 角色名,单屏减少 ~90%+ Token;L2 Diff 仅输出增删改节点,并用 (was [eXX]) 追踪重排编号

强绑定 Scoped Ref (@snap:e01)

对齐 agent-desktop,拒绝无快照上下文的裸 e01;点击前实时复核前台 App、屏幕方向、中心位移(≤24px)与新增浮层遮挡

Set-of-Mark (SoM) 标注截图

自动处理 iOS Retina 2x/3x 逻辑坐标缩放,在截图上叠加高对比度 [e01] 编号框与可选缩图压缩,直接通过 MCP image 块喂给 VLM

跨进程独占租约 + --manual 接管锁

FileLock + 心跳边车 + pid_exists 存活判定;人工接管时阻塞所有 AI 写入(退出码 2)并递增 lease_epoch 使旧快照立即失效

模糊超时保护 (NEEDS_REVIEW)

写操作 POST 响应丢失/超时不盲目重放,返回 NEEDS_REVIEW 并强制重新 observe 核实状态

Android 毫秒级热态 + 微信/OEM 穿透 + IME 零破坏

默认 uiautomator2 常驻 RPC + waitForIdleTimeout=0 缓存,自动解析 adbutils 内置 adb(免配 PATH);遇微信/OriginOS 等拦截 u2 截图或点击时自动无缝降级至 ADB screencap/input;三级 Unicode 输入链路(AdbKeyboard → focused.set_text → 剪贴板 + KEYCODE_PASTE),穿透无障碍屏蔽应用并在 try/finally 中 100% 还原原输入法

iOS Loopback 安全边界

WDA 强制仅限 127.0.0.1 / localhost,执行端口-UDID-iOS 版本 fail-closed 归属校验与 7 天免费签名到期预警,back() 显式返回 UNSUPPORTED_ACTION

语义定位器 + App learnings/ + Code-First exec(v0.2 ego-lite 范式)

支持 role:Button[name="微信"]、text~="Wi-Fi"、id:search_bar >> nth=0;按 App 包名自动加载 learnings/ 避坑经验(如微信自动切 Socket+剪贴板快路径);agent-mobile exec 单会话执行带 if/for 分支的受控 Python 片段,把 8 轮交互压成 1 轮

归一化坐标 + 画面感知哈希 Diff + Deep-Link

支持 --rx / --ry(0.0–1.0)跨分辨率通吃、Pillow 64-bit dHash 动作后验画面变化(--verify-frame-change),以及 open-url Deep-Link / Android Intent 直达内页

零副作用热连接 Daemon + MCP 共享连接池(v0.3)

严格仅绑 127.0.0.1 + Bearer Token + 空闲 15 分钟自动退出的本机常驻 Daemon,复用热设备连接与缓存消除 CLI 冷启动;MCP Server 内置同款 SessionPool;可选零录屏发热的 scrcpy 纯 Control Socket(video=false audio=false)实现 <15ms 触摸注入

统一 JSON Envelope + Batch + 原生 MCP

所有 CLI 输出统一结构化信封;batch --stdin 单会话多步流水线(支持语义定位器、@latest:e01、tap_rel);内置零依赖 stdio MCP Server(17 个 mobile_* 工具)


Related MCP server: Zeus Mobile MCP

架构总览

flowchart TD
    A["AI Agent / User<br/>(Claude Code / Cursor / Antigravity / Shell)"] --> B["CLI (agent-mobile)<br/>JSON Envelope + batch --stdin"]
    A --> C["Built-in MCP Server<br/>(agent-mobile mcp over stdio)"]
    B --> D["Shared Engine & Batch Runner (engine.py)"]
    C --> D
    D --> E["Safety Guardrails<br/>DeviceLease + --manual + lease_epoch<br/>SnapshotTracker + Pre-Tap Freshness"]
    E --> F["Snapshot & Vision Layer<br/>L1 Compact / L2 Diff / SoM Overlay (Pillow)"]
    E --> G["AndroidAdapter<br/>uiautomator2 RPC (default) / ADB fallback"]
    E --> H["IosAdapter<br/>pymobiledevice3 (R0) + Loopback WDA (R1)"]
    G --> I["Android Device (Explicit Serial)"]
    H --> J["iOS Device (Explicit UDID, 127.0.0.1 WDA)"]

快速开始

环境要求

  • Python 3.11+ 与 uv(推荐)

  • Android:已安装 adb 且开启 USB 调试

  • iOS(可选):安装 uv sync --extra ios(引入 pymobiledevice3),并在本地 127.0.0.1:<port> 转发已签名的 WebDriverAgent

安装

git clone https://github.com/linxi27667/agent-mobile-auto.git
cd agent-mobile-auto

# 基础安装(Android + MCP + SoM)
uv sync

# 包含 iOS 支持与开发测试工具
uv sync --extra ios --extra dev

CLI 常用命令

# 0. Android 11+ WLAN 无线调试配对与 mDNS 发现(免插线、免手动配 adb PATH)
uv run agent-mobile wifi pair 192.168.1.50:37000 123456
uv run agent-mobile wifi mdns
uv run agent-mobile wifi connect 192.168.1.50:41000

# 1. 发现已连接的 Android / iOS 设备
uv run agent-mobile devices

# 2. 单设备健康体检(能力矩阵、租约、iOS 签名倒计时)
uv run agent-mobile doctor --platform android --device <SERIAL>

# 3. 抓取紧凑 L1 UI 快照(-i 仅显示可交互控件)
uv run agent-mobile observe --platform android --device <SERIAL> --mode compact -i

# 4. 仅看与上一屏的变化(L2 语义差分)
uv run agent-mobile observe --platform android --device <SERIAL> --mode diff

# 5. 按文本/角色语义查找元素,返回强绑定 scoped_ref
uv run agent-mobile find --platform android --device <SERIAL> --text "Wi-Fi" -i

# 6. 生成 Set-of-Mark (SoM) 编号标注截图(自动压缩至 720px 宽)
uv run agent-mobile screenshot --platform android --device <SERIAL> --som --max-width 720 --out screen_som.png

# 7. 使用语义定位器、强绑定 Scoped Ref 或归一化相对坐标点击(可附加 --verify-frame-change 画面 Diff)
uv run agent-mobile tap 'role:Button[name="Wi-Fi"]' --platform android --device <SERIAL>
uv run agent-mobile tap '@snap-1-0001:e03' --platform android --device <SERIAL>
uv run agent-mobile tap --rx 0.83 --ry 0.078 --verify-frame-change --platform android --device <SERIAL>

# 8. Code-First Runner:单会话内执行带条件分支的 Python 流程(1 轮搞定复杂分支)
uv run agent-mobile exec --platform android --device <SERIAL> --code "
mobile.launch('com.tencent.mm')
mobile.sleep(0.8)
snap = mobile.observe()
if snap.foreground_app == 'com.vivo.doubleinstance':
    mobile.tap('id:main', refresh=False)
result = {'app': snap.foreground_app}
"

# 9. Deep-Link / Android Intent 直达与 App learnings 知识库
uv run agent-mobile open-url --url android.settings.WIFI_SETTINGS --platform android --device <SERIAL>
uv run agent-mobile learnings list
uv run agent-mobile learnings show com.tencent.mm

# 8. 等待某个文本或应用出现(支持 --gone 等待消失)
uv run agent-mobile wait --platform android --device <SERIAL> --text "已连接" --timeout 10

# 10. 启动零副作用热连接 Daemon(可选,CLI 自动复用热连接,空闲 15 分钟自动退出)
uv run agent-mobile daemon start
uv run agent-mobile daemon status
uv run agent-mobile daemon stop

# 11. 人工接管手机(阻塞所有 AI 写入并作废旧快照)/ 释放 / 单设备隔离恢复
uv run agent-mobile lease --platform android --device <SERIAL> --manual
uv run agent-mobile lease --platform android --device <SERIAL> --release
uv run agent-mobile lease --platform android --device <SERIAL> --recover

PowerShell 提示:在 PowerShell 中请用单引号包裹带 @ 的引用(如 '@snap-1-0001:e03'),避免被识别为 splatting 语法。

单会话 Batch 流水线(消除 CLI 冷启动)

通过 batch --stdin 在同一个进程、同一次设备连接和同一个租约内顺序执行多步操作,并支持 @latest:e01 动态绑定上一步 observe 生成的快照:

echo '[
  {"action": "launch", "app": "com.android.settings"},
  {"action": "observe", "mode": "compact", "interactive_only": true},
  {"action": "tap", "ref": "@latest:e01"},
  {"action": "type", "text": "Wi-Fi", "clear": true},
  {"action": "press_key", "key": "enter"},
  {"action": "wait", "text": "Wi-Fi", "timeout_s": 5}
]' | uv run agent-mobile batch --platform android --device <SERIAL> --stdin

接入 MCP(Claude Code / Cursor / Antigravity / OpenCode)

agent-mobile mcp 内置零依赖 JSON-RPC 2.0 stdio 服务器,直接写入客户端 MCP 配置即可:

{
  "mcpServers": {
    "agent-mobile-auto": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/path/to/agent-mobile-auto",
        "agent-mobile",
        "mcp"
      ]
    }
  }
}

暴露的 MCP 工具(17 个)

类别

工具名

说明

发现与体检

mobile_devices, mobile_doctor, mobile_learnings

列举设备、健康检查、能力矩阵与 App learnings/ 知识库

观察与视觉

mobile_observe, mobile_find, mobile_wait, mobile_screenshot

L1/L2 快照、语义查找、轮询等待、原始/SoM 标注截图(直接返回 image content block)

交互动作

mobile_tap, mobile_long_press, mobile_swipe, mobile_type, mobile_press_key

支持语义定位器 / @snap:ref / 归一化坐标 (rx, ry) + 画面 dHash 核验、滑动、Unicode 三级输入、按键

应用与编排

mobile_app, mobile_open_url, mobile_lease, mobile_batch, mobile_exec

应用启停/列表、Deep-Link 直达、人工接管锁与隔离恢复、batch 流水线与 Code-First exec 运行器

给 AI Agent 的内置 Skill

仓库在 skills/agent-mobile-auto/SKILL.md 提供标准 Agent Skill,也可以直接通过命令输出:

uv run agent-mobile skill

统一 JSON 响应契约

所有 CLI 命令与 MCP 文本块共享同一套 Envelope 结构,方便 Agent 准确判断错误码与重试策略:

{
  "version": "1.0",
  "ok": false,
  "command": "tap",
  "error": {
    "code": "MANUAL_TAKEOVER",
    "message": "Device android:DEV01 is locked in manual takeover mode (owner=human)",
    "retryable": false,
    "details": {}
  }
}

退出码

典型 error.code

含义与 Agent 应对策略

0

—

执行成功(若返回 data.state == "NEEDS_REVIEW",须先 observe 核实,禁重放)

1

STALE_SNAPSHOT / ELEMENT_NOT_FOUND / TIMEOUT

快照陈旧或元素未命中,重新 observe 后再决策

1

UNSUPPORTED_ACTION / PORT_OWNERSHIP_CONFLICT

平台不支持或端口归属冲突,不重试

2

LEASE_BUSY / MANUAL_TAKEOVER

设备被其他 Agent 占用或处于人工接管锁,立即退让


开发与测试

项目采用纯 Python 跨平台测试(Windows / macOS / Linux),全部核心逻辑均有离线单元测试覆盖(无需连接真机):

uv sync --extra dev
uv run ruff check src tests
uv run pytest -v

安全边界

  • 本项目为通用移动设备自动化与测试底座,不包含绕过风控、自动付款、代登录、破解解锁或私密数据抓取能力。

  • 支付、下单、实名认证、2FA、锁屏解锁及 iOS 开发者信任等高敏环节须严格交由人工操作(配合 lease --manual)。

  • 切勿向 Git 提交真实设备序列号、UDID、签名证书、.ipa/.apk 或包含个人隐私的截图。

License

MIT © 2026 Jeason Liu (linxi27667)

Tool Schema Changelog

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

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      B
      maintenance
      Enables AI agents to automate Android and iOS devices, including emulators and physical devices, by capturing screenshots, reading UI accessibility trees, performing touch and text input, and managing app lifecycle and logs.
      18
      4
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables local, air-gapped automation of Android and iOS devices and emulators, letting AI agents inspect UI hierarchies, tap, swipe, type, manage apps, capture screenshots, and pull device logs and crash reports. Bundles banking-grade safeguards such as automatic PII and secret redaction, PCI-DSS-style log sanitization, and configurable screenshot-blocking DLP policies.
      Apache 2.0
    • A
      license
      Not graded
      quality
      C
      maintenance
      Turns a real cloud iPhone or Android device into an environment an agent drives through up to eighteen capability-gated tools for reading the screen, tapping, swiping, typing, pressing keys and launching apps. The device also records video and a timed trajectory of every action for later inspection.
      8
      Apache 2.0
    • A
      license
      A
      quality
      B
      maintenance
      Enables agents and LLMs to automate, inspect, and scrape native iOS and Android apps across simulators, emulators, and real devices through a platform-agnostic interface using accessibility snapshots or screenshot-based coordinates. It provides device, app, screen, input, log, and crash management tools, including optional cloud device reservation.
      34
      Apache 2.0