Skip to main content
Glama
README.md
# cua-ag-bridge

A lightweight Windows stdio MCP proxy over [`cua-driver`](https://github.com/trycua/cua) built specifically to eliminate desktop-automation latency bottlenecks in **Google Antigravity** (and other MCP hosts with small inline payload caps or missing `structuredContent` rendering).

---

## Why This Exists

Profiling real multi-step Windows desktop automation sessions (e.g., driving `wxWidgets` / `wxWindowNR` apps like Bambu Studio on high-DPI displays) revealed that **>90% of wall-clock latency is not `cua-driver` execution—it is avoidable LLM round-trips** caused by four integration gaps:

1. **Antigravity's ~10 KB MCP Inline Payload Cap:** Any MCP `ImageContent` > 10,000 bytes is offloaded to `media_0.png`, forcing a mandatory second LLM turn (`read_file`) every time the agent looks at the screen.
2. **Stripped MCP `structuredContent`:** `cua-driver` places `elements[].screenshot_frame`, `capture_id`, `window_bounds`, cursor state, and action metadata in MCP 2025-06-18 `structuredContent`, which Antigravity drops—leaving `get_window_state({tree_format: "elements"})` returning only a placeholder string.
3. **Custom-Toolkit Blind Spots (`wxWindowNR` / Qt / CAD):** Custom buttons show up as unnamed UIA nodes (`Button [id=...]`), silently drop background `PostMessage` clicks, and lose transient `WS_POPUP` dropdowns when `click({delivery_mode: "foreground"})` immediately restores the prior foreground window (and per-HWND `PrintWindow` omits sibling `WS_POPUP` overlays).
4. **Default 600 ms Cursor Glide Floor (`steady`):** `cua-driver`'s default `steady` cursor timing adds ~750 ms per pointer move; switching to `fixed` 50 ms glide cuts cursor overhead **6×** (~125 ms/move).

---

## Architecture

```mermaid
flowchart LR
    AG["Google Antigravity (10 KB inline cap, drops structuredContent)"] <-->|stdio MCP| BR["cua-ag-bridge (bridge.py)"]
    BR <-->|single shared stdio runtime| CD["cua-driver.exe mcp --cursor-reduced-motion on"]
    BR <-->|resident ~80ms warm| OCR["Windows WinRT OCR (ocr_worker.ps1)"]
    BR <-->|Pillow <= 9.6 KB| IMG["Sub-10KB Inline JPEG Tile Encoder"]
```

Because `cua-ag-bridge` wraps a **single shared `cua-driver.exe mcp` child process**, all 60+ native `cua-driver` tools and the 3 bridge super-tools share the exact same sessions, `snapshot_id`s, `element_token`s, `capture_id`s, and `zoom` contexts—with zero duplicate tools or orphan daemons.

---

## Features & Bridge Super-Tools

### Transparent Improvements on All `cua-driver` Tools
- **Automatic `structuredContent` Flattening:** Renders `elements[].screenshot_frame` centroids `@(cx,cy)`, `capture_id`, `window_bounds`, cursor state, and action `route`/`effect`/`escalation`/`next_call` metadata directly into plain MCP text.
- **Auto-Fast Cursor Motion:** Automatically injects `{timing: "fixed", glide_duration_ms: 50, dwell_after_click_ms: 0}` into `start_session` calls when `cursor_motion` is omitted (disable with `CUA_BRIDGE_FAST_CURSOR=0`).
- **Fast Path Enabled by Default:** Seeds `CUA_DRIVER_FAST_PATH=on` and `--cursor-reduced-motion on` on the child `cua-driver` process.

### 3 Added Super-Tools

| Tool | Purpose | Turns / Latency |
| :--- | :--- | :--- |
| **`inspect_window_hybrid`** | Merges `cua-driver` UIA elements (with `screenshot_frame` centers) + Windows built-in **WinRT OCR** (`Windows.Media.Ocr`, zero model downloads, zero AGPL weights) into a single `<9 KB` inline text table. Labels unnamed `wxWindowNR` buttons with their visible OCR text and exact `(cx, cy)` coordinates. | **1 turn** (~220 ms total; **~80 ms** warm OCR) |
| **`peek_region`** | Crops `[x1, y1, x2, y2]` from the window capture, draws crisp window-local coordinate ticks along the top/left edges, and encodes as JPEG strictly **`<= 9,600 bytes`** so Antigravity always inlines it directly as `part_0`. | **1 turn** (~40 ms, zero `read_file`) |
| **`foreground_tx`** | Calls `bring_to_front` once (preventing `WM_KILLFOCUS` dropdown auto-dismissal), runs `run_steps({delivery_mode: "foreground"})`, and optionally returns a `diff`, `hybrid` table, or **`desktop_crop`** (`<= 9.6 KB` inline JPEG of the desktop composite that captures `WS_POPUP` dropdowns missed by `PrintWindow`). | **1 turn** atomic foreground batch |

---

## Quick Start (Windows)

### Prerequisites
- Windows 10 / 11 (uses built-in `Windows.Media.Ocr` via Windows PowerShell 5.1)
- Python 3.9+ with Pillow:
  ```powershell
  pip install -r requirements.txt
  ```
- [`cua-driver`](https://cua.ai) installed (`cua-driver.exe` on `PATH` or in `%LOCALAPPDATA%\Programs\Cua\cua-driver\bin\`)

### 1. Clone & Verify
```powershell
git clone https://github.com/saverchenkov/cua-ag-bridge.git
cd cua-ag-bridge
pip install -r requirements.txt
python test_bridge.py
```

### 2. Configure Google Antigravity (`~/.gemini/config/mcp_config.json`)
Replace (or add) your `"cua-driver"` entry:

```json
{
  "mcpServers": {
    "cua-driver": {
      "command": "python",
      "args": [
        "C:\\path\\to\\cua-ag-bridge\\bridge.py"
      ],
      "env": {
        "PYTHONUTF8": "1",
        "CUA_DRIVER_FAST_PATH": "on",
        "CUA_DRIVER_ARGS": "mcp --cursor-reduced-motion on",
        "CUA_BRIDGE_FAST_CURSOR": "1"
      }
    }
  }
}
```
*(Tip: Use the full path to `python.exe`, e.g. `C:\\Python\\Python39\\python.exe`, and optionally set `CUA_DRIVER_EXE` if `cua-driver.exe` is in a custom location.)*

### 3. Install the Recommended Agent Rule (Optional)
Copy [`rules/cua-driver-antigravity.md`](rules/cua-driver-antigravity.md) into `~/.agents/rules/` so your agents default to `run_steps`, `inspect_window_hybrid`, `peek_region`, and `foreground_tx`.

Also note: if `cua-driver skills install` created a Windows symlink at `~/.agents/skills/cua-driver`, replacing that symlink with a real directory copy ensures Antigravity discovers the bundled `cua-driver` skill docs.

---

## Environment Variables

| Variable | Default | Description |
| :--- | :--- | :--- |
| `CUA_DRIVER_EXE` | Auto-discovered (`PATH` / `%LOCALAPPDATA%`) | Explicit path to `cua-driver.exe` |
| `CUA_DRIVER_ARGS` | `mcp --cursor-reduced-motion on` | Space-separated args passed to `cua-driver.exe` |
| `CUA_DRIVER_FAST_PATH` | `on` | Passed through to `cua-driver.exe` |
| `CUA_BRIDGE_FAST_CURSOR` | `1` | Set to `0` to disable automatic 50 ms `start_session` cursor motion injection |

---

## Rollback

To bypass `cua-ag-bridge` and run `cua-driver.exe` directly, point `"cua-driver"` in `~/.gemini/config/mcp_config.json` back to `cua-driver.exe`:

```json
"cua-driver": {
  "command": "cua-driver.exe",
  "args": ["mcp", "--cursor-reduced-motion", "on"],
  "env": { "CUA_DRIVER_FAST_PATH": "on" }
}
```

## License

[MIT](LICENSE)