fix(mcp): auto-recover when remote browser disconnects mid-session#40652
fix(mcp): auto-recover when remote browser disconnects mid-session#40652pavelfeldman wants to merge 1 commit intomicrosoft:mainfrom
Conversation
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
Test results for "MCP"17 failed 6933 passed, 1058 skipped Merge workflow run. |
Summary
browserContextclose /browserdisconnect insideBrowserBackend.initialize; stampisClose: trueon the next tool result so the MCP server discards the dead backend and the following call transparently reconnects — no manualbrowser_closeneeded.Fixes microsoft/playwright-mcp#1588