[Feature] Per-client tab ownership in `--shared-browser-context` (share one logged-in persistent profile between MCP clients without `--isolated`)
Feature Request
A client should own only the pages it created (plus popups opened from them), and the server should close a client's pages when it disconnects. Four small changes in Context, no new flags:
_onPageCreated: do not adopt a page blindly. Adopt it ifpage.opener()resolves to a page this client already owns (popups,target=_blank,window.open, with or withoutnoopener— all tested); otherwise ignore it.newTab(): adopt the page it just created (this is the only other way a page becomes owned).dispose(): after disposing theTabwrappers,page.close()each owned page.- Optional, for headed shared browsers: create new pages with
Target.createTarget({ background: true })(envPW_SHARED_BG_TABS=1in our patch) so a client cannot steal focus from the user typing a password in another tab; an explicitbrowser_tabs selectstill brings the tab forward.
With this, browser_tabs list per client shows only its tabs, select/close by index cannot reach a foreign tab, a new client starts with an empty list (its first browser_navigate opens its own tab via ensureTab()), and a dead client's tabs disappear instead of becoming someone else's "current".
Our implementation is a 5-anchor text patch on the built bundle (coreBundle.js), applied to _onPageCreated, newTab, Context.dispose, the download handler (.catch instead of void, to keep an unhandled rejection from taking the shared server down) and CRBrowserContext.doCreateNewPage. Source and a scenario suite (isolation, popups, disconnect and ping-timeout cleanup, 4 concurrent clients × 12 cycles, headed focus) are here: https://github.com/757933-cloud/claude-shared-playwright — I'm happy to turn it into a PR against context.ts if the approach is acceptable.
Things I'm not sure about
- Whether
page.opener()is the right ownership signal for pages created by service workers /chrome.tabs-like flows — for a persistent Chrome profile with extensions this may need a fallback. - Firefox/WebKit:
background: trueis CDP-only; the ownership part is engine-agnostic. - Whether closing a dead client's pages should be opt-in (a client that reconnects after a network blip currently gets an empty list rather than its old tabs — acceptable for us, maybe not for everyone).
Example
What goes wrong today (reproduced on 0.0.82)
packages/playwright-core/src/tools/backend/context.ts:
_onPageCreated(line 311) adopts every page of the shared context intothis._tabs, and makes it current if the client has none — so a freshly connected client inherits whatever tab another client opened last (program.ts:155hands every clientbrowser.contexts()[0])._onPageClosed(line 319) makes the neighbouring tab current — after a client closes its own last tab, its nextbrowser_navigatelands in another client's tab.dispose()(line 142) disposesTabwrappers but leaves the pages open, so a client that disconnected (DELETE, or the 5 s ping timeout) leaks its tabs into every other client's list; if it was the last client the whole browser is closed instead.- After a server restart every reconnecting client is again on tab 0, i.e. the same tab.
Measured: two clients ended up navigating and closing each other's tabs within a 12-cycle loop; a client's browser_tabs close (no index) closed a tab another client was working in.
Motivation
@playwright/mcp 0.0.82, macOS, Node 24. Several Claude Code sessions on one machine need the same logged-in persistent profile (GitHub, Railway, Google Search Console…). Per-session stdio servers on one --user-data-dir fail with "Browser is already in use"; --isolated discards the logins. So we run one --port server with --shared-browser-context and point every session at it over HTTP. That works, except that every client's Context sees all pages of the shared BrowserContext.
#1631 reported the resulting cross-client bleed and was closed with "use --isolated / a --user-data-dir per client". That answer does not cover the case where sharing the logged-in profile is the whole point. This is a proposal (with a working implementation we run in production) to make --shared-browser-context safe for that case.
Source: microsoft/playwright