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
13 changes: 6 additions & 7 deletions packages/playwright-core/src/tools/mcp/cdpRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export class CDPRelayServer {
return `${this._wsHost}${this._extensionPath}`;
}

async ensureExtensionConnectionForMCPContext(clientInfo: ClientInfo, forceNewTab: boolean) {
async ensureExtensionConnectionForMCPContext(clientInfo: ClientInfo) {
debugLogger('Ensuring extension connection for MCP context');
if (this._extensionConnection)
return;
this._connectBrowser(clientInfo, forceNewTab);
this._connectBrowser(clientInfo);
debugLogger('Waiting for incoming extension connection');
await Promise.race([
this._extensionConnectionPromise,
Expand All @@ -114,7 +114,7 @@ export class CDPRelayServer {
debugLogger('Extension connection established');
}

private _connectBrowser(clientInfo: ClientInfo, forceNewTab: boolean) {
private _connectBrowser(clientInfo: ClientInfo) {
const mcpRelayEndpoint = `${this._wsHost}${this._extensionPath}`;
// Need to specify "key" in the manifest.json to make the id stable when loading from file.
const url = new URL('chrome-extension://mmlmfjhmonkocbjadbfplnigmagldckm/connect.html');
Expand All @@ -125,16 +125,15 @@ export class CDPRelayServer {
};
url.searchParams.set('client', JSON.stringify(client));
url.searchParams.set('protocolVersion', process.env.PWMCP_TEST_PROTOCOL_VERSION ?? protocol.VERSION.toString());
if (forceNewTab)
url.searchParams.set('newTab', 'true');
const token = process.env.PLAYWRIGHT_MCP_EXTENSION_TOKEN;
if (token)
url.searchParams.set('token', token);
const href = url.toString();

const channel = registry.isChromiumAlias(this._browserChannel) ? 'chromium' : this._browserChannel;
let executablePath = this._executablePath;
if (!executablePath) {
const executableInfo = registry.findExecutable(this._browserChannel);
const executableInfo = registry.findExecutable(channel);
if (!executableInfo)
throw new Error(`Unsupported channel: "${this._browserChannel}"`);
executablePath = executableInfo.executablePath();
Expand All @@ -145,7 +144,7 @@ export class CDPRelayServer {
const args: string[] = [];
if (this._userDataDir)
args.push(`--user-data-dir=${this._userDataDir}`);
if (os.platform() === 'linux' && this._browserChannel === 'chromium')
if (os.platform() === 'linux' && channel === 'chromium')
args.push('--no-sandbox');
args.push(href);
spawn(executablePath, args, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export async function createExtensionBrowser(config: FullConfig, clientInfo: Cli
config.browser.launchOptions.executablePath);
debugLogger(`CDP relay server started, extension endpoint: ${relay.extensionEndpoint()}.`);

await relay.ensureExtensionConnectionForMCPContext(clientInfo, /* forceNewTab */ false);
await relay.ensureExtensionConnectionForMCPContext(clientInfo);
return await playwright.chromium.connectOverCDP(relay.cdpEndpoint(), { isLocal: true });
}
2 changes: 1 addition & 1 deletion tests/mcp/cli-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function cliEnv() {
return {
PLAYWRIGHT_SERVER_REGISTRY: test.info().outputPath('registry'),
PLAYWRIGHT_DAEMON_SESSION_DIR: test.info().outputPath('daemon'),
PLAYWRIGHT_SOCKETS_DIR: path.join(test.info().project.outputDir, 'ds'),
PLAYWRIGHT_SOCKETS_DIR: path.join(test.info().project.outputDir, 'ds', String(test.info().parallelIndex)),
};
}

Expand Down
Loading