Skip to main content
Glama

bg-control-mcp

轻量级 Windows 本地 MCP 服务:把 后台静默 UI Automation(不抢焦点)、后台静默 Chrome/Edge/Electron CDP 自动化(穿透多层 iframe / OOPIF / Shadow DOM) 和 极速单窗前台定点输入 打包在一起,零 VPS、零复杂鉴权、超省 Token。

给 AI Agent / 人类维护者的说明:本仓库只包含 4 个精简 Python 文件,照下方安装步骤即可直接跑起来。

核心特点

  • 不抢你正在用的电脑:

    • 桌面窗口通过 Win32 UI Automation(AutoSetFocus = False + 原生 Pattern)与 PrintWindow 在后台读写、点击和截窗。

    • 网页与 Electron 应用通过独立隔离配置目录(profiles/)+ SW_SHOWMINNOACTIVE 后台最小化启动,走 Chrome DevTools Protocol(CDP)直接操控,不碰你日常浏览器,也不弹窗打断前台操作。

  • 超省 Token 的紧凑快照:

    • cdp_snapshot 默认只抓视口内可交互元素 + 精简单行格式,并支持 filter_text 过滤,输出体积相比整页 DOM/截屏减少约 90%。

    • cdp_action(action="fill", submit=True) 一条调用即可完成输入并提交,兼容 React/Vue 原生表单以及 contenteditable 富文本编辑器(自动切 Input.insertText)。

  • 穿透复杂嵌套页面:

    • 支持同源子 iframe、沙箱化跨进程 OOPIF(blob: / srcdoc,通过 Target.setAutoAttach(flatten=True) 递归附着)以及开放 Shadow DOM,统一分配 e1 / f<idx>_e<n> 引用。

  • 单窗极速前台兜底(fg_window_input):

    • 针对不暴露 UIA ValuePattern 也没有开 CDP 端口的富文本桌面软件(如常规启动的聊天客户端),可在约 0.3 秒内完成「聚焦目标窗 → 定点点击 → 剪贴板粘贴(自动保护并恢复原剪贴板)→ 回车 → 自动归还光标与原前台窗口」,无需扫描全桌面 UI 树。

  • 本地代理免疫:

    • 所有 127.0.0.1 的 CDP HTTP 与 WebSocket 通信均强制绕过系统/环境变量代理,避免本机开梯子导致 CDP 假死或 502。

Related MCP server: GhostHand-MCP

暴露的 12 个 MCP 工具

类别

工具名

用途

Windows 后台 UIA

bg_list_windows

枚举可见窗口(hwnd、进程名、标题、坐标、最小化/前台状态)

Windows 后台 UIA

bg_uia_inspect

后台读取单个窗口控件树与支持的 Pattern,支持 filter_text

Windows 后台 UIA

bg_uia_action

后台执行 invoke/set_value/get_value/toggle/select/expand/collapse/scroll

Windows 后台 UIA

bg_window_screenshot

后台 PrintWindow 截取被遮挡窗口,附空白帧检测

Windows 极速前台输入

fg_window_input

单窗瞬时激活、可选点击、Unicode 粘贴、组合键/回车,并自动归还光标、剪贴板与前窗

后台 CDP

cdp_launch_or_connect

复用现有 CDP 端口,或后台最小化拉起隔离 Chrome/Edge 或指定 Electron 程序(exe_path)

后台 CDP

cdp_list_tabs

列出指定端口的所有页面标签

后台 CDP

cdp_open_tab

打开新标签页(支持 background=True/False)

后台 CDP

cdp_close_tab

关闭指定标签页

后台 CDP

cdp_snapshot

获取紧凑视口元素列表(含 iframe/OOPIF/Shadow DOM 穿透)与页面文本

后台 CDP

cdp_action

执行 click/fill/press/scroll/navigate/eval_js,按 e* 或 f*_e* 自动路由到对应 frame

后台 CDP

cdp_screenshot

后台对指定标签页截图(不激活窗口、不抢焦点)

环境要求

  • Windows 10 / 11

  • Python 3.12(推荐使用 uv 创建独立虚拟环境;部分 Python 3.14 环境缺少 pywin32 等预编译 wheel)

  • 已安装 Chrome 或 Edge(如需使用 CDP 工具)

快速安装

git clone https://github.com/magua521/bg-control-mcp.git
cd bg-control-mcp

# 推荐用 uv 创建 Python 3.12 虚拟环境
uv venv --python 3.12 .venv
uv pip install --python .venv\Scripts\python.exe -r requirements.txt

# 跑一遍自动化冒烟测试(验证后台 UIA + 无头 Chrome CDP + MCP 12 工具握手)
.venv\Scripts\python.exe smoke_test.py

接入 MCP 客户端

把 <INSTALL_DIR> 替换成你实际 clone 的目录路径(建议用正斜杠 /)。

Antigravity / Claude Desktop / 通用 JSON (mcp_config.json)

{
  "mcpServers": {
    "bg-control": {
      "command": "<INSTALL_DIR>/.venv/Scripts/python.exe",
      "args": [
        "<INSTALL_DIR>/server.py"
      ],
      "env": {
        "PYTHONUTF8": "1",
        "NO_PROXY": "127.0.0.1,localhost"
      }
    }
  }
}

Hermes Agent (config.yaml)

mcp_servers:
  bg-control:
    command: <INSTALL_DIR>/.venv/Scripts/python.exe
    args:
      - <INSTALL_DIR>/server.py
    env:
      PYTHONUTF8: "1"
      NO_PROXY: "127.0.0.1,localhost"
    timeout: 120
    connect_timeout: 30
    enabled: true

注:Hermes 会为工具名加上 mcp__bg_control__ 前缀(例如 mcp__bg_control__cdp_snapshot)。

环境变量说明

变量名

是否必需

默认值

说明

PYTHONUTF8

推荐

1

防止 Windows 控制台/管道 GBK 编码乱码

NO_PROXY

推荐

127.0.0.1,localhost

确保本地回环通信不走外部代理

BG_CONTROL_PROFILES_DIR

可选

<项目目录>/profiles

隔离浏览器登录态目录(含 Cookie,已在 .gitignore 忽略)

BG_CONTROL_SCREENSHOTS_DIR

可选

<项目目录>/screenshots

截图默认输出目录

BG_CONTROL_PROXY

可选

自动探测本机 10808/7890/7897

当 cdp_launch_or_connect(proxy="auto") 时为隔离浏览器指定的代理地址;传 proxy="none" 可直连

实测避坑指南(改动时别踩回去)

  1. 为什么不用日常 Chrome 默认 Profile 开 CDP? 新版 Chrome 禁止对默认 User Data 目录开启 --remote-debugging-port;用 profiles/<browser>-<name> 独立目录登录一次即可持久化保存登录态,且完全不打扰日常浏览。

  2. Next.js / SPA 页面在后台打开一片空白? 某些前端框架在非激活标签页会延迟挂载组件;因为隔离 Chrome 窗口本身已最小化在后台,调用 cdp_open_tab(..., background=False) 只会在最小化窗口内部切为当前页,既能完整渲染,也不会弹窗抢焦点。

  3. Electron 桌面软件接入:可通过 cdp_launch_or_connect(exe_path="...", port=9223) 启动支持 --remote-debugging-port 的 Electron 应用并在后台走 CDP 操控;对于内部使用 contenteditable 的富文本输入框,cdp_action(action="fill") 会自动走 Input.insertText。

  4. 安全边界:本工具仅操作本机桌面与本地回环端口,不对外暴露公网服务;请勿将 profiles/ 提交到仓库,也不建议对有强风控封号机制的即时通讯软件做无人值守群发。

目录结构

bg-control-mcp/
├── server.py          # MCP 2.x 服务端入口(暴露 12 个工具)
├── bg_cdp.py          # 后台静默 CDP 引擎(含 OOPIF / Shadow DOM 穿透)
├── bg_uia.py          # Windows 后台 UIA + PrintWindow + fg_window_input 引擎
├── smoke_test.py      # 端到端冒烟测试
├── requirements.txt   # Python 依赖清单
├── .gitignore         # 严格排除 .venv / profiles / screenshots / __pycache__
└── LICENSE            # MIT 协议

License

MIT

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
      Not graded
      quality
      B
      maintenance
      Enables AI agents to observe and drive Windows desktop applications in the background through UI Automation and Win32 messages, without taking over the user's mouse, keyboard, or foreground window. Every action is read back and verified, and outward actions are held for explicit confirmation.
      6
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI clients to automate Windows desktop applications through protocol-first, background-safe window inspection, screenshots, clicks, typing, scrolling, browser CDP control, batch pipelines, and built-in safety guardrails without hijacking the physical mouse or focus.
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to automate Windows applications and Chrome/Edge browsers through UI Automation, browser CDP, OCR, vision, and fast screen capture, including clicking, typing, JavaScript execution, page extraction, and waiting for on-screen changes.
      1
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to inspect and control Windows desktop applications through compact UI Automation trees, dHash-gated screenshots, OCR, and guarded physical, virtual, or parallel input actions with emergency stop.
      MIT