|
| 1 | +# Playwright MCP: Headed vs Headless |
| 2 | + |
| 3 | +Cross-harness runbook for deterministic browser mode selection in AI-first CLI workflows. |
| 4 | + |
| 5 | +## Why This Exists |
| 6 | + |
| 7 | +In CLI harnesses, Playwright MCP mode can appear to "mysteriously" switch to headless because: |
| 8 | +- Harness-level config differs from shell-level env. |
| 9 | +- MCP subprocesses inherit only what the harness passes. |
| 10 | +- Display stack prerequisites are missing even when config requests headed mode. |
| 11 | + |
| 12 | +This runbook makes headed and headless behavior predictable across: |
| 13 | +- Claude Code |
| 14 | +- OpenAI Codex |
| 15 | +- GitHub Copilot CLI |
| 16 | + |
| 17 | +## Mode Selection |
| 18 | + |
| 19 | +Choose `headed` when you need: |
| 20 | +- Login and MFA flows. |
| 21 | +- Live demos and walkthroughs. |
| 22 | +- Visual verification of UI states. |
| 23 | +- Debugging anti-bot/captcha interactions. |
| 24 | + |
| 25 | +Choose `headless` when you need: |
| 26 | +- CI or batch automation. |
| 27 | +- Non-interactive extraction or checks. |
| 28 | +- Faster, repeatable automation without UI. |
| 29 | + |
| 30 | +## Common Failure Signatures |
| 31 | + |
| 32 | +- Launch error about missing X server or `$DISPLAY`. |
| 33 | +- Session dies with transport-closed style errors. |
| 34 | +- Login appears successful but follow-up page is unauthenticated. |
| 35 | +- Browser window never appears despite headed intent. |
| 36 | + |
| 37 | +## Deterministic Diagnostics |
| 38 | + |
| 39 | +Run this sequence in order: |
| 40 | + |
| 41 | +1. Verify harness config source of truth. |
| 42 | + - Confirm the active config file for your harness and session. |
| 43 | +2. Verify MCP subprocess environment. |
| 44 | + - Ensure headed prerequisites are passed in harness config env, not only in shell. |
| 45 | +3. Verify display stack prerequisites. |
| 46 | + - Linux/X11: `DISPLAY` and `XAUTHORITY` must be valid for the session user. |
| 47 | + - Wayland environments may require Xwayland compatibility for specific setups. |
| 48 | +4. Verify restart boundary. |
| 49 | + - If config changed, restart the harness session so MCP launches with new env. |
| 50 | +5. Verify with a neutral authenticated-page check. |
| 51 | + - Confirm post-login access to a known authenticated route/view, not just a login redirect. |
| 52 | + |
| 53 | +## Cross-Harness Notes |
| 54 | + |
| 55 | +### Claude Code |
| 56 | +- Keep MCP server config aligned with `mcp/mcp.json` and enabled server settings. |
| 57 | +- If MCP env changes, restart the CLI session to relaunch subprocesses. |
| 58 | + |
| 59 | +### OpenAI Codex |
| 60 | +- Configure Playwright MCP env under `.codex/config.toml` `[mcp_servers.playwright]`. |
| 61 | +- Treat shell exports as insufficient unless reflected in Codex MCP server env. |
| 62 | + |
| 63 | +### GitHub Copilot CLI |
| 64 | +- Use the CLI's MCP config/env mechanism as the runtime source of truth. |
| 65 | +- Ensure session restarts occur after env/config updates. |
| 66 | + |
| 67 | +## Recovery Playbook |
| 68 | + |
| 69 | +Use smallest-safe-change-first: |
| 70 | + |
| 71 | +1. Confirm intended mode for task (`headed` or `headless`). |
| 72 | +2. Align harness MCP env to that mode. |
| 73 | +3. Restart harness session. |
| 74 | +4. Re-run minimal navigation check. |
| 75 | +5. Re-run login or demo flow. |
| 76 | +6. Validate with an authenticated-page check. |
| 77 | + |
| 78 | +Stop and escalate when: |
| 79 | +- Same failure repeats after two config-correct restarts. |
| 80 | +- Mode appears correct but auth state is unstable across immediate retries. |
| 81 | + |
| 82 | +## Definition Of Done |
| 83 | + |
| 84 | +- Intended mode is explicitly confirmed by behavior. |
| 85 | +- Target flow completes in that mode. |
| 86 | +- Follow-up authenticated-page verification passes. |
| 87 | +- Behavior remains stable after one full harness restart. |
| 88 | + |
0 commit comments