Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/playwright-core/src/server/utils/processLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export async function gracefullyCloseAll() {
await Promise.all(Array.from(gracefullyCloseSet).map(gracefullyClose => gracefullyClose().catch(e => {})));
}

export function gracefullyProcessExitDoNotHang(code: number) {
export function gracefullyProcessExitDoNotHang(code: number, onExit?: () => Promise<void>) {
// Force exit after 30 seconds.
const beforeExit = onExit ? () => onExit().catch(() => {}) : () => Promise.resolve();
// eslint-disable-next-line no-restricted-properties
setTimeout(() => process.exit(code), 30000);
const callback = () => beforeExit().then(() => process.exit(code));

setTimeout(callback, 30000);
// Meanwhile, try to gracefully close all browsers.
gracefullyCloseAll().then(() => {
// eslint-disable-next-line no-restricted-properties
process.exit(code);
});
gracefullyCloseAll().then(callback);
}

function exitHandler() {
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright/src/mcp/browser/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type CLIOptions = {
daemonDataDir?: string;
daemonHeaded?: boolean;
device?: string;
extension?: boolean;
executablePath?: string;
grantPermissions?: string[];
headless?: boolean;
Expand Down Expand Up @@ -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,
},
Expand Down
23 changes: 12 additions & 11 deletions packages/playwright/src/mcp/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/mcp/terminal/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const close = declareCommand({
description: 'Close the page',
category: 'core',
args: z.object({}),
toolName: 'browser_close',
toolName: '',
toolParams: () => ({}),
});

Expand Down
7 changes: 4 additions & 3 deletions packages/playwright/src/mcp/terminal/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ export async function startMcpDaemonServer(
daemonDebug('received command', method);
if (method === 'stop') {
daemonDebug('stop command received, shutting down');
await connection.send({ id, result: 'ok' });
server.close();
gracefullyProcessExitDoNotHang(0);
gracefullyProcessExitDoNotHang(0, async () => {
await connection.send({ id, result: 'ok' }).catch(() => {});
server.close();
});
} else if (method === 'run') {
const { toolName, toolParams } = parseCliCommand(params.args);
const response = await backend.callTool(toolName, toolParams, () => {});
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/src/mcp/terminal/helpGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Loading
Loading