Isolating AI from Your Real Browser: Building Edgebot MCP with Code-Enforced Guardrails
Why Give AI Agents Their Own Dedicated Browser
If you’re new to CDP (Chrome DevTools Protocol), think of it as a remote-control port built into Chromium browsers that lets scripts click, type, and read page content automatically. When connecting AI agents to authenticated sites like Facebook groups, the tempting shortcut is pointing them straight at your daily browser.
That shortcut comes with real risk: your everyday browser holds active personal cookies, banking sessions, and work tabs. Sharing a debugging port invites session bleed, port collisions across tools, and situations where a broad taskkill after a UI hang wipes out your real workspace.
To eliminate those risks cleanly, I built Edgebot MCP (edgebot-mcp)—a ~490-line custom MCP server in Python 3.11 (mcp 2.0 SDK + websocket-client) that talks directly to Microsoft Edge over CDP without heavy wrappers like browser-use, enforcing 4 layers of guardrails directly in code.
Three Failure Modes That Shaped the Guardrails
- Cross-Profile Session Bleed: Your daily browser (e.g., Brave on port
9222) stores personal logins and cookies. Letting an agent attach to that port puts personal sessions one wrong command away from exposure. - Debugging Port Collisions: When personal browsers, social readers, and scraping sandboxes coexist on one machine, relying on default debugging ports invites cross-tool interference.
- Race Conditions & Heavy-Handed Restarts: Concurrent tool calls racing on one tab corrupt DOM state, while frozen React UIs require a restart—where indiscriminate
taskkillcommands close the user’s real browser windows.
Inside the 4-Layer Guardrails Architecture
┌────────────────────────────────────────────────────────────────┐│ Edgebot Guardrails Architecture │├────────────────────────────────────────────────────────────────┤│ AI Agents (Hermes / MCP Clients) ││ │ (MCP stdio JSON-RPC + @functools.wraps) ││ ▼ ││ [Edgebot MCP Server] ─── Port 9333 (127.0.0.1 Loopback) ││ │ ││ ├─► 1. Port & Profile Isolation (~/.hermes-edge-profile) ││ ├─► 2. Identity Gate (Verify 'Edg/' in /json/version & UA)││ ├─► 3. Global Mutex Lock (threading.Lock serialization) ││ └─► 4. Scoped Toolset + Targeted PID Reset (netstat) ││ ││ Target Browser: Microsoft Edge (Dedicated Sandbox Instance) ││ (Isolated from Brave @ 9222 and Agent Reach @ 9444) │└────────────────────────────────────────────────────────────────┘1. Hard Port & Profile Boundaries
- Fixed port ownership across the host:
9222/9223stay untouched for personal Brave, Edgebot owns9333, and Agent Reach runs separately on headless port9444. - Edge launches with
--user-data-dir=~/.hermes-edge-profile, keeping cookies, local cache, and login state strictly isolated from the user’s main profiles.
2. Two-Layer Browser Identity Gate (Edg/)
Instead of blindly assuming port 9333 belongs to Edge, the server verifies browser identity twice:
endpoint_alive()querieshttp://127.0.0.1:9333/json/versionand checks that"Browser"containsEdg/._ensure_browser()evaluatesnavigator.userAgentviaRuntime.evaluateand aborts immediately ifEdg/is missing.
3. Global Mutex Lock Against Race Conditions
Every tool in TOOL_REGISTRY is wrapped via _locked(fn) using threading.Lock() and @functools.wraps. Navigation and extraction calls serialize cleanly over a single dedicated page WebSocket (_page_ws()), preventing concurrent tab collisions.
4. Scoped Toolset & Surgical PID Reset
- Read-Centric Core + Guarded Publish: Core tools focus on inspection and extraction (
health,launch_browser,reset_browser,check_login,navigate,read_page,expand_content,read_post,read_group_feed), whilepost_to_feedcheckslocation.hostname(facebook.com) and verifies composer text (after normalizingCRLF/LF) before clicking publish. - Targeted Reset (
reset_browser): Parsesnetstat -anofor the exact PIDLISTENINGon port9333, runstaskkill /PID <pid> /T /Fon that process tree alone, and relaunches the isolated Edge profile without touching any other browser window.
Reusing This Pattern in Your Own MCP Server (Step-by-Step + Master Prompt)
If you want an AI coding agent (such as Antigravity, Claude Code, Codex, or Cursor) to scaffold an isolated browser MCP server with these same safeguards, use this two-step blueprint:
Step 1: Lock Down Your Port & Profile Layout First
- Reserve a dedicated debugging port for automation (e.g.,
9333) and explicitly forbid touching your primary browser’s port (9222). - Create an isolated user data directory (e.g.,
~/.hermes-edge-profile) and sign into bot/test accounts only inside that profile. - Always launch modern Edge/Chromium with
--remote-allow-origins=*, or localhost WebSocket connections will be rejected with403.
Step 2: Master Prompt to Scaffold an Isolated Browser MCP
Build a Python MCP Server (using mcp 2.0 SDK + websocket-client) to automate Microsoft Edge directly over CDP with 4 mandatory code-enforced guardrails:
[Configuration]:- Dedicated CDP port: 9333 (never touch 9222/9223)- Isolated profile dir: ~/.hermes-edge-profile- Edge launch flags: --remote-debugging-port=9333 --user-data-dir=<profile_dir> --remote-allow-origins=*
[Required Guardrails]:1. Two-layer Identity Gate: - Check /json/version "Browser" field contains "Edg/". - Evaluate navigator.userAgent via Runtime.evaluate and abort if "Edg/" is missing.2. Concurrency Control: - Wrap every MCP tool with threading.Lock() using @functools.wraps (avoid raw **kwargs wrappers, which break mcp 2.0 schema inference).3. Scoped Tools & Surgical Reset: - Use MCPServer from mcp.server.mcpserver.server with add_tool(). - Implement reset_browser() using netstat -ano to locate only the PID LISTENING on port 9333, run taskkill /PID <pid> /T /F on that tree, and relaunch Edge.4. Navigation Drift Verification: - When extracting target posts, verify the permalink ID in location.href after dismissing popups (still_on_post) so silent redirects are caught before trusting extracted content.Real-World Pitfalls Solved in server.py
| Challenge | Operational Impact | Working Fix |
|---|---|---|
MCP 2.0 Removed FastMCP & **kwargs Broke Schemas | Wrapping handlers with **kwargs triggered Field required: kwargs during schema validation | Migrated to MCPServer (mcp.server.mcpserver.server) with add_tool() and @functools.wraps(fn) to preserve original function signatures. |
| Edge 111+ WebSocket Origin 403 | Localhost CDP WebSocket handshakes failed with WebSocketBadStatusException 403 | Passed --remote-allow-origins=* on Edge launch and suppress_origin=True to websocket.create_connection(). |
| Facebook Popup & Navigation Drift | Dismissing save-password popups could silently leave the target post, and Windows innerText exposed CRLF | Added still_on_post ID checks (_post_id_from), CRLF/LF normalization in post_to_feed, and reset_browser() for broken React UI states. |
hermes mcp add Stdio Flag Leak | Passing --connect-timeout leaked into server.py argv and crashed startup | Diagnosed via mcp-stderr.log and registered the stdio server without --connect-timeout. |
Verified Results
- Session Isolation: 0% session bleed between personal Brave (
9222) and dedicated Edge (9333/~/.hermes-edge-profile). - End-to-End MCP Check:
hermes mcp test edgebotconnected in 2,188 ms, and liveread_postruns returnedstill_on_post: Truewith fulldiv[role="article"]extraction (33 blocks in validation). - Safe Recovery:
reset_browser()cleanly restarts only the port-9333Edge process tree while leaving personal browsing sessions undisturbed.