Isolating AI from Your Real Browser: Building Edgebot MCP with Code-Enforced Guardrails

2026-11-13
--
--

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#

  1. 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.
  2. Debugging Port Collisions: When personal browsers, social readers, and scraping sandboxes coexist on one machine, relying on default debugging ports invites cross-tool interference.
  3. Race Conditions & Heavy-Handed Restarts: Concurrent tool calls racing on one tab corrupt DOM state, while frozen React UIs require a restart—where indiscriminate taskkill commands 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/9223 stay untouched for personal Brave, Edgebot owns 9333, and Agent Reach runs separately on headless port 9444.
  • 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() queries http://127.0.0.1:9333/json/version and checks that "Browser" contains Edg/.
  • _ensure_browser() evaluates navigator.userAgent via Runtime.evaluate and aborts immediately if Edg/ 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), while post_to_feed checks location.hostname (facebook.com) and verifies composer text (after normalizing CRLF/LF) before clicking publish.
  • Targeted Reset (reset_browser): Parses netstat -ano for the exact PID LISTENING on port 9333, runs taskkill /PID <pid> /T /F on 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#

  1. Reserve a dedicated debugging port for automation (e.g., 9333) and explicitly forbid touching your primary browser’s port (9222).
  2. Create an isolated user data directory (e.g., ~/.hermes-edge-profile) and sign into bot/test accounts only inside that profile.
  3. Always launch modern Edge/Chromium with --remote-allow-origins=*, or localhost WebSocket connections will be rejected with 403.

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#

ChallengeOperational ImpactWorking Fix
MCP 2.0 Removed FastMCP & **kwargs Broke SchemasWrapping handlers with **kwargs triggered Field required: kwargs during schema validationMigrated to MCPServer (mcp.server.mcpserver.server) with add_tool() and @functools.wraps(fn) to preserve original function signatures.
Edge 111+ WebSocket Origin 403Localhost CDP WebSocket handshakes failed with WebSocketBadStatusException 403Passed --remote-allow-origins=* on Edge launch and suppress_origin=True to websocket.create_connection().
Facebook Popup & Navigation DriftDismissing save-password popups could silently leave the target post, and Windows innerText exposed CRLFAdded 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 LeakPassing --connect-timeout leaked into server.py argv and crashed startupDiagnosed 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 edgebot connected in 2,188 ms, and live read_post runs returned still_on_post: True with full div[role="article"] extraction (33 blocks in validation).
  • Safe Recovery: reset_browser() cleanly restarts only the port-9333 Edge process tree while leaving personal browsing sessions undisturbed.
Isolating AI from Your Real Browser: Building Edgebot MCP with Code-Enforced Guardrails
https://www.chinnakrit.dev/posts/edgebot-browser-guardrails/
Author
chinnakrit
Published
2026-11-13
License
CC BY-NC-SA 4.0
© 2026 chinnakrit
RSS / Sitemap
Powered by Astro & Fuwari
© 2026 chinnakrit
RSS / Sitemap
Powered by Astro & Fuwari