Skip to content

fix(mcp): auto-recover when remote browser disconnects mid-session#40652

Open
pavelfeldman wants to merge 1 commit intomicrosoft:mainfrom
pavelfeldman:fix-1588
Open

fix(mcp): auto-recover when remote browser disconnects mid-session#40652
pavelfeldman wants to merge 1 commit intomicrosoft:mainfrom
pavelfeldman:fix-1588

Conversation

@pavelfeldman
Copy link
Copy Markdown
Member

Summary

  • Listen for browserContext close / browser disconnect inside BrowserBackend.initialize; stamp isClose: true on the next tool result so the MCP server discards the dead backend and the following call transparently reconnects — no manual browser_close needed.
  • Adds a CDP regression test that drops the endpoint mid-session and verifies the next call recovers automatically.

Fixes microsoft/playwright-mcp#1588

When connecting via --cdp-endpoint to a remote browser service that
enforces session timeouts (e.g. Browserless), the remote can kill the
CDP session at any moment. Today, the next tool call fails with
"Target page, context or browser has been closed" — and so do all
subsequent calls, because BrowserBackend.callTool returns the error
without signalling isClose, leaving the server holding a dead backend.
The only escape was a manual browser_close -> browser_navigate sequence,
which AI agents rarely discover on their own.

Listen for the browserContext 'close' and browser 'disconnected' events
on initialize(). When either fires, mark the backend as disconnected
and stamp isClose: true on the next tool result. The existing server.ts
path then disposes the backend and clears backendPromise, so the
following tool call transparently establishes a fresh CDP connection —
no manual browser_close needed.

Adds tests/mcp/cdp.spec.ts coverage that drops the CDP endpoint
mid-session and verifies the next call recovers automatically. The
cdpServer fixture is taught to accept a restart after disconnect.

Fixes: microsoft/playwright-mcp#1588
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

Test results for "MCP"

17 failed
❌ [firefox] › mcp/tracing.spec.ts:54 › check that trace is saved with browser_start_tracing (no output dir) @mcp-windows-latest-firefox
❌ [msedge] › mcp/cli-session.spec.ts:130 › workspace isolation - sessions in different workspaces are isolated @mcp-windows-latest-msedge
❌ [msedge] › mcp/cli-session.spec.ts:162 › list --all lists sessions from all workspaces @mcp-windows-latest-msedge
❌ [msedge] › mcp/cli-storage.spec.ts:61 › state-load restores storage state from file @mcp-windows-latest-msedge
❌ [msedge] › mcp/cli-storage.spec.ts:100 › state-save and state-load roundtrip @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:124 › daemon show: closing page exits the process @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:164 › should capture annotations via show --annotate @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:185 › should start dashboard and annotate when no dashboard is running @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:207 › should enter annotate mode on fresh dashboard.tsx mount with -s --annotate @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:231 › should annotate via direct browser_annotate MCP call @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:264 › should annotate when context has no fixed viewport @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:301 › should cancel browser_annotate when the MCP request is aborted @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:332 › should cancel browser_annotate when the MCP client disconnects @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:361 › should switch screencast to -s session on show --annotate @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:410 › should disengage annotate mode when --annotate client disconnects @mcp-windows-latest-msedge
❌ [msedge] › mcp/dashboard.spec.ts:499 › save recording streams WebM bytes to the chosen file @mcp-windows-latest-msedge
❌ [webkit] › mcp/config.ini.spec.ts:57 › ini config sets browser launch options @mcp-windows-latest-webkit

6933 passed, 1058 skipped


Merge workflow run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CDP endpoint: auto-recover on remote browser disconnect instead of requiring browser_close

1 participant