@szymonrybczak/playwright-mcp 0.0.2 → 0.0.5

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.
Files changed (115) hide show
  1. package/index.d.ts +20 -8
  2. package/lib/cjs/_virtual/_commonjsHelpers.js +9 -0
  3. package/lib/cjs/_virtual/browser.js +11 -0
  4. package/lib/cjs/_virtual/browser2.js +7 -0
  5. package/lib/cjs/cloudflare/package.json.js +10 -0
  6. package/lib/cjs/index.js +27 -0
  7. package/lib/cjs/node_modules/debug/src/browser.js +290 -0
  8. package/lib/cjs/node_modules/debug/src/common.js +307 -0
  9. package/lib/cjs/node_modules/ms/index.js +176 -0
  10. package/lib/cjs/package.js +9 -0
  11. package/lib/cjs/src/browserContextFactory.js +240 -0
  12. package/lib/cjs/src/config.js +77 -0
  13. package/lib/cjs/src/connection.js +74 -0
  14. package/lib/cjs/src/context.js +281 -0
  15. package/lib/cjs/src/fileUtils.js +25 -0
  16. package/lib/cjs/src/index.js +15 -0
  17. package/lib/cjs/src/javascript.js +41 -0
  18. package/lib/cjs/src/manualPromise.js +39 -0
  19. package/lib/cjs/src/pageSnapshot.js +35 -0
  20. package/lib/cjs/src/tab.js +91 -0
  21. package/lib/cjs/src/tools/common.js +60 -0
  22. package/lib/cjs/src/tools/console.js +36 -0
  23. package/lib/cjs/src/tools/dialogs.js +44 -0
  24. package/lib/cjs/src/tools/files.js +43 -0
  25. package/lib/cjs/src/tools/install.js +49 -0
  26. package/lib/cjs/src/tools/keyboard.js +38 -0
  27. package/lib/cjs/src/tools/navigate.js +85 -0
  28. package/lib/cjs/src/tools/network.js +43 -0
  29. package/lib/cjs/src/tools/pdf.js +42 -0
  30. package/lib/cjs/src/tools/screenshot.js +69 -0
  31. package/lib/cjs/src/tools/snapshot.js +195 -0
  32. package/lib/cjs/src/tools/tabs.js +110 -0
  33. package/lib/cjs/src/tools/testing.js +52 -0
  34. package/lib/cjs/src/tools/tool.js +9 -0
  35. package/lib/cjs/src/tools/utils.js +75 -0
  36. package/lib/cjs/src/tools/vision.js +181 -0
  37. package/lib/cjs/src/tools/wait.js +51 -0
  38. package/lib/cjs/src/tools.js +54 -0
  39. package/lib/esm/_virtual/_commonjsHelpers.js +5 -0
  40. package/lib/esm/_virtual/browser.js +7 -0
  41. package/lib/esm/_virtual/browser2.js +3 -0
  42. package/lib/esm/cloudflare/package.json.js +5 -0
  43. package/lib/esm/index.js +23 -0
  44. package/lib/esm/node_modules/debug/src/browser.js +286 -0
  45. package/lib/esm/node_modules/debug/src/common.js +303 -0
  46. package/lib/esm/node_modules/ms/index.js +172 -0
  47. package/lib/esm/package.js +5 -0
  48. package/lib/esm/src/browserContextFactory.js +216 -0
  49. package/lib/esm/src/config.js +72 -0
  50. package/lib/esm/src/connection.js +69 -0
  51. package/lib/esm/src/context.js +277 -0
  52. package/lib/esm/src/fileUtils.js +20 -0
  53. package/lib/esm/src/index.js +11 -0
  54. package/lib/esm/src/javascript.js +35 -0
  55. package/lib/esm/src/manualPromise.js +35 -0
  56. package/lib/esm/src/pageSnapshot.js +31 -0
  57. package/lib/esm/src/tab.js +87 -0
  58. package/lib/esm/src/tools/common.js +56 -0
  59. package/lib/esm/src/tools/console.js +32 -0
  60. package/lib/esm/src/tools/dialogs.js +40 -0
  61. package/lib/esm/src/tools/files.js +39 -0
  62. package/lib/esm/src/tools/install.js +45 -0
  63. package/lib/esm/src/tools/keyboard.js +34 -0
  64. package/lib/esm/src/tools/navigate.js +81 -0
  65. package/lib/esm/src/tools/network.js +39 -0
  66. package/lib/esm/src/tools/pdf.js +38 -0
  67. package/lib/esm/src/tools/screenshot.js +65 -0
  68. package/lib/esm/src/tools/snapshot.js +191 -0
  69. package/lib/esm/src/tools/tabs.js +106 -0
  70. package/lib/esm/src/tools/testing.js +48 -0
  71. package/lib/esm/src/tools/tool.js +5 -0
  72. package/lib/esm/src/tools/utils.js +68 -0
  73. package/lib/esm/src/tools/vision.js +177 -0
  74. package/lib/esm/src/tools/wait.js +47 -0
  75. package/lib/esm/src/tools.js +49 -0
  76. package/package.json +15 -44
  77. package/LICENSE +0 -202
  78. package/README.md +0 -508
  79. package/cli.js +0 -18
  80. package/config.d.ts +0 -128
  81. package/index.js +0 -19
  82. package/lib/browserContextFactory.js +0 -227
  83. package/lib/browserServer.js +0 -151
  84. package/lib/config.js +0 -189
  85. package/lib/connection.js +0 -82
  86. package/lib/context.js +0 -291
  87. package/lib/fileUtils.js +0 -32
  88. package/lib/httpServer.js +0 -201
  89. package/lib/index.js +0 -36
  90. package/lib/javascript.js +0 -49
  91. package/lib/manualPromise.js +0 -111
  92. package/lib/package.js +0 -20
  93. package/lib/pageSnapshot.js +0 -43
  94. package/lib/program.js +0 -72
  95. package/lib/server.js +0 -48
  96. package/lib/tab.js +0 -101
  97. package/lib/tools/common.js +0 -68
  98. package/lib/tools/console.js +0 -44
  99. package/lib/tools/dialogs.js +0 -52
  100. package/lib/tools/files.js +0 -51
  101. package/lib/tools/install.js +0 -57
  102. package/lib/tools/keyboard.js +0 -46
  103. package/lib/tools/navigate.js +0 -93
  104. package/lib/tools/network.js +0 -51
  105. package/lib/tools/pdf.js +0 -49
  106. package/lib/tools/screenshot.js +0 -77
  107. package/lib/tools/snapshot.js +0 -204
  108. package/lib/tools/tabs.js +0 -118
  109. package/lib/tools/testing.js +0 -60
  110. package/lib/tools/tool.js +0 -18
  111. package/lib/tools/utils.js +0 -80
  112. package/lib/tools/vision.js +0 -189
  113. package/lib/tools/wait.js +0 -59
  114. package/lib/tools.js +0 -61
  115. package/lib/transport.js +0 -133
@@ -1,227 +0,0 @@
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 net from 'node:net';
18
- import path from 'node:path';
19
- import os from 'node:os';
20
- import debug from 'debug';
21
- import * as playwright from '@szymonrybczak/patchright';
22
- import { userDataDir } from './fileUtils.js';
23
- const testDebug = debug('pw:mcp:test');
24
- export function contextFactory(browserConfig) {
25
- if (browserConfig.remoteEndpoint)
26
- return new RemoteContextFactory(browserConfig);
27
- if (browserConfig.cdpEndpoint)
28
- return new CdpContextFactory(browserConfig);
29
- if (browserConfig.isolated)
30
- return new IsolatedContextFactory(browserConfig);
31
- if (browserConfig.browserAgent)
32
- return new BrowserServerContextFactory(browserConfig);
33
- return new PersistentContextFactory(browserConfig);
34
- }
35
- class BaseContextFactory {
36
- browserConfig;
37
- _browserPromise;
38
- name;
39
- constructor(name, browserConfig) {
40
- this.name = name;
41
- this.browserConfig = browserConfig;
42
- }
43
- async _obtainBrowser() {
44
- if (this._browserPromise)
45
- return this._browserPromise;
46
- testDebug(`obtain browser (${this.name})`);
47
- this._browserPromise = this._doObtainBrowser();
48
- void this._browserPromise.then(browser => {
49
- browser.on('disconnected', () => {
50
- this._browserPromise = undefined;
51
- });
52
- }).catch(() => {
53
- this._browserPromise = undefined;
54
- });
55
- return this._browserPromise;
56
- }
57
- async _doObtainBrowser() {
58
- throw new Error('Not implemented');
59
- }
60
- async createContext() {
61
- testDebug(`create browser context (${this.name})`);
62
- const browser = await this._obtainBrowser();
63
- const browserContext = await this._doCreateContext(browser);
64
- return { browserContext, close: () => this._closeBrowserContext(browserContext, browser) };
65
- }
66
- async _doCreateContext(browser) {
67
- throw new Error('Not implemented');
68
- }
69
- async _closeBrowserContext(browserContext, browser) {
70
- testDebug(`close browser context (${this.name})`);
71
- if (browser.contexts().length === 1)
72
- this._browserPromise = undefined;
73
- await browserContext.close().catch(() => { });
74
- if (browser.contexts().length === 0) {
75
- testDebug(`close browser (${this.name})`);
76
- await browser.close().catch(() => { });
77
- }
78
- }
79
- }
80
- class IsolatedContextFactory extends BaseContextFactory {
81
- constructor(browserConfig) {
82
- super('isolated', browserConfig);
83
- }
84
- async _doObtainBrowser() {
85
- await injectCdpPort(this.browserConfig);
86
- const browserType = playwright[this.browserConfig.browserName];
87
- return browserType.launch({
88
- ...this.browserConfig.launchOptions,
89
- handleSIGINT: false,
90
- handleSIGTERM: false,
91
- }).catch(error => {
92
- if (error.message.includes('Executable doesn\'t exist'))
93
- throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
94
- throw error;
95
- });
96
- }
97
- async _doCreateContext(browser) {
98
- return browser.newContext(this.browserConfig.contextOptions);
99
- }
100
- }
101
- class CdpContextFactory extends BaseContextFactory {
102
- constructor(browserConfig) {
103
- super('cdp', browserConfig);
104
- }
105
- async _doObtainBrowser() {
106
- return playwright.chromium.connectOverCDP(this.browserConfig.cdpEndpoint);
107
- }
108
- async _doCreateContext(browser) {
109
- return this.browserConfig.isolated ? await browser.newContext() : browser.contexts()[0];
110
- }
111
- }
112
- class RemoteContextFactory extends BaseContextFactory {
113
- constructor(browserConfig) {
114
- super('remote', browserConfig);
115
- }
116
- async _doObtainBrowser() {
117
- const url = new URL(this.browserConfig.remoteEndpoint);
118
- url.searchParams.set('browser', this.browserConfig.browserName);
119
- if (this.browserConfig.launchOptions)
120
- url.searchParams.set('launch-options', JSON.stringify(this.browserConfig.launchOptions));
121
- return playwright[this.browserConfig.browserName].connect(String(url));
122
- }
123
- async _doCreateContext(browser) {
124
- return browser.newContext();
125
- }
126
- }
127
- class PersistentContextFactory {
128
- browserConfig;
129
- _userDataDirs = new Set();
130
- constructor(browserConfig) {
131
- this.browserConfig = browserConfig;
132
- }
133
- async createContext() {
134
- await injectCdpPort(this.browserConfig);
135
- testDebug('create browser context (persistent)');
136
- const userDataDir = this.browserConfig.userDataDir ?? await this._createUserDataDir();
137
- this._userDataDirs.add(userDataDir);
138
- testDebug('lock user data dir', userDataDir);
139
- const browserType = playwright[this.browserConfig.browserName];
140
- for (let i = 0; i < 5; i++) {
141
- try {
142
- const browserContext = await browserType.launchPersistentContext(userDataDir, {
143
- ...this.browserConfig.launchOptions,
144
- ...this.browserConfig.contextOptions,
145
- handleSIGINT: false,
146
- handleSIGTERM: false,
147
- });
148
- const close = () => this._closeBrowserContext(browserContext, userDataDir);
149
- return { browserContext, close };
150
- }
151
- catch (error) {
152
- if (error.message.includes('Executable doesn\'t exist'))
153
- throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
154
- if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
155
- // User data directory is already in use, try again.
156
- await new Promise(resolve => setTimeout(resolve, 1000));
157
- continue;
158
- }
159
- throw error;
160
- }
161
- }
162
- throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
163
- }
164
- async _closeBrowserContext(browserContext, userDataDir) {
165
- testDebug('close browser context (persistent)');
166
- testDebug('release user data dir', userDataDir);
167
- await browserContext.close().catch(() => { });
168
- this._userDataDirs.delete(userDataDir);
169
- testDebug('close browser context complete (persistent)');
170
- }
171
- async _createUserDataDir() {
172
- let cacheDirectory;
173
- if (process.platform === 'linux')
174
- cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
175
- else if (process.platform === 'darwin')
176
- cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
177
- else if (process.platform === 'win32')
178
- cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
179
- else
180
- throw new Error('Unsupported platform: ' + process.platform);
181
- const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${this.browserConfig.launchOptions?.channel ?? this.browserConfig?.browserName}-profile`);
182
- await fs.promises.mkdir(result, { recursive: true });
183
- return result;
184
- }
185
- }
186
- export class BrowserServerContextFactory extends BaseContextFactory {
187
- constructor(browserConfig) {
188
- super('persistent', browserConfig);
189
- }
190
- async _doObtainBrowser() {
191
- const response = await fetch(new URL(`/json/launch`, this.browserConfig.browserAgent), {
192
- method: 'POST',
193
- body: JSON.stringify({
194
- browserType: this.browserConfig.browserName,
195
- userDataDir: this.browserConfig.userDataDir ?? await this._createUserDataDir(),
196
- launchOptions: this.browserConfig.launchOptions,
197
- contextOptions: this.browserConfig.contextOptions,
198
- }),
199
- });
200
- const info = await response.json();
201
- if (info.error)
202
- throw new Error(info.error);
203
- return await playwright.chromium.connectOverCDP(`http://localhost:${info.cdpPort}/`);
204
- }
205
- async _doCreateContext(browser) {
206
- return this.browserConfig.isolated ? await browser.newContext() : browser.contexts()[0];
207
- }
208
- async _createUserDataDir() {
209
- const dir = await userDataDir(this.browserConfig);
210
- await fs.promises.mkdir(dir, { recursive: true });
211
- return dir;
212
- }
213
- }
214
- async function injectCdpPort(browserConfig) {
215
- if (browserConfig.browserName === 'chromium')
216
- browserConfig.launchOptions.cdpPort = await findFreePort();
217
- }
218
- async function findFreePort() {
219
- return new Promise((resolve, reject) => {
220
- const server = net.createServer();
221
- server.listen(0, () => {
222
- const { port } = server.address();
223
- server.close(() => resolve(port));
224
- });
225
- server.on('error', reject);
226
- });
227
- }
@@ -1,151 +0,0 @@
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
- /* eslint-disable no-console */
17
- import net from 'net';
18
- import { program } from 'commander';
19
- import playwright from '@szymonrybczak/patchright';
20
- import { HttpServer } from './httpServer.js';
21
- import { packageJSON } from './package.js';
22
- class BrowserServer {
23
- _server = new HttpServer();
24
- _entries = [];
25
- constructor() {
26
- this._setupExitHandler();
27
- }
28
- async start(port) {
29
- await this._server.start({ port });
30
- this._server.routePath('/json/list', (req, res) => {
31
- this._handleJsonList(res);
32
- });
33
- this._server.routePath('/json/launch', async (req, res) => {
34
- void this._handleLaunchBrowser(req, res).catch(e => console.error(e));
35
- });
36
- this._setEntries([]);
37
- }
38
- _handleJsonList(res) {
39
- const list = this._entries.map(browser => browser.info);
40
- res.end(JSON.stringify(list));
41
- }
42
- async _handleLaunchBrowser(req, res) {
43
- const request = await readBody(req);
44
- let info = this._entries.map(entry => entry.info).find(info => info.userDataDir === request.userDataDir);
45
- if (!info || info.error)
46
- info = await this._newBrowser(request);
47
- res.end(JSON.stringify(info));
48
- }
49
- async _newBrowser(request) {
50
- const cdpPort = await findFreePort();
51
- request.launchOptions.cdpPort = cdpPort;
52
- const info = {
53
- browserType: request.browserType,
54
- userDataDir: request.userDataDir,
55
- cdpPort,
56
- launchOptions: request.launchOptions,
57
- contextOptions: request.contextOptions,
58
- };
59
- const browserType = playwright[request.browserType];
60
- const { browser, error } = await browserType.launchPersistentContext(request.userDataDir, {
61
- ...request.launchOptions,
62
- ...request.contextOptions,
63
- handleSIGINT: false,
64
- handleSIGTERM: false,
65
- }).then(context => {
66
- return { browser: context.browser(), error: undefined };
67
- }).catch(error => {
68
- return { browser: undefined, error: error.message };
69
- });
70
- this._setEntries([...this._entries, {
71
- browser,
72
- info: {
73
- browserType: request.browserType,
74
- userDataDir: request.userDataDir,
75
- cdpPort,
76
- launchOptions: request.launchOptions,
77
- contextOptions: request.contextOptions,
78
- error,
79
- },
80
- }]);
81
- browser?.on('disconnected', () => {
82
- this._setEntries(this._entries.filter(entry => entry.browser !== browser));
83
- });
84
- return info;
85
- }
86
- _updateReport() {
87
- // Clear the current line and move cursor to top of screen
88
- process.stdout.write('\x1b[2J\x1b[H');
89
- process.stdout.write(`Playwright Browser Server v${packageJSON.version}\n`);
90
- process.stdout.write(`Listening on ${this._server.urlPrefix('human-readable')}\n\n`);
91
- if (this._entries.length === 0) {
92
- process.stdout.write('No browsers currently running\n');
93
- return;
94
- }
95
- process.stdout.write('Running browsers:\n');
96
- for (const entry of this._entries) {
97
- const status = entry.browser ? 'running' : 'error';
98
- const statusColor = entry.browser ? '\x1b[32m' : '\x1b[31m'; // green for running, red for error
99
- process.stdout.write(`${statusColor}${entry.info.browserType}\x1b[0m (${entry.info.userDataDir}) - ${statusColor}${status}\x1b[0m\n`);
100
- if (entry.info.error)
101
- process.stdout.write(` Error: ${entry.info.error}\n`);
102
- }
103
- }
104
- _setEntries(entries) {
105
- this._entries = entries;
106
- this._updateReport();
107
- }
108
- _setupExitHandler() {
109
- let isExiting = false;
110
- const handleExit = async () => {
111
- if (isExiting)
112
- return;
113
- isExiting = true;
114
- setTimeout(() => process.exit(0), 15000);
115
- for (const entry of this._entries)
116
- await entry.browser?.close().catch(() => { });
117
- process.exit(0);
118
- };
119
- process.stdin.on('close', handleExit);
120
- process.on('SIGINT', handleExit);
121
- process.on('SIGTERM', handleExit);
122
- }
123
- }
124
- program
125
- .name('browser-agent')
126
- .option('-p, --port <port>', 'Port to listen on', '9224')
127
- .action(async (options) => {
128
- await main(options);
129
- });
130
- void program.parseAsync(process.argv);
131
- async function main(options) {
132
- const server = new BrowserServer();
133
- await server.start(+options.port);
134
- }
135
- function readBody(req) {
136
- return new Promise((resolve, reject) => {
137
- const chunks = [];
138
- req.on('data', (chunk) => chunks.push(chunk));
139
- req.on('end', () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
140
- });
141
- }
142
- async function findFreePort() {
143
- return new Promise((resolve, reject) => {
144
- const server = net.createServer();
145
- server.listen(0, () => {
146
- const { port } = server.address();
147
- server.close(() => resolve(port));
148
- });
149
- server.on('error', reject);
150
- });
151
- }
package/lib/config.js DELETED
@@ -1,189 +0,0 @@
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 'fs';
17
- import os from 'os';
18
- import path from 'path';
19
- import { devices } from '@szymonrybczak/patchright';
20
- import { sanitizeForFilePath } from './tools/utils.js';
21
- const defaultConfig = {
22
- browser: {
23
- browserName: 'chromium',
24
- launchOptions: {
25
- channel: 'chrome',
26
- headless: os.platform() === 'linux' && !process.env.DISPLAY,
27
- chromiumSandbox: true,
28
- },
29
- contextOptions: {
30
- viewport: null,
31
- },
32
- },
33
- network: {
34
- allowedOrigins: undefined,
35
- blockedOrigins: undefined,
36
- },
37
- server: {},
38
- outputDir: path.join(os.tmpdir(), 'playwright-mcp-output', sanitizeForFilePath(new Date().toISOString())),
39
- };
40
- export async function resolveConfig(config) {
41
- return mergeConfig(defaultConfig, config);
42
- }
43
- export async function resolveCLIConfig(cliOptions) {
44
- const configInFile = await loadConfig(cliOptions.config);
45
- const cliOverrides = await configFromCLIOptions(cliOptions);
46
- const result = mergeConfig(mergeConfig(defaultConfig, configInFile), cliOverrides);
47
- // Derive artifact output directory from config.outputDir
48
- if (result.saveTrace)
49
- result.browser.launchOptions.tracesDir = path.join(result.outputDir, 'traces');
50
- return result;
51
- }
52
- export async function configFromCLIOptions(cliOptions) {
53
- let browserName;
54
- let channel;
55
- switch (cliOptions.browser) {
56
- case 'chrome':
57
- case 'chrome-beta':
58
- case 'chrome-canary':
59
- case 'chrome-dev':
60
- case 'chromium':
61
- case 'msedge':
62
- case 'msedge-beta':
63
- case 'msedge-canary':
64
- case 'msedge-dev':
65
- browserName = 'chromium';
66
- channel = cliOptions.browser;
67
- break;
68
- case 'firefox':
69
- browserName = 'firefox';
70
- break;
71
- case 'webkit':
72
- browserName = 'webkit';
73
- break;
74
- }
75
- // Launch options
76
- const launchOptions = {
77
- channel,
78
- executablePath: cliOptions.executablePath,
79
- headless: cliOptions.headless,
80
- };
81
- // --no-sandbox was passed, disable the sandbox
82
- if (!cliOptions.sandbox)
83
- launchOptions.chromiumSandbox = false;
84
- if (cliOptions.proxyServer) {
85
- launchOptions.proxy = {
86
- server: cliOptions.proxyServer
87
- };
88
- if (cliOptions.proxyBypass)
89
- launchOptions.proxy.bypass = cliOptions.proxyBypass;
90
- }
91
- if (cliOptions.device && cliOptions.cdpEndpoint)
92
- throw new Error('Device emulation is not supported with cdpEndpoint.');
93
- // Context options
94
- const contextOptions = cliOptions.device ? devices[cliOptions.device] : {};
95
- if (cliOptions.storageState)
96
- contextOptions.storageState = cliOptions.storageState;
97
- if (cliOptions.userAgent)
98
- contextOptions.userAgent = cliOptions.userAgent;
99
- if (cliOptions.viewportSize) {
100
- try {
101
- const [width, height] = cliOptions.viewportSize.split(',').map(n => +n);
102
- if (isNaN(width) || isNaN(height))
103
- throw new Error('bad values');
104
- contextOptions.viewport = { width, height };
105
- }
106
- catch (e) {
107
- throw new Error('Invalid viewport size format: use "width,height", for example --viewport-size="800,600"');
108
- }
109
- }
110
- if (cliOptions.ignoreHttpsErrors)
111
- contextOptions.ignoreHTTPSErrors = true;
112
- if (cliOptions.blockServiceWorkers)
113
- contextOptions.serviceWorkers = 'block';
114
- const result = {
115
- browser: {
116
- browserAgent: cliOptions.browserAgent ?? process.env.PW_BROWSER_AGENT,
117
- browserName,
118
- isolated: cliOptions.isolated,
119
- userDataDir: cliOptions.userDataDir,
120
- launchOptions,
121
- contextOptions,
122
- cdpEndpoint: cliOptions.cdpEndpoint,
123
- },
124
- server: {
125
- port: cliOptions.port,
126
- host: cliOptions.host,
127
- },
128
- capabilities: cliOptions.caps?.split(',').map((c) => c.trim()),
129
- vision: !!cliOptions.vision,
130
- network: {
131
- allowedOrigins: cliOptions.allowedOrigins,
132
- blockedOrigins: cliOptions.blockedOrigins,
133
- },
134
- saveTrace: cliOptions.saveTrace,
135
- outputDir: cliOptions.outputDir,
136
- imageResponses: cliOptions.imageResponses,
137
- };
138
- return result;
139
- }
140
- async function loadConfig(configFile) {
141
- if (!configFile)
142
- return {};
143
- try {
144
- return JSON.parse(await fs.promises.readFile(configFile, 'utf8'));
145
- }
146
- catch (error) {
147
- throw new Error(`Failed to load config file: ${configFile}, ${error}`);
148
- }
149
- }
150
- export async function outputFile(config, name) {
151
- await fs.promises.mkdir(config.outputDir, { recursive: true });
152
- const fileName = sanitizeForFilePath(name);
153
- return path.join(config.outputDir, fileName);
154
- }
155
- function pickDefined(obj) {
156
- return Object.fromEntries(Object.entries(obj ?? {}).filter(([_, v]) => v !== undefined));
157
- }
158
- function mergeConfig(base, overrides) {
159
- const browser = {
160
- ...pickDefined(base.browser),
161
- ...pickDefined(overrides.browser),
162
- browserName: overrides.browser?.browserName ?? base.browser?.browserName ?? 'chromium',
163
- isolated: overrides.browser?.isolated ?? base.browser?.isolated ?? false,
164
- launchOptions: {
165
- ...pickDefined(base.browser?.launchOptions),
166
- ...pickDefined(overrides.browser?.launchOptions),
167
- ...{ assistantMode: true },
168
- },
169
- contextOptions: {
170
- ...pickDefined(base.browser?.contextOptions),
171
- ...pickDefined(overrides.browser?.contextOptions),
172
- },
173
- };
174
- if (browser.browserName !== 'chromium' && browser.launchOptions)
175
- delete browser.launchOptions.channel;
176
- return {
177
- ...pickDefined(base),
178
- ...pickDefined(overrides),
179
- browser,
180
- network: {
181
- ...pickDefined(base.network),
182
- ...pickDefined(overrides.network),
183
- },
184
- server: {
185
- ...pickDefined(base.server),
186
- ...pickDefined(overrides.server),
187
- },
188
- };
189
- }
package/lib/connection.js DELETED
@@ -1,82 +0,0 @@
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 { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';
17
- import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
18
- import { zodToJsonSchema } from 'zod-to-json-schema';
19
- import { Context } from './context.js';
20
- import { snapshotTools, visionTools } from './tools.js';
21
- import { packageJSON } from './package.js';
22
- export function createConnection(config, browserContextFactory) {
23
- const allTools = config.vision ? visionTools : snapshotTools;
24
- const tools = allTools.filter(tool => !config.capabilities || tool.capability === 'core' || config.capabilities.includes(tool.capability));
25
- const context = new Context(tools, config, browserContextFactory);
26
- const server = new McpServer({ name: 'Playwright', version: packageJSON.version }, {
27
- capabilities: {
28
- tools: {},
29
- }
30
- });
31
- server.setRequestHandler(ListToolsRequestSchema, async () => {
32
- return {
33
- tools: tools.map(tool => ({
34
- name: tool.schema.name,
35
- description: tool.schema.description,
36
- inputSchema: zodToJsonSchema(tool.schema.inputSchema),
37
- annotations: {
38
- title: tool.schema.title,
39
- readOnlyHint: tool.schema.type === 'readOnly',
40
- destructiveHint: tool.schema.type === 'destructive',
41
- openWorldHint: true,
42
- },
43
- })),
44
- };
45
- });
46
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
47
- const errorResult = (...messages) => ({
48
- content: [{ type: 'text', text: messages.join('\n') }],
49
- isError: true,
50
- });
51
- const tool = tools.find(tool => tool.schema.name === request.params.name);
52
- if (!tool)
53
- return errorResult(`Tool "${request.params.name}" not found`);
54
- const modalStates = context.modalStates().map(state => state.type);
55
- if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
56
- return errorResult(`The tool "${request.params.name}" can only be used when there is related modal state present.`, ...context.modalStatesMarkdown());
57
- if (!tool.clearsModalState && modalStates.length)
58
- return errorResult(`Tool "${request.params.name}" does not handle the modal state.`, ...context.modalStatesMarkdown());
59
- try {
60
- return await context.run(tool, request.params.arguments);
61
- }
62
- catch (error) {
63
- return errorResult(String(error));
64
- }
65
- });
66
- return new Connection(server, context);
67
- }
68
- export class Connection {
69
- server;
70
- context;
71
- constructor(server, context) {
72
- this.server = server;
73
- this.context = context;
74
- this.server.oninitialized = () => {
75
- this.context.clientVersion = this.server.getClientVersion();
76
- };
77
- }
78
- async close() {
79
- await this.server.close();
80
- await this.context.close();
81
- }
82
- }