Android设备控制服务器

一个通过ADB提供程序化控制Android设备的MCP服务器,支持ADB命令执行、设备截图捕获、UI布局分析和设备包管理等功能。

  • by minhalvp
  • Python
  • 本地部署
  • 811 星标
  • 更新于
在 GitHub 查看 minhalvp/android-mcp-server

使用说明

安卓MCP服务器

MCP(模型上下文协议)服务器,提供对以下内容的编程控制 Android设备通过ADB(Android调试桥)。此服务器公开 MCP可以访问的各种Android设备管理功能 客户喜欢 克劳德桌面 和代码编辑器 (例如。 光标)

特性

  • 🔧 ADB命令执行
  • 📸 设备截图
  • 🎯 UI布局分析
  • 📱 设备包管理

先决条件

  • Python 3.x
  • ADB(安卓调试桥)已安装并配置
  • Android设备或模拟器(未测试)

安装

  1. 克隆存储库:
git clone https://github.com/minhalvp/android-mcp-server.git
cd android-mcp-server
  1. 安装依赖项: 此项目使用 紫外线 对于项目 通过各种方法进行管理 安装.
uv python install 3.11
uv sync

配置

服务器支持多种使用场景的灵活设备配置。

设备选择模式

1.自动选择(建议用于单个设备)

  • 不需要配置文件
  • 自动连接到唯一连接的设备
  • 非常适合使用单个测试设备进行开发

2.手动设备选择

  • 当您连接了多个设备时使用
  • 在配置文件中指定确切的设备

配置文件(可选)

配置文件(config.yaml)是 可选的。如果不存在,如果只有一个设备连接,服务器将自动选择该设备。

用于自动选择

只需确保只连接了一台设备并运行服务器即可,无需配置!

用于手动选择

  1. 创建配置文件:
cp config.yaml.example config.yaml
  1. 编辑 config.yaml 并指定您的设备:
device:
  name: "your-device-serial-here" # Device identifier from 'adb devices'

用于自动选择,您可以使用以下任何方法:

device:
  name: null              # Explicit null (recommended)
  # name: ""              # Empty string  
  # name:                 # Or leave empty/comment out

查找您的设备序列号

要查找您的设备标识符,请运行:

adb devices

输出示例:

List of devices attached
13b22d7f        device
emulator-5554   device

使用第一列值(例如。, 13b22d7f 或 emulator-5554)作为设备名称。

使用场景

场景 需要配置 行为
单个设备已连接 无 ✅ 自动连接到设备
多个设备,需要特定的一个 config.yaml 随着 device.name ✅ 连接到指定设备
多个设备,无配置 无 ❌ 显示可用设备的错误
未连接设备 N/A ❌ 显示“无设备”错误

备注:如果您连接了多个设备,但未指定使用哪一个,服务器将显示一条错误消息,列出所有可用设备。

用法

需要MCP客户端才能使用此服务器。Claude Desktop应用程序就是一个例子 MCP客户端。要将此服务器与Claude Desktop一起使用,请执行以下操作:

  1. 找到您的Claude Desktop配置文件:

    • 窗户: %APPDATA%\Claude\claude_desktop_config.json
    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  2. 将Android MCP服务器配置添加到 mcpServers 章节:

{
  "mcpServers": {
    "android": {
      "command": "path/to/uv",
      "args": ["--directory", "path/to/android-mcp-server", "run", "server.py"]
    }
  }
}

替换:

  • path/to/uv 与您的实际路径 uv 可执行
  • path/to/android-mcp-server 带有克隆此文件的绝对路径 仓库

https://github.com/user-attachments/assets/c45bbc17-f698-43e7-85b4-f1b39b8326a8

可用工具

服务器公开了以下工具:

def get_packages() -> str:
    """
    Get all installed packages on the device.
    Returns:
        str: A list of all installed packages on the device as a string
    """
def execute_adb_command(command: str) -> str:
    """
    Executes an ADB command and returns the output.
    Args:
        command (str): The ADB command to execute
    Returns:
        str: The output of the ADB command
    """
def get_uilayout() -> str:
    """
    Retrieves information about clickable elements in the current UI.
    Returns a formatted string containing details about each clickable element,
    including their text, content description, bounds, and center coordinates.

    Returns:
        str: A formatted list of clickable elements with their properties
    """
def get_screenshot() -> Image:
    """
    Takes a screenshot of the device and returns it.
    Returns:
        Image: the screenshot
    """
def get_package_action_intents(package_name: str) -> list[str]:
    """
    Get all non-data actions from Activity Resolver Table for a package
    Args:
        package_name (str): The name of the package to get actions for
    Returns:
        list[str]: A list of all non-data actions from the Activity Resolver
        Table for the package
    """

贡献

欢迎投稿!

致谢