@playwright/mcp 0.0.26 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -137,7 +137,10 @@ Playwright MCP server supports following arguments. They can be provided in the
137
137
  --ignore-https-errors ignore https errors
138
138
  --isolated keep the browser profile in memory, do not save
139
139
  it to disk.
140
- --no-image-responses do not send image responses to the client.
140
+ --image-responses <mode> whether to send image responses to the client.
141
+ Can be "allow", "omit", or "auto". Defaults to
142
+ "auto", which sends images if the client can
143
+ display them.
141
144
  --no-sandbox disable the sandbox for all process types that
142
145
  are normally sandboxed.
143
146
  --output-dir <path> path to the directory for output files.
@@ -196,7 +199,7 @@ state [here](https://playwright.dev/docs/auth).
196
199
  "args": [
197
200
  "@playwright/mcp@latest",
198
201
  "--isolated",
199
- "--storage-state={path/to/storage.json}
202
+ "--storage-state={path/to/storage.json}"
200
203
  ]
201
204
  }
202
205
  }
package/config.d.ts CHANGED
@@ -117,7 +117,7 @@ export type Config = {
117
117
  };
118
118
 
119
119
  /**
120
- * Do not send image responses to the client.
120
+ * Whether to send image responses to the client. Can be "allow", "omit", or "auto". Defaults to "auto", which sends images if the client can display them.
121
121
  */
122
- noImageResponses?: boolean;
122
+ imageResponses?: 'allow' | 'omit' | 'auto';
123
123
  };
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@
18
18
  import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
19
19
  import type { Config } from './config';
20
20
  import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
21
+ import type { BrowserContext } from 'playwright';
21
22
 
22
23
  export type Connection = {
23
24
  server: Server;
@@ -25,5 +26,5 @@ export type Connection = {
25
26
  close(): Promise<void>;
26
27
  };
27
28
 
28
- export declare function createConnection(config?: Config): Promise<Connection>;
29
+ export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Connection>;
29
30
  export {};
package/index.js CHANGED
@@ -15,5 +15,5 @@
15
15
  * limitations under the License.
16
16
  */
17
17
 
18
- import { createConnection } from './lib/index';
18
+ import { createConnection } from './lib/index.js';
19
19
  export { createConnection };
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import fs from 'node:fs';
17
+ import os from 'node:os';
18
+ import path from 'node:path';
19
+ import debug from 'debug';
20
+ import * as playwright from 'playwright';
21
+ const testDebug = debug('pw:mcp:test');
22
+ export function contextFactory(browserConfig) {
23
+ if (browserConfig.remoteEndpoint)
24
+ return new RemoteContextFactory(browserConfig);
25
+ if (browserConfig.cdpEndpoint)
26
+ return new CdpContextFactory(browserConfig);
27
+ if (browserConfig.isolated)
28
+ return new IsolatedContextFactory(browserConfig);
29
+ return new PersistentContextFactory(browserConfig);
30
+ }
31
+ class BaseContextFactory {
32
+ browserConfig;
33
+ _browserPromise;
34
+ name;
35
+ constructor(name, browserConfig) {
36
+ this.name = name;
37
+ this.browserConfig = browserConfig;
38
+ }
39
+ async _obtainBrowser() {
40
+ if (this._browserPromise)
41
+ return this._browserPromise;
42
+ testDebug(`obtain browser (${this.name})`);
43
+ this._browserPromise = this._doObtainBrowser();
44
+ void this._browserPromise.then(browser => {
45
+ browser.on('disconnected', () => {
46
+ this._browserPromise = undefined;
47
+ });
48
+ }).catch(() => {
49
+ this._browserPromise = undefined;
50
+ });
51
+ return this._browserPromise;
52
+ }
53
+ async _doObtainBrowser() {
54
+ throw new Error('Not implemented');
55
+ }
56
+ async createContext() {
57
+ testDebug(`create browser context (${this.name})`);
58
+ const browser = await this._obtainBrowser();
59
+ const browserContext = await this._doCreateContext(browser);
60
+ return { browserContext, close: () => this._closeBrowserContext(browserContext, browser) };
61
+ }
62
+ async _doCreateContext(browser) {
63
+ throw new Error('Not implemented');
64
+ }
65
+ async _closeBrowserContext(browserContext, browser) {
66
+ testDebug(`close browser context (${this.name})`);
67
+ if (browser.contexts().length === 1)
68
+ this._browserPromise = undefined;
69
+ await browserContext.close().catch(() => { });
70
+ if (browser.contexts().length === 0) {
71
+ testDebug(`close browser (${this.name})`);
72
+ await browser.close().catch(() => { });
73
+ }
74
+ }
75
+ }
76
+ class IsolatedContextFactory extends BaseContextFactory {
77
+ constructor(browserConfig) {
78
+ super('isolated', browserConfig);
79
+ }
80
+ async _doObtainBrowser() {
81
+ const browserType = playwright[this.browserConfig.browserName];
82
+ return browserType.launch({
83
+ ...this.browserConfig.launchOptions,
84
+ handleSIGINT: false,
85
+ handleSIGTERM: false,
86
+ }).catch(error => {
87
+ if (error.message.includes('Executable doesn\'t exist'))
88
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
89
+ throw error;
90
+ });
91
+ }
92
+ async _doCreateContext(browser) {
93
+ return browser.newContext(this.browserConfig.contextOptions);
94
+ }
95
+ }
96
+ class CdpContextFactory extends BaseContextFactory {
97
+ constructor(browserConfig) {
98
+ super('cdp', browserConfig);
99
+ }
100
+ async _doObtainBrowser() {
101
+ return playwright.chromium.connectOverCDP(this.browserConfig.cdpEndpoint);
102
+ }
103
+ async _doCreateContext(browser) {
104
+ return this.browserConfig.isolated ? await browser.newContext() : browser.contexts()[0];
105
+ }
106
+ }
107
+ class RemoteContextFactory extends BaseContextFactory {
108
+ constructor(browserConfig) {
109
+ super('remote', browserConfig);
110
+ }
111
+ async _doObtainBrowser() {
112
+ const url = new URL(this.browserConfig.remoteEndpoint);
113
+ url.searchParams.set('browser', this.browserConfig.browserName);
114
+ if (this.browserConfig.launchOptions)
115
+ url.searchParams.set('launch-options', JSON.stringify(this.browserConfig.launchOptions));
116
+ return playwright[this.browserConfig.browserName].connect(String(url));
117
+ }
118
+ async _doCreateContext(browser) {
119
+ return browser.newContext();
120
+ }
121
+ }
122
+ class PersistentContextFactory {
123
+ browserConfig;
124
+ _userDataDirs = new Set();
125
+ constructor(browserConfig) {
126
+ this.browserConfig = browserConfig;
127
+ }
128
+ async createContext() {
129
+ testDebug('create browser context (persistent)');
130
+ const userDataDir = this.browserConfig.userDataDir ?? await this._createUserDataDir();
131
+ this._userDataDirs.add(userDataDir);
132
+ testDebug('lock user data dir', userDataDir);
133
+ const browserType = playwright[this.browserConfig.browserName];
134
+ for (let i = 0; i < 5; i++) {
135
+ try {
136
+ const browserContext = await browserType.launchPersistentContext(userDataDir, {
137
+ ...this.browserConfig.launchOptions,
138
+ ...this.browserConfig.contextOptions,
139
+ handleSIGINT: false,
140
+ handleSIGTERM: false,
141
+ });
142
+ const close = () => this._closeBrowserContext(browserContext, userDataDir);
143
+ return { browserContext, close };
144
+ }
145
+ catch (error) {
146
+ if (error.message.includes('Executable doesn\'t exist'))
147
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
148
+ if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
149
+ // User data directory is already in use, try again.
150
+ await new Promise(resolve => setTimeout(resolve, 1000));
151
+ continue;
152
+ }
153
+ throw error;
154
+ }
155
+ }
156
+ throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
157
+ }
158
+ async _closeBrowserContext(browserContext, userDataDir) {
159
+ testDebug('close browser context (persistent)');
160
+ testDebug('release user data dir', userDataDir);
161
+ await browserContext.close().catch(() => { });
162
+ this._userDataDirs.delete(userDataDir);
163
+ testDebug('close browser context complete (persistent)');
164
+ }
165
+ async _createUserDataDir() {
166
+ let cacheDirectory;
167
+ if (process.platform === 'linux')
168
+ cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
169
+ else if (process.platform === 'darwin')
170
+ cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
171
+ else if (process.platform === 'win32')
172
+ cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
173
+ else
174
+ throw new Error('Unsupported platform: ' + process.platform);
175
+ const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${this.browserConfig.launchOptions?.channel ?? this.browserConfig?.browserName}-profile`);
176
+ await fs.promises.mkdir(result, { recursive: true });
177
+ return result;
178
+ }
179
+ }
package/lib/config.js CHANGED
@@ -35,6 +35,7 @@ const defaultConfig = {
35
35
  allowedOrigins: undefined,
36
36
  blockedOrigins: undefined,
37
37
  },
38
+ server: {},
38
39
  outputDir: path.join(os.tmpdir(), 'playwright-mcp-output', sanitizeForFilePath(new Date().toISOString())),
39
40
  };
40
41
  export async function resolveConfig(config) {
@@ -47,6 +48,8 @@ export async function resolveCLIConfig(cliOptions) {
47
48
  // Derive artifact output directory from config.outputDir
48
49
  if (result.saveTrace)
49
50
  result.browser.launchOptions.tracesDir = path.join(result.outputDir, 'traces');
51
+ if (result.browser.browserName === 'chromium')
52
+ result.browser.launchOptions.cdpPort = await findFreePort();
50
53
  return result;
51
54
  }
52
55
  export async function configFromCLIOptions(cliOptions) {
@@ -71,9 +74,6 @@ export async function configFromCLIOptions(cliOptions) {
71
74
  case 'webkit':
72
75
  browserName = 'webkit';
73
76
  break;
74
- default:
75
- browserName = 'chromium';
76
- channel = 'chrome';
77
77
  }
78
78
  // Launch options
79
79
  const launchOptions = {
@@ -81,13 +81,9 @@ export async function configFromCLIOptions(cliOptions) {
81
81
  executablePath: cliOptions.executablePath,
82
82
  headless: cliOptions.headless,
83
83
  };
84
- if (browserName === 'chromium') {
85
- launchOptions.cdpPort = await findFreePort();
86
- if (!cliOptions.sandbox) {
87
- // --no-sandbox was passed, disable the sandbox
88
- launchOptions.chromiumSandbox = false;
89
- }
90
- }
84
+ // --no-sandbox was passed, disable the sandbox
85
+ if (!cliOptions.sandbox)
86
+ launchOptions.chromiumSandbox = false;
91
87
  if (cliOptions.proxyServer) {
92
88
  launchOptions.proxy = {
93
89
  server: cliOptions.proxyServer
@@ -137,11 +133,8 @@ export async function configFromCLIOptions(cliOptions) {
137
133
  },
138
134
  saveTrace: cliOptions.saveTrace,
139
135
  outputDir: cliOptions.outputDir,
136
+ imageResponses: cliOptions.imageResponses,
140
137
  };
141
- if (!cliOptions.imageResponses) {
142
- // --no-image-responses was passed, disable image responses
143
- result.noImageResponses = true;
144
- }
145
138
  return result;
146
139
  }
147
140
  async function findFreePort() {
@@ -198,6 +191,10 @@ function mergeConfig(base, overrides) {
198
191
  network: {
199
192
  ...pickDefined(base.network),
200
193
  ...pickDefined(overrides.network),
201
- }
194
+ },
195
+ server: {
196
+ ...pickDefined(base.server),
197
+ ...pickDefined(overrides.server),
198
+ },
202
199
  };
203
200
  }
package/lib/connection.js CHANGED
@@ -13,16 +13,17 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
16
+ import { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';
17
17
  import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
18
18
  import { zodToJsonSchema } from 'zod-to-json-schema';
19
- import { Context, packageJSON } from './context.js';
19
+ import { Context } from './context.js';
20
20
  import { snapshotTools, visionTools } from './tools.js';
21
- export async function createConnection(config) {
21
+ import { packageJSON } from './package.js';
22
+ export function createConnection(config, browserContextFactory) {
22
23
  const allTools = config.vision ? visionTools : snapshotTools;
23
24
  const tools = allTools.filter(tool => !config.capabilities || tool.capability === 'core' || config.capabilities.includes(tool.capability));
24
- const context = new Context(tools, config);
25
- const server = new Server({ name: 'Playwright', version: packageJSON.version }, {
25
+ const context = new Context(tools, config, browserContextFactory);
26
+ const server = new McpServer({ name: 'Playwright', version: packageJSON.version }, {
26
27
  capabilities: {
27
28
  tools: {},
28
29
  }
@@ -62,8 +63,7 @@ export async function createConnection(config) {
62
63
  return errorResult(String(error));
63
64
  }
64
65
  });
65
- const connection = new Connection(server, context);
66
- return connection;
66
+ return new Connection(server, context);
67
67
  }
68
68
  export class Connection {
69
69
  server;
@@ -71,14 +71,9 @@ export class Connection {
71
71
  constructor(server, context) {
72
72
  this.server = server;
73
73
  this.context = context;
74
- }
75
- async connect(transport) {
76
- await this.server.connect(transport);
77
- await new Promise(resolve => {
78
- this.server.oninitialized = () => resolve();
79
- });
80
- if (this.server.getClientVersion()?.name.includes('cursor'))
81
- this.context.config.noImageResponses = true;
74
+ this.server.oninitialized = () => {
75
+ this.context.clientVersion = this.server.getClientVersion();
76
+ };
82
77
  }
83
78
  async close() {
84
79
  await this.server.close();
package/lib/context.js CHANGED
@@ -13,27 +13,35 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import fs from 'node:fs';
17
- import url from 'node:url';
18
- import os from 'node:os';
19
- import path from 'node:path';
20
- import * as playwright from 'playwright';
16
+ import debug from 'debug';
21
17
  import { callOnPageNoTrace, waitForCompletion } from './tools/utils.js';
22
18
  import { ManualPromise } from './manualPromise.js';
23
19
  import { Tab } from './tab.js';
24
20
  import { outputFile } from './config.js';
21
+ const testDebug = debug('pw:mcp:test');
25
22
  export class Context {
26
23
  tools;
27
24
  config;
28
25
  _browserContextPromise;
26
+ _browserContextFactory;
29
27
  _tabs = [];
30
28
  _currentTab;
31
29
  _modalStates = [];
32
30
  _pendingAction;
33
31
  _downloads = [];
34
- constructor(tools, config) {
32
+ clientVersion;
33
+ constructor(tools, config, browserContextFactory) {
35
34
  this.tools = tools;
36
35
  this.config = config;
36
+ this._browserContextFactory = browserContextFactory;
37
+ testDebug('create context');
38
+ }
39
+ clientSupportsImages() {
40
+ if (this.config.imageResponses === 'allow')
41
+ return true;
42
+ if (this.config.imageResponses === 'omit')
43
+ return false;
44
+ return !this.clientVersion?.name.includes('cursor');
37
45
  }
38
46
  modalStates() {
39
47
  return this._modalStates;
@@ -233,14 +241,13 @@ ${code.join('\n')}
233
241
  async close() {
234
242
  if (!this._browserContextPromise)
235
243
  return;
244
+ testDebug('close context');
236
245
  const promise = this._browserContextPromise;
237
246
  this._browserContextPromise = undefined;
238
- await promise.then(async ({ browserContext, browser }) => {
247
+ await promise.then(async ({ browserContext, close }) => {
239
248
  if (this.config.saveTrace)
240
249
  await browserContext.tracing.stop();
241
- await browserContext.close().then(async () => {
242
- await browser?.close();
243
- }).catch(() => { });
250
+ await close();
244
251
  });
245
252
  }
246
253
  async _setupRequestInterception(context) {
@@ -264,7 +271,9 @@ ${code.join('\n')}
264
271
  return this._browserContextPromise;
265
272
  }
266
273
  async _setupBrowserContext() {
267
- const { browser, browserContext } = await this._createBrowserContext();
274
+ // TODO: move to the browser context factory to make it based on isolation mode.
275
+ const result = await this._browserContextFactory.createContext();
276
+ const { browserContext } = result;
268
277
  await this._setupRequestInterception(browserContext);
269
278
  for (const page of browserContext.pages())
270
279
  this._onPageCreated(page);
@@ -277,70 +286,6 @@ ${code.join('\n')}
277
286
  sources: false,
278
287
  });
279
288
  }
280
- return { browser, browserContext };
281
- }
282
- async _createBrowserContext() {
283
- if (this.config.browser?.remoteEndpoint) {
284
- const url = new URL(this.config.browser?.remoteEndpoint);
285
- if (this.config.browser.browserName)
286
- url.searchParams.set('browser', this.config.browser.browserName);
287
- if (this.config.browser.launchOptions)
288
- url.searchParams.set('launch-options', JSON.stringify(this.config.browser.launchOptions));
289
- const browser = await playwright[this.config.browser?.browserName ?? 'chromium'].connect(String(url));
290
- const browserContext = await browser.newContext();
291
- return { browser, browserContext };
292
- }
293
- if (this.config.browser?.cdpEndpoint) {
294
- const browser = await playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint);
295
- const browserContext = this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
296
- return { browser, browserContext };
297
- }
298
- return this.config.browser?.isolated ?
299
- await createIsolatedContext(this.config.browser) :
300
- await launchPersistentContext(this.config.browser);
301
- }
302
- }
303
- async function createIsolatedContext(browserConfig) {
304
- try {
305
- const browserName = browserConfig?.browserName ?? 'chromium';
306
- const browserType = playwright[browserName];
307
- const browser = await browserType.launch(browserConfig.launchOptions);
308
- const browserContext = await browser.newContext(browserConfig.contextOptions);
309
- return { browser, browserContext };
310
- }
311
- catch (error) {
312
- if (error.message.includes('Executable doesn\'t exist'))
313
- throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
314
- throw error;
315
- }
316
- }
317
- async function launchPersistentContext(browserConfig) {
318
- try {
319
- const browserName = browserConfig.browserName ?? 'chromium';
320
- const userDataDir = browserConfig.userDataDir ?? await createUserDataDir({ ...browserConfig, browserName });
321
- const browserType = playwright[browserName];
322
- const browserContext = await browserType.launchPersistentContext(userDataDir, { ...browserConfig.launchOptions, ...browserConfig.contextOptions });
323
- return { browserContext };
324
- }
325
- catch (error) {
326
- if (error.message.includes('Executable doesn\'t exist'))
327
- throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
328
- throw error;
289
+ return result;
329
290
  }
330
291
  }
331
- async function createUserDataDir(browserConfig) {
332
- let cacheDirectory;
333
- if (process.platform === 'linux')
334
- cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
335
- else if (process.platform === 'darwin')
336
- cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
337
- else if (process.platform === 'win32')
338
- cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
339
- else
340
- throw new Error('Unsupported platform: ' + process.platform);
341
- const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${browserConfig.launchOptions?.channel ?? browserConfig?.browserName}-profile`);
342
- await fs.promises.mkdir(result, { recursive: true });
343
- return result;
344
- }
345
- const __filename = url.fileURLToPath(import.meta.url);
346
- export const packageJSON = JSON.parse(fs.readFileSync(path.join(path.dirname(__filename), '..', 'package.json'), 'utf8'));
package/lib/index.js CHANGED
@@ -15,7 +15,22 @@
15
15
  */
16
16
  import { createConnection as createConnectionImpl } from './connection.js';
17
17
  import { resolveConfig } from './config.js';
18
- export async function createConnection(userConfig = {}) {
18
+ import { contextFactory } from './browserContextFactory.js';
19
+ export async function createConnection(userConfig = {}, contextGetter) {
19
20
  const config = await resolveConfig(userConfig);
20
- return createConnectionImpl(config);
21
+ const factory = contextGetter ? new SimpleBrowserContextFactory(contextGetter) : contextFactory(config.browser);
22
+ return createConnectionImpl(config, factory);
23
+ }
24
+ class SimpleBrowserContextFactory {
25
+ _contextGetter;
26
+ constructor(contextGetter) {
27
+ this._contextGetter = contextGetter;
28
+ }
29
+ async createContext() {
30
+ const browserContext = await this._contextGetter();
31
+ return {
32
+ browserContext,
33
+ close: () => browserContext.close()
34
+ };
35
+ }
21
36
  }
package/lib/package.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import fs from 'node:fs';
17
+ import url from 'node:url';
18
+ import path from 'node:path';
19
+ const __filename = url.fileURLToPath(import.meta.url);
20
+ export const packageJSON = JSON.parse(fs.readFileSync(path.join(path.dirname(__filename), '..', 'package.json'), 'utf8'));
@@ -29,9 +29,6 @@ export class PageSnapshot {
29
29
  return this._text;
30
30
  }
31
31
  async _build() {
32
- // FIXME: Rountrip evaluate to ensure _snapshotForAI works.
33
- // This probably broke once we moved off locator snapshots
34
- await this._page.evaluate(() => 1);
35
32
  const snapshot = await callOnPageNoTrace(this._page, page => page._snapshotForAI());
36
33
  this._text = [
37
34
  `- Page Snapshot`,
@@ -40,7 +37,7 @@ export class PageSnapshot {
40
37
  '```',
41
38
  ].join('\n');
42
39
  }
43
- refLocator(ref) {
44
- return this._page.locator(`aria-ref=${ref}`);
40
+ refLocator(params) {
41
+ return this._page.locator(`aria-ref=${params.ref}`).describe(params.element);
45
42
  }
46
43
  }
package/lib/program.js CHANGED
@@ -14,11 +14,12 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { program } from 'commander';
17
- import { startHttpTransport, startStdioTransport } from './transport.js';
18
- import { resolveCLIConfig } from './config.js';
19
17
  // @ts-ignore
20
18
  import { startTraceViewerServer } from 'playwright-core/lib/server';
21
- import { packageJSON } from './context.js';
19
+ import { startHttpTransport, startStdioTransport } from './transport.js';
20
+ import { resolveCLIConfig } from './config.js';
21
+ import { Server } from './server.js';
22
+ import { packageJSON } from './package.js';
22
23
  program
23
24
  .version('Version ' + packageJSON.version)
24
25
  .name(packageJSON.name)
@@ -35,7 +36,7 @@ program
35
36
  .option('--host <host>', 'host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
36
37
  .option('--ignore-https-errors', 'ignore https errors')
37
38
  .option('--isolated', 'keep the browser profile in memory, do not save it to disk.')
38
- .option('--no-image-responses', 'do not send image responses to the client.')
39
+ .option('--image-responses <mode>', 'whether to send image responses to the client. Can be "allow", "omit", or "auto". Defaults to "auto", which sends images if the client can display them.')
39
40
  .option('--no-sandbox', 'disable the sandbox for all process types that are normally sandboxed.')
40
41
  .option('--output-dir <path>', 'path to the directory for output files.')
41
42
  .option('--port <port>', 'port to listen on for SSE transport.')
@@ -49,12 +50,12 @@ program
49
50
  .option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
50
51
  .action(async (options) => {
51
52
  const config = await resolveCLIConfig(options);
52
- const connectionList = [];
53
- setupExitWatchdog(connectionList);
54
- if (options.port)
55
- startHttpTransport(config, +options.port, options.host, connectionList);
53
+ const server = new Server(config);
54
+ server.setupExitWatchdog();
55
+ if (config.server.port !== undefined)
56
+ startHttpTransport(server);
56
57
  else
57
- await startStdioTransport(config, connectionList);
58
+ await startStdioTransport(server);
58
59
  if (config.saveTrace) {
59
60
  const server = await startTraceViewerServer();
60
61
  const urlPrefix = server.urlPrefix('human-readable');
@@ -63,18 +64,7 @@ program
63
64
  console.error('\nTrace viewer listening on ' + url);
64
65
  }
65
66
  });
66
- function setupExitWatchdog(connectionList) {
67
- const handleExit = async () => {
68
- setTimeout(() => process.exit(0), 15000);
69
- for (const connection of connectionList)
70
- await connection.close();
71
- process.exit(0);
72
- };
73
- process.stdin.on('close', handleExit);
74
- process.on('SIGINT', handleExit);
75
- process.on('SIGTERM', handleExit);
76
- }
77
67
  function semicolonSeparatedList(value) {
78
68
  return value.split(';').map(v => v.trim());
79
69
  }
80
- program.parse(process.argv);
70
+ void program.parseAsync(process.argv);
package/lib/server.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { createConnection } from './connection.js';
17
+ import { contextFactory } from './browserContextFactory.js';
18
+ export class Server {
19
+ config;
20
+ _connectionList = [];
21
+ _browserConfig;
22
+ _contextFactory;
23
+ constructor(config) {
24
+ this.config = config;
25
+ this._browserConfig = config.browser;
26
+ this._contextFactory = contextFactory(this._browserConfig);
27
+ }
28
+ async createConnection(transport) {
29
+ const connection = createConnection(this.config, this._contextFactory);
30
+ this._connectionList.push(connection);
31
+ await connection.server.connect(transport);
32
+ return connection;
33
+ }
34
+ setupExitWatchdog() {
35
+ let isExiting = false;
36
+ const handleExit = async () => {
37
+ if (isExiting)
38
+ return;
39
+ isExiting = true;
40
+ setTimeout(() => process.exit(0), 15000);
41
+ await Promise.all(this._connectionList.map(connection => connection.close()));
42
+ process.exit(0);
43
+ };
44
+ process.stdin.on('close', handleExit);
45
+ process.on('SIGINT', handleExit);
46
+ process.on('SIGTERM', handleExit);
47
+ }
48
+ }
@@ -48,12 +48,12 @@ const screenshot = defineTool({
48
48
  const code = [
49
49
  `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
50
50
  ];
51
- const locator = params.ref ? snapshot.refLocator(params.ref) : null;
51
+ const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null;
52
52
  if (locator)
53
53
  code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
54
54
  else
55
55
  code.push(`await page.screenshot(${javascript.formatObject(options)});`);
56
- const includeBase64 = !context.config.noImageResponses;
56
+ const includeBase64 = context.clientSupportsImages();
57
57
  const action = async () => {
58
58
  const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
59
59
  return {
@@ -50,7 +50,7 @@ const click = defineTool({
50
50
  },
51
51
  handle: async (context, params) => {
52
52
  const tab = context.currentTabOrDie();
53
- const locator = tab.snapshotOrDie().refLocator(params.ref);
53
+ const locator = tab.snapshotOrDie().refLocator(params);
54
54
  const code = [
55
55
  `// Click ${params.element}`,
56
56
  `await page.${await generateLocator(locator)}.click();`
@@ -79,8 +79,8 @@ const drag = defineTool({
79
79
  },
80
80
  handle: async (context, params) => {
81
81
  const snapshot = context.currentTabOrDie().snapshotOrDie();
82
- const startLocator = snapshot.refLocator(params.startRef);
83
- const endLocator = snapshot.refLocator(params.endRef);
82
+ const startLocator = snapshot.refLocator({ ref: params.startRef, element: params.startElement });
83
+ const endLocator = snapshot.refLocator({ ref: params.endRef, element: params.endElement });
84
84
  const code = [
85
85
  `// Drag ${params.startElement} to ${params.endElement}`,
86
86
  `await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`
@@ -104,7 +104,7 @@ const hover = defineTool({
104
104
  },
105
105
  handle: async (context, params) => {
106
106
  const snapshot = context.currentTabOrDie().snapshotOrDie();
107
- const locator = snapshot.refLocator(params.ref);
107
+ const locator = snapshot.refLocator(params);
108
108
  const code = [
109
109
  `// Hover over ${params.element}`,
110
110
  `await page.${await generateLocator(locator)}.hover();`
@@ -133,7 +133,7 @@ const type = defineTool({
133
133
  },
134
134
  handle: async (context, params) => {
135
135
  const snapshot = context.currentTabOrDie().snapshotOrDie();
136
- const locator = snapshot.refLocator(params.ref);
136
+ const locator = snapshot.refLocator(params);
137
137
  const code = [];
138
138
  const steps = [];
139
139
  if (params.slowly) {
@@ -173,7 +173,7 @@ const selectOption = defineTool({
173
173
  },
174
174
  handle: async (context, params) => {
175
175
  const snapshot = context.currentTabOrDie().snapshotOrDie();
176
- const locator = snapshot.refLocator(params.ref);
176
+ const locator = snapshot.refLocator(params);
177
177
  const code = [
178
178
  `// Select options [${params.values.join(', ')}] in ${params.element}`,
179
179
  `await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`
@@ -66,8 +66,8 @@ export function sanitizeForFilePath(s) {
66
66
  return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
67
67
  }
68
68
  export async function generateLocator(locator) {
69
- return locator._frame._wrapApiCall(() => locator._generateLocatorString(), true);
69
+ return locator._generateLocatorString();
70
70
  }
71
71
  export async function callOnPageNoTrace(page, callback) {
72
- return await page._wrapApiCall(() => callback(page), true);
72
+ return await page._wrapApiCall(() => callback(page), { internal: true });
73
73
  }
package/lib/transport.js CHANGED
@@ -16,16 +16,15 @@
16
16
  import http from 'node:http';
17
17
  import assert from 'node:assert';
18
18
  import crypto from 'node:crypto';
19
+ import debug from 'debug';
19
20
  import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
20
21
  import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
21
22
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
22
- import { createConnection } from './connection.js';
23
- export async function startStdioTransport(config, connectionList) {
24
- const connection = await createConnection(config);
25
- await connection.connect(new StdioServerTransport());
26
- connectionList.push(connection);
23
+ export async function startStdioTransport(server) {
24
+ await server.createConnection(new StdioServerTransport());
27
25
  }
28
- async function handleSSE(config, req, res, url, sessions, connectionList) {
26
+ const testDebug = debug('pw:mcp:test');
27
+ async function handleSSE(server, req, res, url, sessions) {
29
28
  if (req.method === 'POST') {
30
29
  const sessionId = url.searchParams.get('sessionId');
31
30
  if (!sessionId) {
@@ -42,22 +41,20 @@ async function handleSSE(config, req, res, url, sessions, connectionList) {
42
41
  else if (req.method === 'GET') {
43
42
  const transport = new SSEServerTransport('/sse', res);
44
43
  sessions.set(transport.sessionId, transport);
45
- const connection = await createConnection(config);
46
- await connection.connect(transport);
47
- connectionList.push(connection);
44
+ testDebug(`create SSE session: ${transport.sessionId}`);
45
+ const connection = await server.createConnection(transport);
48
46
  res.on('close', () => {
47
+ testDebug(`delete SSE session: ${transport.sessionId}`);
49
48
  sessions.delete(transport.sessionId);
50
- connection.close().catch(e => {
51
- // eslint-disable-next-line no-console
52
- console.error(e);
53
- });
49
+ // eslint-disable-next-line no-console
50
+ void connection.close().catch(e => console.error(e));
54
51
  });
55
52
  return;
56
53
  }
57
54
  res.statusCode = 405;
58
55
  res.end('Method not allowed');
59
56
  }
60
- async function handleStreamable(config, req, res, sessions, connectionList) {
57
+ async function handleStreamable(server, req, res, sessions) {
61
58
  const sessionId = req.headers['mcp-session-id'];
62
59
  if (sessionId) {
63
60
  const transport = sessions.get(sessionId);
@@ -79,28 +76,25 @@ async function handleStreamable(config, req, res, sessions, connectionList) {
79
76
  if (transport.sessionId)
80
77
  sessions.delete(transport.sessionId);
81
78
  };
82
- const connection = await createConnection(config);
83
- connectionList.push(connection);
84
- await Promise.all([
85
- connection.connect(transport),
86
- transport.handleRequest(req, res),
87
- ]);
79
+ await server.createConnection(transport);
80
+ await transport.handleRequest(req, res);
88
81
  return;
89
82
  }
90
83
  res.statusCode = 400;
91
84
  res.end('Invalid request');
92
85
  }
93
- export function startHttpTransport(config, port, hostname, connectionList) {
86
+ export function startHttpTransport(server) {
94
87
  const sseSessions = new Map();
95
88
  const streamableSessions = new Map();
96
89
  const httpServer = http.createServer(async (req, res) => {
97
90
  const url = new URL(`http://localhost${req.url}`);
98
91
  if (url.pathname.startsWith('/mcp'))
99
- await handleStreamable(config, req, res, streamableSessions, connectionList);
92
+ await handleStreamable(server, req, res, streamableSessions);
100
93
  else
101
- await handleSSE(config, req, res, url, sseSessions, connectionList);
94
+ await handleSSE(server, req, res, url, sseSessions);
102
95
  });
103
- httpServer.listen(port, hostname, () => {
96
+ const { host, port } = server.config.server;
97
+ httpServer.listen(port, host, () => {
104
98
  const address = httpServer.address();
105
99
  assert(address, 'Could not bind server socket');
106
100
  let url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playwright/mcp",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "Playwright Tools for MCP",
5
5
  "type": "module",
6
6
  "repository": {
@@ -37,14 +37,16 @@
37
37
  "dependencies": {
38
38
  "@modelcontextprotocol/sdk": "^1.11.0",
39
39
  "commander": "^13.1.0",
40
- "playwright": "1.53.0-alpha-1746832516000",
40
+ "debug": "^4.4.1",
41
+ "playwright": "1.53.0-alpha-2025-05-27",
41
42
  "zod-to-json-schema": "^3.24.4"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@eslint/eslintrc": "^3.2.0",
45
46
  "@eslint/js": "^9.19.0",
46
- "@playwright/test": "1.53.0-alpha-1746832516000",
47
+ "@playwright/test": "1.53.0-alpha-2025-05-27",
47
48
  "@stylistic/eslint-plugin": "^3.0.1",
49
+ "@types/debug": "^4.1.12",
48
50
  "@types/node": "^22.13.10",
49
51
  "@typescript-eslint/eslint-plugin": "^8.26.1",
50
52
  "@typescript-eslint/parser": "^8.26.1",