# browser-for-ai (bfa) ![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg) ![Node](https://img.shields.io/badge/node-%E2%89%A520-brightgreen.svg) ![MCP](https://img.shields.io/badge/MCP-server-8A2BE2.svg) ![TypeScript](https://img.shields.io/badge/TypeScript-strict-3178C6.svg) ![Tools](https://img.shields.io/badge/tools-52-0a8fa6.svg) **English** · [ภาษาไทย](README.th.md) A **CDP-native MCP server** that lets an AI agent (Claude Code and any other MCP client) drive a real Chrome at full depth — reading the network and console the way a human does with DevTools open, operating the page robustly, and **reverse-engineering a site's API flow into runnable code**. --- ## Why bfa The things a screenshot-only browser tool can't do: - **⭐ Turn a real session into runnable code.** Mark a flow, perform it in the browser, and bfa **synthesizes replay code** (curl / TypeScript / Go / Python) with cross-call **dependencies chained automatically** — an auth token from one response becomes a variable the next request re-uses, not a baked-in literal. `flow_replay` then runs it for real to prove the reversal reproduces. - **🔬 See the whole network.** Full request/response **bodies** (text *and* binary base64), the **complete on-the-wire headers** (`Cookie` and custom signing headers included, merged from CDP ExtraInfo), timing, redirect hops, and WebSocket frames — surfaced by the exact question you're asking: `failures`, `pending` (hangs), `slow`. - **🎮 Drive anything — by ref, by sight, or by pixel.** Ref / CSS interaction, raw **coordinate + touch** for `` / WebGL, and **see-then-click**: `page_look` returns a screenshot with a numbered badge on every clickable element (Set-of-Mark), 1:1 with CSS px, so the model reads the picture and clicks by number — no coordinate math, no misclicks. Every action reports the network / console / URL **delta** it caused. - **🧪 Shape traffic.** Block / mock / modify requests, and **throttle** to Slow 3G / offline / custom bandwidth with CPU slowdown. - **🗂️ Real sessions.** Many concurrent sessions, incognito, **attach to your logged-in Chrome**, save/restore cookies + storage, and complete cache clearing. - **🎯 Drives the right tab, on a viewport that stays put — game after game.** The session you launch is the active one and every launch loads the URL you gave (it never silently reuses an earlier game's tab); bfa **auto-follows a tab the page opens** and **self-heals** a detached / closed / chrome-error tab (actions re-attach and retry; `browser_recover` re-attaches a stale frame or hands you a fresh tab) instead of forcing a relaunch. Launch with `device:"mobile"` for a **stable 390×844 phone viewport** (touch on, so `page_tap_at` and `page_batch {action:"tap_at"}` drive Cocos/canvas games) that will not self-shrink, so coordinates stay accurate across a whole 12-game batch. - **⚡ Fast, and it never hangs.** `page_batch` runs a whole sequence in one round-trip and can end with a look; actions settle on *quiet* instead of fixed sleeps; `page_wait_for` / `net_wait` return the moment a condition holds. Every call is time-bounded: a runaway script is terminated (`page_eval` budget, `browser_recover`), `browser_close` force-kills an owned Chrome that won't exit, and the recorder is ring-bounded for day-long sessions. ### How it compares | Capability | bfa | typical browser MCPs | |---|:---:|:---:| | Reverse a captured flow → runnable code, dependency-chained + replay-verified | ✅ | ✗ (at most Playwright-script codegen from UI actions) | | Full response **bodies** (text + binary) & WebSocket frames, on by default | ✅ | mostly metadata only | | Secret **redaction** in the emitted code | ✅ | ✗ | | Coordinate **+ touch** interaction for canvas / WebGL | ✅ | some (vision mode) | | Attach to your **logged-in** Chrome | ✅ | ✅ (common) | | Network / CPU throttling presets | ✅ | some | | Multi-step batch + see-the-result in ONE call; bounded calls, runaway-script recovery | ✅ | rare | | Cloud-scaled browsers · stealth · proxies · CAPTCHA | ✗ *(local by design)* | some cloud tools | bfa is a **local, developer-facing inspection & reverse-engineering** tool, not a cloud scraping farm — that focus is why the first three rows are rare elsewhere. --- ## Requirements - **Node.js ≥ 20** - **Google Chrome** installed (or set `BFA_CHROME_PATH` to your Chrome binary) ## Install Published on npm as **[`browser-for-ai`](https://www.npmjs.com/package/browser-for-ai)** — no clone or build required. ```bash # zero-install (recommended) — pulls the latest and runs on demand npx -y browser-for-ai # …or install globally, exposing a `browser-for-ai` command npm install -g browser-for-ai ```
Build from source instead ```bash git clone https://github.com/icueth/browser-for-ai.git cd browser-for-ai npm install npm run build # → dist/server.js ```
## Register with an MCP client **Claude Code** — via the published package (no path needed): ```bash claude mcp add browser-for-ai --scope user -- npx -y browser-for-ai ``` Verify with `claude mcp get browser-for-ai` (should say **✔ Connected**). Tools load into a **new** session, so start a fresh Claude Code session afterward. **AgentSpace** ships `browser-for-ai` as a **default** MCP server (`npx -y browser-for-ai`) — it appears under **Settings → MCP / Integrations** out of the box. **Any MCP client** (raw stdio config): ```jsonc { "command": "npx", "args": ["-y", "browser-for-ai"] } ```
Register a local build instead of the npm package ```bash claude mcp add browser-for-ai --scope user -- node /absolute/path/to/browser-for-ai/dist/server.js ``` > If `node` comes from a version manager (nvm, asdf, …), pass the **absolute** path to the node binary — the MCP server is spawned by a non-interactive shell that won't resolve aliases.
--- ## Quick start ```jsonc browser_launch { "mode": "fresh", "url": "https://example.com" } // real window page_screenshot net_list // recent requests net_failures // anything that errored net_pending // anything still hanging page_snapshot // ref-annotated DOM page_click { "selector": "#login" } net_get { "url": "/api/login" } // one call in full: headers + bodies browser_close { "all": true } ``` --- ## Sessions `browser_launch { mode, url?, port?, profile?, incognito?, headless?, viewport? }` - **`fresh`** — launch our own Chrome (headful by default; `headless: true` for none). - **`attach`** — connect to a Chrome started with `--remote-debugging-port` (only `port` is used; default `9222`). - **`incognito: true`** — isolated context, no prior state. - **Profiles.** No `profile` → ephemeral temp profile wiped on close. A named `{ "profile": "work" }` persists under `~/.bfa/profiles/work` so logins survive. Two concurrent sessions on the *same named* profile collide; unnamed ones are always safe. - **`device: "mobile"`** — a stable 390×844 phone viewport (dpr 3, mobile layout + UA) that does **not** track the OS window, so it never self-shrinks and click coordinates stay put. Use it for phone / PG-style games. `device: "desktop"` is 1280×800. An explicit `viewport` overrides a preset; with neither, the page tracks the real window (which can resize). - **Viewport** at launch, or `page_set_viewport { device }` / `{ width, height }` on a live session. **One Chrome per job.** `browser_launch` **reuses** a matching open session (same incognito / profile / headless) and just navigates it to `url` — it does not start another Chrome; pass `new: true` to force a second browser. Owned sessions that sit idle auto-close after **`BFA_IDLE_MINUTES`** (default 20; 0 = never), at most **`BFA_MAX_SESSIONS`** (default 3; 0 = unlimited) stay open (the least-recently-used is evicted), `browser_sessions` shows each one's idle time, and a new server start kills Chromes orphaned by a bfa that died. Attach sessions (your own Chrome) are never reaped. Still: `browser_close { all: true }` when a job is done. The session you launch becomes the **active** one; a second launch makes that one active. In fresh mode bfa **auto-follows a tab the page opens** and **self-heals** to another live tab if the driven one closes. Manage with `browser_sessions`, `browser_use { sessionId }`, `browser_tabs`, `browser_use_tab { index }`, `browser_close`. Most tools accept an optional `sessionId`; without it they target the active session. --- ## Tool reference (52) ### Sessions & lifecycle | tool | purpose | |---|---| | `browser_launch` | launch fresh / attach a session | | `browser_sessions` | list open sessions | | `browser_use` | set the default session | | `browser_tabs` | list a session's tabs/targets | | `browser_use_tab` | switch the driven tab (carries recorder + intercept rules; bfa auto-follows page-opened tabs in fresh **and attach** mode) | | `browser_close_tab` | close one tab by index without closing the session (heals to a live tab if it was the driven one) | | `browser_close` | close one session, or `all` | | `browser_clear_cache` | clear cache + cookies + storage | | `browser_hard_reload` | bypass-cache reload | | `browser_recover` | unfreeze a page whose JS is pinned (terminate script → scripts off → still readable/closable) | ### Navigation, state & read | tool | purpose | |---|---| | `page_goto` | navigate to a URL | | `page_state` | url, title, readyState, viewport | | `page_set_viewport` | resize a live session's viewport | | `page_snapshot` | compact, ref-annotated DOM (source of element **refs**) | | `page_find` | find element(s) by text / ARIA role / CSS → refs (targeted vs snapshot) | | `page_read` | read/search the page's **text content** (optionally by selector + query) | | `page_look` | **see-then-click**: 1:1 screenshot with numbered badges on every clickable element + legend → `page_click {ref}` | | `page_wait_for` | wait until a selector / text / URL / network-idle condition holds (instead of sleeping) | | `page_observe` | delta since last observe — new console/network/URL/DOM | | `page_screenshot` | PNG of viewport, full page, or one element | | `page_eval` | evaluate JS in the page → value; opt-in `delta:true` (network/console side-effects) + `screenshot:true` (viewport image) in one call | ### Interaction | tool | purpose | |---|---| | `page_click` | click a ref / selector (reports the delta) | | `page_type` | type into a field (`clear:true` to replace) | | `page_fill` | fill several fields in one call | | `page_select` | choose an `