android-adb-mcp

android-adb-mcp

An MCP server that wraps Android ADB functionality into AI assistant tools, enabling device management, shell execution, file operations, app management, media capture, and log analysis via natural language.

Category
Visit Server

README

ADB MCP Server

Build Status codecov npm version License: MIT

🚀 Production-Ready MCP Server for Android Device Management

一个功能完整、生产就绪的 Model Context Protocol (MCP) 服务器,将 Android Debug Bridge (ADB) 功能封装为现代化的 AI 助手工具。提供 35 个专业工具,覆盖 Android 设备管理的所有核心场景。

⚡ 快速体验

直接运行(无需安装)

npx android-adb-mcp

Claude Desktop 配置

将以下配置添加到 Claude Desktop 的 claude_desktop_config.json 文件:

{
  "mcpServers": {
    "android-devices": {
      "command": "npx",
      "args": ["android-adb-mcp"],
      "env": {
        "ADB_PATH": "/usr/local/bin/adb"
      }
    }
  }
}

重启 Claude Desktop,即可开始使用 AI 助手管理您的 Android 设备!

💡 使用示例

配置完成后,您可以在 Claude Desktop 中使用自然语言管理 Android 设备:

用户: 列出所有连接的 Android 设备
助手: 我来为您检查连接的设备...
[调用 list_devices 工具]
找到 1 个设备:4745ca3b (Redmi Note 7 Pro)

用户: 获取设备的详细信息
助手: 正在获取设备详细信息...
[调用 get_device_info 工具]
设备信息:
- 型号:Redmi Note 7 Pro
- Android 版本:10
- 内存:6GB
- 存储:128GB

用户: 截取一张屏幕截图
助手: 正在为您截取屏幕...
[调用 capture_screenshot 工具]
✅ 截图已保存到:/tmp/screenshot_20241106.png

用户: 安装一个 APK 文件
助手: 请提供 APK 文件路径,我来帮您安装...
[调用 install_app 工具]

✨ 核心特性

🎯 全面的设备管理

  • 设备发现: 自动识别并列出所有连接的 Android 设备
  • 设备信息: 获取详细的设备规格、系统版本、硬件信息
  • 连接管理: 支持USB和无线TCP/IP连接
  • 状态监控: 实时检查设备连接状态和健康度

🛡️ 安全的Shell执行

  • 命令白名单: 严格的安全命令验证机制
  • 沙箱执行: 防止恶意命令执行的安全沙箱
  • 权限控制: 基于命令类型的细粒度权限管理
  • 审计日志: 完整的命令执行审计追踪

📁 强大的文件操作

  • 批量传输: 支持大文件的分块传输
  • 目录管理: 完整的目录创建、删除、浏览功能
  • 文件信息: 详细的文件属性和元数据获取
  • 安全验证: 路径遍历攻击防护

📱 完整的应用管理

  • 应用生命周期: 安装、卸载、启动、停止、清除数据
  • 应用信息: 详细的包信息、权限、版本信息
  • 批量操作: 支持多应用的批量管理操作
  • 系统应用: 区分系统和用户应用的管理策略

🎬 专业的媒体操作

  • 屏幕捕获: 高质量屏幕截图和录制
  • 显示管理: 屏幕密度、分辨率动态调整
  • 媒体格式: 支持多种截图和视频格式
  • 实时预览: 屏幕内容的实时获取

📊 高级日志分析

  • 多源日志: logcat、崩溃日志、ANR日志、内核日志
  • 智能过滤: 基于标签、级别、时间的智能过滤
  • 流式处理: 实时日志流式传输
  • 诊断分析: 自动化的错误诊断和问题定位

高级功能

  • 批量执行: 多命令序列化执行,支持条件分支
  • 性能监控: CPU、内存、磁盘、网络、电池实时监控
  • 无线管理: 无线ADB连接的自动发现和管理
  • 流式传输: 大文件的分块流式传输

🚀 快速开始

前置要求

  • Node.js: >= 18.0.0
  • Android SDK: 配置完整的 ADB 工具
  • 设备权限: 启用 USB 调试的 Android 设备

📦 安装方式

方式一:直接使用 npx(推荐)

# 直接运行(无需安装)
npx android-adb-mcp

# 或者指定 ADB 路径
ADB_PATH=/path/to/adb npx android-adb-mcp

方式二:全局安装

# 全局安装
npm install -g android-adb-mcp

# 启动服务
android-adb-mcp

# 或者使用 npx
npx android-adb-mcp

方式三:项目本地安装

# 本地安装
npm install android-adb-mcp

# 启动服务
npx android-adb-mcp

方式四:从源码构建

# 克隆项目
git clone https://github.com/qinxiandiqi/android-adb-mcp.git
cd adb-mcp

# 安装依赖
pnpm install

# 构建项目
pnpm build

# 启动服务器
pnpm start

🐳 Docker 部署

# 使用预构建镜像
docker run -d --name adb-mcp --privileged -v /dev/bus/usb:/dev/bus/usb qinxiandiqi/android-adb-mcp

# 或者从源码构建
docker build -t adb-mcp .
docker run -d --name adb-mcp --privileged -v /dev/bus/usb:/dev/bus/usb adb-mcp

📋 工具清单

🔧 设备工具 (6个)

工具名称 功能描述 参数
list_devices 列出所有连接的设备 -
get_device_info 获取设备详细信息 deviceId
connect_device 连接TCP/IP设备 host, port
disconnect_device 断开设备连接 host, port
wait_for_device 等待设备连接 deviceId, timeout
check_device_status 检查设备状态 deviceId

🐚 Shell工具 (5个)

工具名称 功能描述 参数
execute_shell 执行安全shell命令 command, deviceId
get_system_properties 获取系统属性 property, deviceId
list_processes 列出运行进程 deviceId
get_memory_info 获取内存信息 deviceId
get_disk_usage 获取磁盘使用情况 path, deviceId

📁 文件工具 (6个)

工具名称 功能描述 参数
list_files 列出文件和目录 path, deviceId
push_file 推送文件到设备 localPath, remotePath, deviceId
pull_file 从设备拉取文件 remotePath, localPath, deviceId
create_directory 创建目录 path, deviceId
remove_file 删除文件或目录 path, deviceId
get_file_info 获取文件信息 path, deviceId

📱 应用工具 (7个)

工具名称 功能描述 参数
list_apps 列出已安装应用 filter, deviceId
get_app_info 获取应用信息 packageName, deviceId
install_app 安装APK apkPath, deviceId
uninstall_app 卸载应用 packageName, deviceId
start_app 启动应用 packageName, deviceId
stop_app 停止应用 packageName, deviceId
clear_app_data 清除应用数据 packageName, deviceId

📸 媒体工具 (6个)

工具名称 功能描述 参数
capture_screenshot 截取屏幕截图 savePath, deviceId
record_screen 录制屏幕 duration, savePath, deviceId
get_screen_info 获取屏幕信息 deviceId
set_screen_density 设置屏幕密度 density, deviceId
reset_screen_density 重置屏幕密度 deviceId
get_display_modes 获取显示模式 deviceId

📋 日志工具 (6个)

工具名称 功能描述 参数
get_logs 获取logcat日志 level, tag, count, deviceId
clear_logs 清除日志缓冲区 buffer, deviceId
get_crash_logs 获取崩溃日志 packageName, count, deviceId
get_anr_logs 获取ANR日志 packageName, count, deviceId
get_kernel_logs 获取内核日志 count, deviceId
stream_logs 流式获取日志 filters, deviceId

🚀 高级工具 (5个)

工具名称 功能描述 参数
execute_batch 批量执行命令 commands, errorStrategy
get_performance_metrics 获取性能指标 metrics, deviceId
manage_wireless_connection 管理无线连接 action, host, port
stream_file 流式传输文件 source, destination, chunkSize
analyze_storage 分析存储使用 path, deviceId

⚙️ 配置

环境变量

变量名 描述 默认值
ADB_PATH ADB可执行文件路径 adb
ADB_HOST ADB服务器主机 localhost
ADB_PORT ADB服务器端口 5037
ADB_TIMEOUT 命令执行超时(秒) 30
LOG_LEVEL 日志级别 info

⚙️ MCP 客户端配置

方式一:使用 npx(推荐)

{
  "mcpServers": {
    "android-devices": {
      "command": "npx",
      "args": ["android-adb-mcp"],
      "env": {
        "ADB_PATH": "/path/to/platform-tools/adb",
        "LOG_LEVEL": "debug"
      }
    }
  }
}

方式二:全局安装

{
  "mcpServers": {
    "android-devices": {
      "command": "android-adb-mcp",
      "args": [],
      "env": {
        "ADB_PATH": "/path/to/platform-tools/adb",
        "LOG_LEVEL": "debug"
      }
    }
  }
}

方式三:本地安装

{
  "mcpServers": {
    "android-devices": {
      "command": "node",
      "args": ["./node_modules/android-adb-mcp/dist/src/index.js"],
      "env": {
        "ADB_PATH": "/path/to/platform-tools/adb",
        "LOG_LEVEL": "debug"
      }
    }
  }
}

方式四:从源码运行

{
  "mcpServers": {
    "android-devices": {
      "command": "node",
      "args": ["/absolute/path/to/adb-mcp/dist/src/index.js"],
      "env": {
        "ADB_PATH": "/path/to/platform-tools/adb",
        "LOG_LEVEL": "debug"
      }
    }
  }
}

🖥️ Claude Desktop 配置示例

快速配置(npx 方式)

{
  "mcpServers": {
    "android-devices": {
      "command": "npx",
      "args": ["android-adb-mcp"],
      "env": {
        "ADB_PATH": "/usr/local/bin/adb"
      }
    }
  }
}

完整配置示例

{
  "mcpServers": {
    "android-devices": {
      "command": "npx",
      "args": ["android-adb-mcp"],
      "env": {
        "ADB_PATH": "/usr/local/bin/adb",
        "ADB_HOST": "localhost",
        "ADB_PORT": "5037",
        "ADB_TIMEOUT": "30",
        "LOG_LEVEL": "info"
      }
    }
  }
}

🛡️ 安全特性

多层安全防护

  1. 命令验证: 基于白名单的安全命令验证
  2. 路径安全: 防止路径遍历和目录逃逸攻击
  3. 输入验证: 使用Zod进行严格的类型和数据验证
  4. 权限控制: 细粒度的操作权限管理
  5. 审计日志: 完整的操作审计和安全日志

安全命令列表

允许执行的Shell命令类别:

  • 系统信息获取 (getprop, dumpsys, wm, am)
  • 文件操作 (ls, cat, stat, mkdir, rm)
  • 进程管理 (ps, top)
  • 网络诊断 (ping, ip, netstat)
  • 日志管理 (logcat, dmesg)

🏗️ 项目架构

技术栈

  • 运行时: Node.js 18+
  • 语言: TypeScript 5.9+
  • 协议: Model Context Protocol (MCP) 2024-11-05
  • 验证: Zod 4.x
  • 测试: Jest 30+
  • 构建: TypeScript + tsc-alias
  • 代码质量: ESLint 9.x + TypeScript ESLint

架构设计

┌─────────────────────────────────────┐
│           MCP Protocol Layer        │
│  ┌─────────────┐  ┌───────────────┐ │
│  │ JSON-RPC    │  │ Tool Registry │ │
│  │ Handler     │  │ & Discovery   │ │
│  └─────────────┘  └───────────────┘ │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│            Business Layer           │
│  ┌─────────────┐  ┌───────────────┐ │
│  │   Tool      │  │   Input       │ │
│  │   Modules   │  │ Validation    │ │
│  │ (35 tools)  │  │   (Zod)       │ │
│  └─────────────┘  └───────────────┘ │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│          Infrastructure Layer       │
│  ┌─────────────┐  ┌───────────────┐ │
│  │   ADB       │  │   Utility     │ │
│  │   Client    │  │   Functions   │ │
│  └─────────────┘  └───────────────┘ │
└─────────────────────────────────────┘

目录结构

adb-mcp/
├── src/                          # 源代码
│   ├── adb/                      # ADB客户端实现
│   │   ├── client.ts             # 主要ADB客户端类
│   │   └── types.ts              # 类型定义
│   ├── tools/                    # MCP工具模块 (35个工具)
│   │   ├── device-tools.ts       # 设备管理 (6个)
│   │   ├── shell-tools.ts        # Shell命令 (5个)
│   │   ├── file-tools.ts         # 文件操作 (6个)
│   │   ├── app-tools.ts          # 应用管理 (7个)
│   │   ├── media-tools.ts        # 媒体操作 (6个)
│   │   ├── log-tools.ts          # 日志访问 (6个)
│   │   └── advanced-tools.ts     # 高级功能 (5个)
│   ├── utils/                    # 工具函数
│   │   └── validation.ts         # 输入验证和安全
│   └── index.ts                  # MCP服务器入口
├── tests/                        # 测试文件
│   ├── unit/                     # 单元测试 (35+ 测试文件)
│   ├── integration/              # 集成测试
│   ├── mocks/                    # 测试模拟
│   └── benchmark/                # 性能基准测试
├── .github/workflows/            # CI/CD 配置
├── docs/                         # 文档
├── scripts/                      # 构建脚本
└── dist/                         # 构建输出

🧪 测试

运行测试

# 运行所有测试
pnpm test

# 运行测试并生成覆盖率报告
pnpm test:coverage

# 监听模式运行测试
pnpm test:watch

# 运行特定测试
pnpm test device-tools

测试覆盖

  • 单元测试: 100% 工具模块覆盖
  • 集成测试: 完整的ADB客户端测试
  • 安全测试: 输入验证和安全检查测试
  • 性能测试: 基准测试和性能回归测试

测试统计

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 src      |     100 |      100 |     100 |     100 |
  adb     |     100 |      100 |     100 |     100 |
  tools   |     100 |      100 |     100 |     100 |
  utils   |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------

🚀 部署

生产部署

# 构建生产版本
pnpm build

# 启动生产服务器
NODE_ENV=production node dist/src/index.js

PM2 部署

# 安装 PM2
npm install -g pm2

# 启动服务
pm2 start ecosystem.config.js

# 监控状态
pm2 monit

Docker 部署

FROM node:18-alpine

# 安装 ADB
RUN apk add --no-cache android-tools

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

COPY dist ./dist
EXPOSE 3000

CMD ["node", "dist/src/index.js"]

📊 性能

基准测试结果

操作类型 平均响应时间 QPS 内存占用
设备列表 45ms 22/s 12MB
Shell命令 120ms 8/s 15MB
文件传输 250ms 4/s 20MB
截图操作 800ms 1.2/s 25MB

性能优化

  • 连接池: ADB连接复用和池化
  • 缓存机制: 设备信息和系统属性缓存
  • 并发控制: 合理的并发限制和队列管理
  • 内存管理: 及时释放大文件和临时数据

🔧 开发

开发环境设置

# 克隆项目
git clone https://github.com/qinxiandiqi/android-adb-mcp.git
cd adb-mcp

# 安装依赖
pnpm install

# 开发模式 (热重载)
pnpm dev

# 类型检查
pnpm type-check

# 代码检查
pnpm lint

# 格式化代码
pnpm format

添加新工具

  1. 创建工具文件:
// src/tools/my-tool.ts
import { z } from 'zod';
import { AdbClient } from '../adb/client.js';

export const myTool = {
  name: 'my_tool',
  description: 'Tool description',
  inputSchema: {
    type: 'object',
    properties: {
      param1: { type: 'string', description: 'Parameter description' }
    },
    required: ['param1']
  },
  handler: async (args) => {
    const client = new AdbClient();
    // 实现工具逻辑
    return { success: true, data: result };
  }
};
  1. 注册工具:
// src/index.ts
import { myTool } from './tools/my-tool.js';

// 在工具注册中添加
const allTools = {
  ...deviceTools,
  ...myTool, // 添加新工具
};
  1. 添加测试:
// tests/unit/tools/my-tool.test.ts
import { myTool } from '../../../src/tools/my-tool.js';

describe('myTool', () => {
  it('should execute successfully', async () => {
    const result = await myTool.handler({ param1: 'test' });
    expect(result.success).toBe(true);
  });
});

贡献指南

  1. Fork 项目
  2. 创建功能分支: git checkout -b feature/amazing-feature
  3. 提交更改: git commit -m 'Add amazing feature'
  4. 推送分支: git push origin feature/amazing-feature
  5. 创建 Pull Request

代码规范

  • 使用 TypeScript 严格模式
  • 遵循 ESLint 和 Prettier 配置
  • 编写完整的单元测试
  • 添加 JSDoc 注释
  • 遵循语义化版本控制

🔍 故障排除

常见问题

ADB 连接问题

# 检查 ADB 服务状态
adb devices

# 重启 ADB 服务
adb kill-server
adb start-server

# 检查设备授权
adb devices -l

权限问题

# Linux/Mac: 检查 USB 权限
sudo usermod -aG plugdev $USER

# 重新加载 udev 规则
sudo udevadm control --reload-rules

网络连接问题

# 检查防火墙设置
sudo ufw status

# 测试端口连通性
telnet localhost 5037

调试模式

# 启用详细日志
DEBUG=adb-mcp:* pnpm start

# 启用 ADB 调试
ADB_TRACE=1 pnpm start

# 启用网络调试
NODE_DEBUG=net pnpm start

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

🤝 贡献者

感谢所有为这个项目做出贡献的开发者!

<a href="https://github.com/qinxiandiqi/android-adb-mcp/graphs/contributors"> <img src="https://contrib.rocks/image?repo=qinxiandiqi/android-adb-mcp" /> </a>

🙏 致谢

📞 支持


<div align="center">

⬆ 回到顶部

Made with ❤️ by the ADB MCP Team

</div>

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured