Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(mcp): support --extesnsion in cli (#38979)
Yury Semikhatsky51b1d43e836e
diff --git a/packages/playwright/src/mcp/browser/config.ts b/packages/playwright/src/mcp/browser/config.tsindex 9021dabea5..7c3a628e13 100644--- a/packages/playwright/src/mcp/browser/config.ts+++ b/packages/playwright/src/mcp/browser/config.ts@@ -46,6 +46,7 @@ export type CLIOptions = {daemonDataDir?: string;daemonHeaded?: boolean;device?: string;+ extension?: boolean;executablePath?: string;grantPermissions?: string[];headless?: boolean;@@ -108,7 +109,7 @@ export const defaultConfig: FullConfig = {const defaultDaemonConfig = (cliOptions: CLIOptions) => mergeConfig(defaultConfig, {browser: {- userDataDir: '<daemon-data-dir>',+ userDataDir: cliOptions.extension ? undefined : '<daemon-data-dir>', // Use default user profile with extension.launchOptions: {headless: !cliOptions.daemonHeaded,},diff --git a/packages/playwright/src/mcp/program.ts b/packages/playwright/src/mcp/program.tsindex 49f124870d..0cea3d07fc 100644--- a/packages/playwright/src/mcp/program.ts+++ b/packages/playwright/src/mcp/program.ts@@ -106,26 +106,27 @@ export function decorateCommand(command: Command, version: string) {const browserContextFactory = contextFactory(config);const extensionContextFactory = new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir, config.browser.launchOptions.executablePath);- if (options.extension) {+ if (options.daemon) {+ const contextFactory = options.extension ? extensionContextFactory : browserContextFactory;const serverBackendFactory: mcpServer.ServerBackendFactory = {- name: 'Playwright w/ extension',- nameInConfig: 'playwright-extension',+ name: 'Playwright',+ nameInConfig: 'playwright-daemon',version,- create: () => new BrowserServerBackend(config, extensionContextFactory)+ create: () => new BrowserServerBackend(config, contextFactory, { allTools: true, structuredOutput: true })};- await mcpServer.start(serverBackendFactory, config.server);+ const socketPath = await startMcpDaemonServer(options.daemon, serverBackendFactory);+ console.error(`Daemon server listening on ${socketPath}`);return;}- if (options.daemon) {+ if (options.extension) {const serverBackendFactory: mcpServer.ServerBackendFactory = {- name: 'Playwright',- nameInConfig: 'playwright-daemon',+ name: 'Playwright w/ extension',+ nameInConfig: 'playwright-extension',version,- create: () => new BrowserServerBackend(config, browserContextFactory, { allTools: true, structuredOutput: true })+ create: () => new BrowserServerBackend(config, extensionContextFactory)};- const socketPath = await startMcpDaemonServer(options.daemon, serverBackendFactory);- console.error(`Daemon server listening on ${socketPath}`);+ await mcpServer.start(serverBackendFactory, config.server);return;}diff --git a/packages/playwright/src/mcp/terminal/helpGenerator.ts b/packages/playwright/src/mcp/terminal/helpGenerator.tsindex 787b0d340e..288d02684d 100644--- a/packages/playwright/src/mcp/terminal/helpGenerator.ts+++ b/packages/playwright/src/mcp/terminal/helpGenerator.ts@@ -97,6 +97,7 @@ export function generateHelp() {lines.push('\nGlobal options:');lines.push(formatWithGap(' --config <path>', 'create a session with custom config, defaults to `playwright-cli.json`'));+ lines.push(formatWithGap(' --extension', 'connect to a running browser instance using Playwright MCP Bridge extension'));lines.push(formatWithGap(' --headed', 'create a headed session'));lines.push(formatWithGap(' --help [command]', 'print help'));lines.push(formatWithGap(' --session', 'run command in the scope of a specific session'));diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.tsindex 70fbae675e..7908b64119 100644--- a/packages/playwright/src/mcp/terminal/program.ts+++ b/packages/playwright/src/mcp/terminal/program.ts@@ -34,15 +34,17 @@ export type StructuredResponse = {sections: Section[];};+type SessionOptions = { config?: string, headed?: boolean, extension?: boolean };+class Session {readonly name: string;private _socketPath: string;private _connection: SocketConnection | undefined;private _nextMessageId = 1;private _callbacks = new Map<number, { resolve: (o: any) => void, reject: (e: Error) => void, method: string, params: any }>();- private _options: { config?: string, headed?: boolean };+ private _options: SessionOptions;- constructor(name: string, options: { config?: string, headed?: boolean }) {+ constructor(name: string, options: SessionOptions) {this.name = name;this._socketPath = this._daemonSocketPath();this._options = options;@@ -190,6 +192,7 @@ class Session {const configFile = resolveConfigFile(this._options.config);const configArg = configFile !== undefined ? [`--config=${configFile}`] : [];const headedArg = this._options.headed ? [`--daemon-headed`] : [];+ const extensionArg = this._options.extension ? [`--extension`] : [];const outLog = path.join(daemonProfilesDir, 'out.log');const errLog = path.join(daemonProfilesDir, 'err.log');@@ -203,6 +206,7 @@ class Session {`--daemon-data-dir=${userDataDir}`,...configArg,...headedArg,+ ...extensionArg,], {detached: true,stdio: ['ignore', out, err],@@ -253,8 +257,6 @@ class Session {}}-type SessionOptions = { config?: string, headed?: boolean };-class SessionManager {readonly sessions: Map<string, Session>;@@ -400,10 +402,12 @@ const daemonProfilesDir = (() => {export async function program(options: { version: string }) {const argv = process.argv.slice(2);const args = require('minimist')(argv, {- boolean: ['help', 'version', 'headed'],+ boolean: ['help', 'version', 'headed', 'extension'],});if (!argv.includes('--headed') && !argv.includes('--no-headed'))delete args.headed;+ if (!argv.includes('--extension'))+ delete args.extension;const help = require('./help.json');const commandName = args._[0];