Skip to main content
Glama
UIAutomation MCP Server

UIAutomation MCP Server

by dividduang
README.md
# UIAutomation MCP Server

[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![MCP](https://img.shields.io/badge/Protocol-MCP-green.svg)](https://modelcontextprotocol.io/)

> 基于 [Python-UIAutomation-for-Windows](https://github.com/yinkaisheng/Python-UIAutomation-for-Windows) 的 MCP (Model Context Protocol) 服务器

**特别感谢** [yinkaisheng](https://github.com/yinkaisheng) 开发的优秀 UI 自动化库,本项目是其 MCP 封装实现。

---

## 简介

UIAutomation MCP Server 让 AI 助手(如 Claude、ChatGPT)能够通过 MCP 协议直接操作 Windows UI 控件,实现:

- 自动化桌面应用程序操作
- 控件信息查询与截图
- 键盘鼠标模拟
- 窗口管理

支持的应用类型:Win32、MFC、WPF、Windows Forms、Modern UI (UWP)、Qt、Firefox、Chrome、Electron 等。

## 安装

```bash
pip install -e .          # 或 uv sync
pip install comtypes overlay-arrows-and-more
```

> `overlay-arrows-and-more` 只有 `ui_highlight` 高亮工具需要;未安装时该工具会返回
> `DEPENDENCY_MISSING`,其余工具不受影响。

## 配置

在 Claude Desktop / Claude Code / Cursor / Kiro 等 MCP 客户端的配置文件中添加:

```json
{
  "mcpServers": {
    "uiautomation": {
      "command": "uiautomation-mcp",
      "type": "stdio"
    }
  }
}
```

也可以不安装、直接用源码运行:

```json
{
  "mcpServers": {
    "uiautomation": {
      "command": "uv",
      "args": [
        "--directory",
        "D:\\path\\to\\Python-UIAutomation-for-Windows",
        "run",
        "main.py"
      ],
      "type": "stdio"
    }
  }
}
```


## 可用工具

### 控件查找 (6)

| 工具 | 描述 |
|------|------|
| `ui_find_window` | 查找顶层窗口 |
| `ui_find_control` | 查找子控件 |
| `ui_get_children` | 获取子控件列表 |
| `ui_get_focused` | 获取当前焦点控件 |
| `ui_get_foreground` | 获取前台窗口 |
| `ui_control_from_point` | 获取屏幕坐标处的控件 |

### 交互操作 (5)

| 工具 | 描述 |
|------|------|
| `ui_click` | 点击控件 (支持左键/右键/双击) |
| `ui_send_keys` | 发送键盘输入 |
| `ui_set_value` | 设置文本值 |
| `ui_close_window` | 关闭窗口 (需确认) |
| `ui_move_window` | 移动/调整窗口 |

### 信息查询 (6)

| 工具 | 描述 |
|------|------|
| `ui_get_properties` | 获取控件属性 |
| `ui_get_text` | 获取文本内容 |
| `ui_get_rect` | 获取边界矩形 |
| `ui_screenshot` | 控件/窗口截图 |
| `ui_exists` | 检查控件是否存在 |
| `ui_wait_for` | 等待条件满足 |

### Pattern 操作 (6)

| 工具 | 描述 |
|------|------|
| `ui_invoke` | 调用按钮 (InvokePattern) |
| `ui_toggle` | 切换状态 (TogglePattern) |
| `ui_expand_collapse` | 展开/折叠 (ExpandCollapsePattern) |
| `ui_select_item` | 选择项 (SelectionItemPattern) |
| `ui_scroll` | 滚动控件 (ScrollPattern) |
| `ui_terminate_process` | 终止进程 (需确认) |

### 辅助工具 (5)

| 工具 | 描述 |
|------|------|
| `ui_clipboard_get` | 获取剪贴板内容 |
| `ui_clipboard_set` | 设置剪贴板内容 |
| `ui_list_processes` | 列出运行中的进程 |
| `ui_show_desktop` | 显示/最小化所有窗口 |
| `ui_get_screen_size` | 获取屏幕尺寸 |

### 高亮与拾取 (3)

| 工具 | 描述 |
|------|------|
| `ui_highlight` | 在控件周围绘制高亮边框 (需 overlay-arrows-and-more) |
| `ui_interactive_pick` | 启动交互式拾取器 (鼠标指向控件 → 点「完成」) |
| `ui_pick_result` | 读取拾取结果 (searchDepth / codeSuggestion) |

## 环境变量

| 变量 | 默认值 | 描述 |
|------|--------|------|
| `UIAUTOMATION_LOG_LEVEL` | `INFO` | 日志级别 (DEBUG/INFO/WARNING/ERROR) |
| `UIAUTOMATION_TIMEOUT` | `10` | 默认操作超时秒数 |
| `UIAUTOMATION_ADMIN_CHECK` | `true` | 是否检查管理员权限 |
| `UIAUTOMATION_CONFIRMATION_ENABLED` | `true` | 危险操作确认机制 |

## 安全机制

以下操作需要用户确认才会执行:

- `ui_close_window` - 关闭窗口
- `ui_terminate_process` - 终止进程

可通过设置 `UIAUTOMATION_CONFIRMATION_ENABLED=false` 禁用确认机制(不推荐)。

## 使用示例

配置完成后,你可以对 AI 助手说:

```
请帮我打开记事本,输入 "Hello World",然后保存到桌面
```

```
截图当前活动窗口并保存
```

```
列出所有打开的 Chrome 标签页
```

## 系统要求

- **操作系统**: Windows 7 SP1 或更高版本
- **Python**: 3.10 或更高版本
- **权限**: 建议以管理员权限运行以获得最佳兼容性

## 注意事项

1. **管理员权限**: 要自动化以管理员权限运行的应用,MCP 服务器也需要管理员权限
2. **Chrome/Electron**: 需要添加 `--force-renderer-accessibility` 启动参数
3. **UWP 应用**: 部分现代应用可能有访问限制

## 致谢

- [yinkaisheng/Python-UIAutomation-for-Windows](https://github.com/yinkaisheng/Python-UIAutomation-for-Windows) - 核心库
- [Model Context Protocol](https://modelcontextprotocol.io/) - 协议规范

## License

Apache License 2.0