fetch-ui-dom
README.md
# fetch-ui-dom: MCP Browser Bridge
`fetch-ui-dom` is a powerful bridge that connects LLMs (like Claude) to your live browser instance. It exposes your browser's DOM as a set of tools through the **Model Context Protocol (MCP)**, allowing AI agents to "see" and "reason" about the UI you are currently developing or using.
## π Why use this?
- **Zero Copy-Paste**: Stop manually inspecting and copying HTML into prompts.
- **Token Efficient**: Uses **DOM Tree Pruning Programming (DTPP)** to reduce DOM size by **25x+** while keeping it fully interactive for the AI.
- **Resilient Selectors**: Automatically identifies the best, most stable selectors (like `data-cy` or `aria-label`) so the AI suggests reliable automation code.
- **Self-Healing**: If a selector breaks, the tool can diagnose the failure and suggest the most likely replacement.
## π How it Works
1. **CDP Connection**: The tool connects to Chrome via the **Chrome DevTools Protocol (CDP)** on port `9222`.
2. **DOM Capture**: It identifies the active tab and captures the full DOM, including content inside **Shadow Roots** and **iframes**.
3. **Pruning**: It recursively traverses the DOM, stripping away non-essential tags (`<script>`, `<style>`, etc.) and non-functional attributes to minimize token usage.
4. **MCP Interface**: It runs as a local server that your AI client (Claude Desktop, etc.) can talk to.
## π Prerequisites
- **Node.js**: v20.x or higher.
- **Chrome**: Must be launched with remote debugging enabled.
## βοΈ Implementation & Setup
### 1. Enable Remote Debugging in Chrome (Reliable Method)
Chrome only allows the debugging port on the **very first** process started for a given profile. If "background apps" are running or a ghost process exists, the flag will be silently ignored.
#### 1.1 The "Clean Start" Method (Command-line)
1. Close all Chrome windows.
2. Run this command in your terminal (Windows PowerShell):
```powershell
# Kill ghost processes and start a fresh instance with a dedicated profile
Get-Process chrome -ErrorAction SilentlyContinue | Stop-Process -Force; `
start chrome "--remote-debugging-port=9222 --user-data-dir=$env:TEMP\chrome-debug"
```
#### 1.2 Create a Dedicated "Dev Shortcut"
Instead of using your main profile (which has all your extensions and history), create a shortcut specifically for debugging.
1. Right-click on your Desktop > New > Shortcut.
2. In the "location" box, paste this (adjusting the path if Chrome is installed elsewhere):
`"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\chrome-dev-profile"`
3. Name it "Chrome Debug".
* Using a dedicated `--user-data-dir` ensures this instance always starts fresh and never conflicts with your main browser window.
#### 1.3 Create a "Reset" Script
If things still get stuck, keep this one-liner handy as a `.ps1` (PowerShell) file on your desktop (or use the provided `scripts/kill-chrome.ps1` in this repo):
```powershell
# kill-chrome.ps1
Get-Process chrome -ErrorAction SilentlyContinue | Stop-Process -Force
Write-Host "All Chrome instances killed. You can now start with the debug flag." -ForegroundColor Green
```
**Summary of the Process:**
The Remote Debugging Port is a security-sensitive feature. Chrome only allows it to be enabled on the very first process that starts for a given user-data-dir. By using a dedicated profile folder, you guarantee that your debugging instance is always the "first" process, making the connection 100% reliable.
**Verification:**
Run this to ensure Chrome is actually listening on the port:
```powershell
Get-NetTCPConnection -LocalPort 9222 -ErrorAction SilentlyContinue
```
*If you see a row with state `Listen`, the connection is active.*
### 2. Install the Bridge
Clone this repository and install the dependencies:
```bash
npm install
npm run build
```
### 3. Connect to your AI Client (e.g., Claude Desktop)
Add the following to your `claude_desktop_config.json`. **Note:** Use the absolute path to the `dist` folder.
```json
{
"mcpServers": {
"browser-bridge": {
"command": "node",
"args": ["C:/Users/91897/Desktop/AI/AI Projects/fetch-ui-dom/dist/server/index.js"],
"env": {
"CYPRESS_DEBUG_PORT": "9222"
}
}
}
}
```
## β οΈ Reliability & Troubleshooting
### Why is my connection refused?
Chrome is a "single-instance" application. If any hidden background process (like an extension, updater, or "zombie" tab) is running, any new command with the `--remote-debugging-port` flag is ignored because the existing instance "hijacks" the request.
### How to fix it permanently:
1. **Disable Background Apps**: Go to Chrome `Settings > System` and turn **OFF** *"Continue running background apps when Google Chrome is closed"*. This ensures that closing the window actually kills the process.
2. **Use Dedicated Profiles**: Always use the `--user-data-dir` flag when debugging to separate your "Dev" browser from your main browser profile. This avoids conflicts and makes the connection 100% reliable.
## π§° Tools Provided
| Tool | Description |
| --- | --- |
| `fetch_dom` | Captures the pruned, token-optimized DOM of the active tab. |
| `find_element` | Locates specific elements using the resilient selector hierarchy. |
| `diagnose_failure` | Analyzes a broken selector and suggests the correct one based on similarity. |
## π§ͺ Testing with CLI
Before using it with an AI client, you can test the logic directly:
- **Fetch active DOM**: `npx tsx src/cli/fetch-cli.ts`
- **Prune local file**: `npx tsx src/cli/prune-cli.ts input.html`
- **Analyze selector**: `npx tsx src/cli/select-cli.ts input.html ".my-button"`
## π License
ISC
This server cannot be deployed
Maintenance
ActivityInactive
ResponsivenessNo recent community issues