144 lines
5.8 KiB
Markdown
144 lines
5.8 KiB
Markdown
# Firefox MCP Bridge
|
|
|
|
An MCP (Model Context Protocol) server that lets an LLM control a local Firefox instance through a browser extension. Uses a **Bridge Architecture** — no remote debugging, no automation flags — all interactions go through a Firefox extension's content scripts to mimic human behavior.
|
|
|
|
## Architecture
|
|
|
|
```
|
|
LLM (MCP Client)
|
|
│ SSE + HTTP POST (port 8008)
|
|
▼
|
|
Python Bridge (FastAPI/Starlette)
|
|
│ WebSocket (ws://127.0.0.1:8008/ws)
|
|
▼
|
|
Firefox Extension (Background Script)
|
|
│ browser.tabs.sendMessage
|
|
▼
|
|
Content Script (DOM interaction in web pages)
|
|
```
|
|
|
|
## Directory Layout
|
|
|
|
```
|
|
mcp-firefox-extension/
|
|
├── bridge/ # Python MCP server
|
|
│ ├── main.py # Starlette app (SSE + WebSocket)
|
|
│ ├── mcp_logic.py # MCP protocol + extension correlation
|
|
│ ├── config.py # Configuration (port 8008, etc.)
|
|
│ ├── requirements.txt # Python deps
|
|
│ └── tools/ # MCP tool handlers
|
|
│ ├── __init__.py # Tool registry
|
|
│ ├── utils.py # Shared helpers
|
|
│ ├── page_summary.py
|
|
│ ├── element_details.py
|
|
│ ├── click_element.py
|
|
│ ├── type_text.py
|
|
│ ├── press_key.py
|
|
│ ├── scroll_page.py
|
|
│ ├── execute_script.py
|
|
│ ├── take_screenshot.py
|
|
│ ├── read_page_text.py
|
|
│ ├── list_tabs.py
|
|
│ ├── switch_tab.py
|
|
│ ├── navigate_to.py
|
|
│ ├── open_new_tab.py
|
|
│ ├── get_current_url.py
|
|
│ └── get_page_cookies.py
|
|
├── extension/ # Firefox WebExtension
|
|
│ ├── manifest.json
|
|
│ ├── background.js # WebSocket client + browser.* APIs
|
|
│ └── content.js # DOM scanning + interaction
|
|
└── README.md
|
|
```
|
|
|
|
## Setup
|
|
|
|
### 1. Create a Python virtual environment
|
|
|
|
Create a venv if needed:
|
|
|
|
```bash
|
|
python3 -m venv bridge/venv
|
|
source bridge/venv/bin/activate
|
|
pip install -r bridge/requirements.txt
|
|
```
|
|
|
|
### 2. Start the Bridge server
|
|
|
|
```bash
|
|
cd bridge
|
|
source venv/bin/activate # if not already activated
|
|
python main.py
|
|
```
|
|
|
|
The server will listen on `http://127.0.0.1:8008` and expose:
|
|
- `GET /sse` — MCP SSE endpoint
|
|
- `POST /messages` — MCP message endpoint
|
|
- `WS /ws` — WebSocket for the Firefox extension
|
|
|
|
### 3. Load the Firefox Extension
|
|
|
|
1. Open Firefox and go to `about:debugging`
|
|
2. Click **"This Firefox"** in the left sidebar
|
|
3. Click **"Load Temporary Add-on..."**
|
|
4. Select the `extension/manifest.json` file from this project
|
|
5. The extension will attempt to connect to `ws://127.0.0.1:8008/ws` automatically
|
|
|
|
You should see "Firefox extension connected via WebSocket" in the bridge server logs.
|
|
|
|
> **Note:** Temporary add-ons are cleared when Firefox restarts. You'll need to reload the add-on via `about:debugging` after each browser restart.
|
|
|
|
### 4. Connect your LLM client
|
|
|
|
Point your MCP client at the SSE endpoint:
|
|
- SSE URL: `http://127.0.0.1:8008/sse`
|
|
- Message URL: `http://127.0.0.1:8008/messages?session_id=<session-id>`
|
|
|
|
The session ID is provided in the first SSE event (`event: endpoint`).
|
|
|
|
## Available MCP Tools
|
|
|
|
| Tool | Description |
|
|
|------|-------------|
|
|
| `get_page_summary` | Scans the page and indexes interactive elements (buttons, links, inputs, etc.) with `data-mcp-id` attributes |
|
|
| `get_element_details(id)` | Returns detailed attributes, HTML, bounding box, and computed style for an element |
|
|
| `click_element(id)` | Clicks the element with the given mcp-id |
|
|
| `type_text(id, text)` | Focuses an element and types text into it |
|
|
| `press_key(key)` | Simulates a keyboard key press (Enter, Escape, Tab, etc.) |
|
|
| `scroll_page(direction, amount)` | Scrolls up/down/left/right by pixels or percentage |
|
|
| `execute_script(js_code)` | Executes arbitrary JavaScript in the page (God Mode) |
|
|
| `take_screenshot()` | Captures a PNG screenshot of the viewport |
|
|
| `read_page_text()` | Returns reader-mode text content of the page |
|
|
| `list_tabs()` | Lists all open Firefox tabs |
|
|
| `switch_tab(tab_id, window_id)` | Switches to a specific tab or window |
|
|
| `navigate_to(url)` | Navigates the active tab to a URL |
|
|
| `open_new_tab(url, window_id)` | Opens a new tab in a specific window |
|
|
| `open_new_window(url)` | Opens a new Firefox window |
|
|
| `get_current_url()` | Returns the current active tab's URL |
|
|
| `get_page_cookies()` | Returns cookies for the current domain |
|
|
|
|
## How Element Indexing Works
|
|
|
|
When `get_page_summary` is called, the content script:
|
|
1. Queries all interactive elements (`button`, `a`, `input`, `select`, `textarea`, elements with `role` attributes, `onclick` handlers, or `contenteditable`)
|
|
2. Assigns each a unique `data-mcp-id` attribute (e.g., `data-mcp-id="1"`)
|
|
3. Returns a list with id, role, visible text, tag name, and other attributes
|
|
|
|
All subsequent interaction tools reference elements by this `mcp-id`, so you never need fragile CSS selectors.
|
|
|
|
## Stealth
|
|
|
|
- No `remote-debugging-port` is used
|
|
- No `automation` flags are set
|
|
- All DOM interactions are triggered by the extension's content scripts, which run in the same context as normal page scripts
|
|
- The extension appears as a normal Firefox add-on
|
|
|
|
## Troubleshooting
|
|
|
|
| Issue | Solution |
|
|
|-------|----------|
|
|
| "Firefox extension is not connected" | Make sure the bridge is running (`python main.py`) and the extension is loaded via `about:debugging`. Check the bridge logs for WebSocket connection messages. |
|
|
| "Content script not available in this tab" | Refresh the page or navigate to a different URL. Content scripts are injected on page load. |
|
|
| Extension won't load | Make sure you're using Firefox (not Chrome). Manifest V2 is used for better Firefox compatibility. |
|
|
| `port 8008 already in use` | Change `config.PORT` in `bridge/config.py` to another free port. |
|