Skip to content

fix(mcp): create a browser context when connecting to a remote browser with no contexts#41354

Open
abhishek-lambda wants to merge 1 commit into
microsoft:mainfrom
abhishek-lambda:fix/mcp-remote-endpoint-no-context
Open

fix(mcp): create a browser context when connecting to a remote browser with no contexts#41354
abhishek-lambda wants to merge 1 commit into
microsoft:mainfrom
abhishek-lambda:fix/mcp-remote-endpoint-no-context

Conversation

@abhishek-lambda

@abhishek-lambda abhishek-lambda commented Jun 18, 2026

Copy link
Copy Markdown

Problem

Driving the Playwright MCP server against a remote browser via --endpoint connects successfully, but the first tool call (e.g. browser_navigate) throws:

TypeError: Cannot read properties of undefined (reading 'once')

Root cause

In packages/playwright-core/src/tools/mcp/program.ts, the backend resolves the context like this:

const browserContext = config.browser.isolated
  ? await browser.newContext(config.browser.contextOptions)
  : browser.contexts()[0];

For the --endpoint (remote) transport, config resolution forces isolated = false, so the browser.contexts()[0] branch is taken. A browser obtained via browserType.connect() has zero contexts (the caller is expected to create one), so contexts()[0] is undefined. BrowserBackend then dereferences it (browserContext.once('close', …)), crashing on the first tool call.

The sibling cli-daemon/program.ts already anticipates the empty-contexts case (it throws an explicit error there), but the MCP server path neither guards nor handles it.

Fix

Create a context when the connected browser has none — a one-condition change, no duplicated newContext call:

const browserContext = config.browser.isolated || !browser.contexts().length
  ? await browser.newContext(config.browser.contextOptions)
  : browser.contexts()[0];

Behavior is unchanged for the local/persistent and isolated paths (which already have a context or explicitly create one); it only adds a context for remote-connected browsers that start with none.

Verification

Reproduced against a remote Playwright endpoint (LambdaTest grid, wss://cdp.lambdatest.com/playwright) on @playwright/mcp v0.0.76:

  • Before: first browser_navigate throws Cannot read properties of undefined (reading 'once').
  • After: a context is created and navigate → type → wait → snapshot runs end-to-end with no errors.

The same root cause is currently worked around externally by supplying a contextGetter to the programmatic createConnection(config, contextGetter) API; this change makes the stock --endpoint flag work without that workaround.


Connecting to a remote browser via --endpoint forces isolated=false, so
the server takes the browser.contexts()[0] branch. A browserType.connect()-ed
browser has no contexts, so contexts()[0] is undefined and BrowserBackend
crashes on the first tool call with "Cannot read properties of undefined
(reading 'once')". Create a context when none exists.
@abhishek-lambda

Copy link
Copy Markdown
Author

@dgozman please review this PR

@abhishek-lambda

Copy link
Copy Markdown
Author

@yury-s @dcrousso please review as well

await browser.bind(sessionName, { workspaceDir: clientInfo.cwd });
}
const browserContext = config.browser.isolated ? await browser.newContext(config.browser.contextOptions) : browser.contexts()[0];
const browserContext = config.browser.isolated || !browser.contexts().length ? await browser.newContext(config.browser.contextOptions) : browser.contexts()[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: browser.contexts() is potentially not a cheap operation, so can we reorganize this a little?

let browserContext = browser.contexts()[0];
if (!browserContext || config.browser.isolated)
    browserContext = await browser.newContext(config.browser.contextOptions);

@github-actions

Copy link
Copy Markdown
Contributor

Test results for "MCP"

7354 passed, 1122 skipped


Merge workflow run.

@dgozman

dgozman commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

@abhishek-lambda We require an issue for every non-trivial contribution, see this guide.

@yury-s

yury-s commented Jun 18, 2026

Copy link
Copy Markdown
Member

@abhishek-lambda We require an issue for every non-trivial contribution, see this guide.

Yes, please start with that. Which version of mcp are you using? I believe this was fixed in #41203

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.

4 participants