Repository navigation
fix(mcp): connect each client to the shared browser separately #42622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Pavel Feldman (pavelfeldman)
merged 1 commit into
microsoft:main
from
pavelfeldman:fix-42608-shared-connect
Sep 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,15 @@ import { Option as ProgramOption } from 'commander'; | |
| import * as mcpServer from '../utils/mcp/server'; | ||
| import { commaSeparatedList, defaultCodegenLanguage, dotenvFileLoader, enumParser, headerParser, numberParser, resolutionParser, resolveCLIConfigForMCP, semicolonSeparatedList } from './config'; | ||
| import { setupExitWatchdog } from './watchdog'; | ||
| import { createBrowserWithInfo } from './browserFactory'; | ||
| import { connectToBrowserEndpoint, createBrowserWithInfo } from './browserFactory'; | ||
| import { BrowserBackend } from '../backend/browserBackend'; | ||
| import { filteredTools } from '../backend/tools'; | ||
| import { testDebug } from './log'; | ||
| import { packageJSON } from '../../package'; | ||
|
|
||
| import type { Command } from 'commander'; | ||
| import type { ClientInfo } from '../utils/mcp/server'; | ||
| import type { BrowserWithInfo } from './browserFactory'; | ||
| import type * as playwright from '../../..'; | ||
|
|
||
| const version = packageJSON.version; | ||
|
|
@@ -97,7 +98,7 @@ export function decorateMCPCommand(command: Command) { | |
| const config = await resolveCLIConfigForMCP(options); | ||
| const tools = filteredTools(config); | ||
| const useSharedBrowser = config.sharedBrowserContext || config.browser.isolated; | ||
| let sharedBrowserPromise: Promise<playwright.Browser> | undefined; | ||
| let sharedBrowserPromise: Promise<BrowserWithInfo> | undefined; | ||
| let clientCount = 0; | ||
| const clientNameCounters = new Map<string, number>(); | ||
|
|
||
|
|
@@ -108,55 +109,72 @@ export function decorateMCPCommand(command: Command) { | |
| toolSchemas: tools.map(tool => tool.schema), | ||
| create: async (clientInfo: ClientInfo) => { | ||
| if (useSharedBrowser && !sharedBrowserPromise) { | ||
| const promise = (async () => { | ||
| const { browser, canBind } = await createBrowserWithInfo(config, clientInfo, options); | ||
| if (canBind) | ||
| await browser.bind(clientInfo.clientName, { workspaceDir: clientInfo.cwd }); | ||
| browser.once('disconnected', () => { | ||
| const promise = createBrowserWithInfo(config, clientInfo, options, { title: clientInfo.clientName, workspaceDir: clientInfo.cwd }).then(shared => { | ||
| shared.browser.once('disconnected', () => { | ||
| if (sharedBrowserPromise === promise) | ||
| sharedBrowserPromise = undefined; | ||
| }); | ||
| return browser; | ||
| })().catch(error => { | ||
| return shared; | ||
| }, error => { | ||
| if (sharedBrowserPromise === promise) | ||
| sharedBrowserPromise = undefined; | ||
| throw error; | ||
| }); | ||
| sharedBrowserPromise = promise; | ||
| } | ||
| clientCount++; | ||
| const promise = sharedBrowserPromise; | ||
| let shared: BrowserWithInfo | undefined; | ||
| let browser: BrowserWithInfo['browser']; | ||
| try { | ||
| const promise = sharedBrowserPromise; | ||
| const { browser, canBind } = promise ? { browser: await promise, canBind: false } : await createBrowserWithInfo(config, clientInfo, options); | ||
| if (canBind) { | ||
| shared = await promise; | ||
| if (shared) { | ||
| testDebug('connect to shared browser'); | ||
| browser = await connectToBrowserEndpoint(config, shared.browser, shared.endpoint); | ||
| } else { | ||
| const count = (clientNameCounters.get(clientInfo.clientName) ?? 0) + 1; | ||
| clientNameCounters.set(clientInfo.clientName, count); | ||
| const sessionName = count > 1 ? `${clientInfo.clientName} (${count})` : clientInfo.clientName; | ||
| await browser.bind(sessionName, { workspaceDir: clientInfo.cwd }); | ||
| browser = (await createBrowserWithInfo(config, clientInfo, options, { title: sessionName, workspaceDir: clientInfo.cwd })).browser; | ||
| } | ||
| const browserContext = config.browser.isolated ? await browser.newContext(config.browser.contextOptions) : browser.contexts()[0]; | ||
| return new BrowserBackend(config, browserContext, tools, async () => { | ||
| clientCount--; | ||
|
|
||
| if (sharedBrowserPromise && clientCount > 0) { | ||
| if (config.browser.isolated) { | ||
| testDebug('close context'); | ||
| await browserContext.close().catch(() => { }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| testDebug('close browser'); | ||
| if (sharedBrowserPromise === promise) | ||
| sharedBrowserPromise = undefined; | ||
| await browserContext.close().catch(() => { }); | ||
| await browser.close().catch(() => { }); | ||
| }); | ||
| } catch (error) { | ||
| // The dispose callback never runs for a failed create. | ||
| clientCount--; | ||
| throw error; | ||
| } | ||
|
|
||
| let browserContext: playwright.BrowserContext; | ||
| try { | ||
| // A `launchServer` remote isolates contexts per connection, so a fresh connection may see none. | ||
| browserContext = config.browser.isolated ? await browser.newContext(config.browser.contextOptions) : browser.contexts()[0] ?? await browser.newContext(config.browser.contextOptions); | ||
| } catch (error) { | ||
| clientCount--; | ||
| await browser.close().catch(() => { }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we call |
||
| throw error; | ||
| } | ||
|
|
||
| return new BrowserBackend(config, browserContext, tools, async () => { | ||
| clientCount--; | ||
| const last = !shared || !clientCount; | ||
| if (last && sharedBrowserPromise === promise) | ||
| sharedBrowserPromise = undefined; | ||
|
|
||
| if (!last) { | ||
| if (config.browser.isolated) { | ||
| testDebug('close context'); | ||
| await browserContext.close().catch(() => { }); | ||
| } else { | ||
| testDebug('disconnect from shared browser'); | ||
| } | ||
| await browser.close().catch(() => { }); | ||
| return; | ||
| } | ||
|
|
||
| testDebug('close browser'); | ||
| await browserContext.close().catch(() => { }); | ||
| await browser.close().catch(() => { }); | ||
| await shared?.browser.close().catch(() => { }); | ||
| }); | ||
| }, | ||
| }; | ||
| await mcpServer.start(factory, config.server); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now binds in
extensionmode too. Do we really want that?