Skip to content

Commit 330a3da

Browse files
committed
chore(cli): "info" method that returns the version
This will be used for compatibility checks later on. Drive-by: remove `sessionConfig` from the mcp config, leave it only for the cli daemon.
1 parent fed4a0a commit 330a3da

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

packages/playwright/src/cli/client/registry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export type ClientInfo = {
2828
workspaceDir: string | undefined;
2929
};
3030

31+
export type ServerInfo = {
32+
version: string;
33+
};
34+
3135
export type SessionConfig = {
3236
name: string;
3337
version: string;

packages/playwright/src/cli/client/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ to restart the browser session.`);
173173
if (!socket)
174174
socket = await this._startDaemon();
175175

176-
this._connection = new SocketConnection(socket, this.config.version);
176+
this._connection = new SocketConnection(socket);
177177
this._connection.onmessage = message => this._onMessage(message);
178178
this._connection.onclose = () => this.disconnect();
179179
return this._connection;

packages/playwright/src/cli/client/socketConnection.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ import net from 'net';
1919
export class SocketConnection {
2020
private _socket: net.Socket;
2121
private _pendingBuffers: Buffer[] = [];
22-
private _version: string;
2322

2423
onclose?: () => void;
2524
onmessage?: (message: any) => void;
26-
onversionerror?: (id: number, versions: { expected: string, received: string }) => boolean;
2725

28-
constructor(socket: net.Socket, version: string) {
26+
constructor(socket: net.Socket) {
2927
this._socket = socket;
30-
this._version = version;
3128
socket.on('data', buffer => this._onData(buffer));
3229
socket.on('close', () => {
3330
this.onclose?.();
@@ -39,7 +36,7 @@ export class SocketConnection {
3936

4037
async send(message: { id: number, error?: string, result?: any }) {
4138
await new Promise((resolve, reject) => {
42-
this._socket.write(`${JSON.stringify({ ...message, version: this._version })}\n`, error => {
39+
this._socket.write(`${JSON.stringify(message)}\n`, error => {
4340
if (error)
4441
reject(error);
4542
else
@@ -76,10 +73,6 @@ export class SocketConnection {
7673
private _dispatchMessage(message: string) {
7774
try {
7875
const parsedMessage = JSON.parse(message);
79-
if (parsedMessage.version !== this._version) {
80-
if (this.onversionerror?.(parsedMessage.id, { expected: this._version, received: parsedMessage.version }))
81-
return;
82-
}
8376
this.onmessage?.(parsedMessage);
8477
} catch (e) {
8578
// eslint-disable-next-line no-console

packages/playwright/src/cli/daemon/daemon.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { parseCommand } from './command';
3131
import type * as mcp from '../../mcp/sdk/exports';
3232
import type { BrowserContextFactory } from '../../mcp/browser/browserContextFactory';
3333
import type { FullConfig } from '../../mcp/browser/config';
34+
import type { ServerInfo, SessionConfig } from '../client/registry';
3435

3536
const daemonDebug = debug('pw:daemon');
3637

@@ -45,11 +46,11 @@ async function socketExists(socketPath: string): Promise<boolean> {
4546
}
4647

4748
export async function startMcpDaemonServer(
48-
config: FullConfig,
49+
mcpConfig: FullConfig,
50+
sessionConfig: SessionConfig,
4951
contextFactory: BrowserContextFactory,
5052
): Promise<string> {
51-
const sessionConfig = config.sessionConfig!;
52-
const { socketPath, version } = sessionConfig;
53+
const { socketPath } = sessionConfig;
5354
// Clean up existing socket file on Unix
5455
if (os.platform() !== 'win32' && await socketExists(socketPath)) {
5556
daemonDebug(`Socket already exists, removing: ${socketPath}`);
@@ -81,7 +82,7 @@ export async function startMcpDaemonServer(
8182
const existingContextFactory = {
8283
createContext: () => Promise.resolve({ browserContext, close }),
8384
};
84-
const backend = new BrowserServerBackend(config, existingContextFactory, { allTools: true });
85+
const backend = new BrowserServerBackend(mcpConfig, existingContextFactory, { allTools: true });
8586
await backend.initialize?.(clientInfo);
8687

8788
await fs.mkdir(path.dirname(socketPath), { recursive: true });
@@ -94,7 +95,7 @@ export async function startMcpDaemonServer(
9495

9596
const server = net.createServer(socket => {
9697
daemonDebug('new client connection');
97-
const connection = new SocketConnection(socket, version);
98+
const connection = new SocketConnection(socket);
9899
connection.onclose = () => {
99100
daemonDebug('client disconnected');
100101
};
@@ -114,6 +115,9 @@ export async function startMcpDaemonServer(
114115
toolParams._meta = { cwd: params.cwd };
115116
const response = await backend.callTool(toolName, toolParams);
116117
await connection.send({ id, result: formatResult(response) });
118+
} else if (method === 'info') {
119+
const info: ServerInfo = { version: sessionConfig.version };
120+
await connection.send({ id, result: info });
117121
} else {
118122
throw new Error(`Unknown method: ${method}`);
119123
}

packages/playwright/src/cli/daemon/program.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ export function decorateCLICommand(command: Command, version: string) {
3737
options.chromiumSandbox = options.chromiumSandbox === true ? undefined : false;
3838
setupExitWatchdog();
3939

40-
const config = await resolveCLIConfig(options.daemonSession);
41-
const browserContextFactory = contextFactory(config);
42-
const extensionContextFactory = new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir, config.browser.launchOptions.executablePath);
40+
const sessionConfig = await fs.promises.readFile(options.daemonSession, 'utf-8').then(data => JSON.parse(data) as SessionConfig);
41+
const mcpConfig = await resolveCLIConfig(sessionConfig);
42+
const browserContextFactory = contextFactory(mcpConfig);
43+
const extensionContextFactory = new ExtensionContextFactory(mcpConfig.browser.launchOptions.channel || 'chrome', mcpConfig.browser.userDataDir, mcpConfig.browser.launchOptions.executablePath);
4344

44-
const cf = config.extension ? extensionContextFactory : browserContextFactory;
45+
const cf = mcpConfig.extension ? extensionContextFactory : browserContextFactory;
4546
try {
46-
const socketPath = await startMcpDaemonServer(config, cf);
47+
const socketPath = await startMcpDaemonServer(mcpConfig, sessionConfig, cf);
4748
console.log(`### Config`);
4849
console.log('```json');
49-
console.log(JSON.stringify(config, null, 2));
50+
console.log(JSON.stringify(mcpConfig, null, 2));
5051
console.log('```');
5152
console.log(`### Success\nDaemon listening on ${socketPath}`);
5253
console.log('<EOF>');
@@ -58,8 +59,7 @@ export function decorateCLICommand(command: Command, version: string) {
5859
});
5960
}
6061

61-
export async function resolveCLIConfig(daemonSession: string): Promise<FullConfig> {
62-
const sessionConfig = await fs.promises.readFile(daemonSession, 'utf-8').then(data => JSON.parse(data) as SessionConfig);
62+
async function resolveCLIConfig(sessionConfig: SessionConfig): Promise<FullConfig> {
6363
const daemonOverrides = configFromCLIOptions({
6464
config: sessionConfig.cli.config,
6565
browser: sessionConfig.cli.browser,
@@ -96,9 +96,8 @@ export async function resolveCLIConfig(daemonSession: string): Promise<FullConfi
9696
}
9797

9898
result.configFile = configFile;
99-
result.sessionConfig = sessionConfig;
10099
result.skillMode = true;
101-
if (result.sessionConfig && result.browser.launchOptions.headless !== false)
100+
if (result.browser.launchOptions.headless !== false)
102101
result.browser.contextOptions.viewport ??= { width: 1280, height: 720 };
103102

104103
await validateConfig(result);

packages/playwright/src/mcp/browser/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ export type FullConfig = Config & {
134134
},
135135
skillMode?: boolean;
136136
configFile?: string;
137-
sessionConfig?: SessionConfig;
138137
};
139138

140139
export async function resolveConfig(config: Config): Promise<FullConfig> {

0 commit comments

Comments
 (0)