playwright-mcp-phantomx 0.0.1

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 (56) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +688 -0
  3. package/cli.js +18 -0
  4. package/config.d.ts +119 -0
  5. package/index.d.ts +23 -0
  6. package/index.js +19 -0
  7. package/lib/browserContextFactory.js +205 -0
  8. package/lib/browserServerBackend.js +121 -0
  9. package/lib/config.js +246 -0
  10. package/lib/context.js +226 -0
  11. package/lib/extension/cdpRelay.js +346 -0
  12. package/lib/extension/extensionContextFactory.js +56 -0
  13. package/lib/extension/main.js +26 -0
  14. package/lib/fileUtils.js +32 -0
  15. package/lib/httpServer.js +39 -0
  16. package/lib/index.js +39 -0
  17. package/lib/javascript.js +49 -0
  18. package/lib/log.js +21 -0
  19. package/lib/loop/loop.js +69 -0
  20. package/lib/loop/loopClaude.js +152 -0
  21. package/lib/loop/loopOpenAI.js +141 -0
  22. package/lib/loop/main.js +60 -0
  23. package/lib/loopTools/context.js +66 -0
  24. package/lib/loopTools/main.js +49 -0
  25. package/lib/loopTools/perform.js +32 -0
  26. package/lib/loopTools/snapshot.js +29 -0
  27. package/lib/loopTools/tool.js +18 -0
  28. package/lib/manualPromise.js +111 -0
  29. package/lib/mcp/inProcessTransport.js +72 -0
  30. package/lib/mcp/server.js +93 -0
  31. package/lib/mcp/transport.js +140 -0
  32. package/lib/package.js +20 -0
  33. package/lib/program.js +103 -0
  34. package/lib/response.js +165 -0
  35. package/lib/sessionLog.js +121 -0
  36. package/lib/tab.js +249 -0
  37. package/lib/tools/common.js +55 -0
  38. package/lib/tools/console.js +33 -0
  39. package/lib/tools/dialogs.js +47 -0
  40. package/lib/tools/evaluate.js +53 -0
  41. package/lib/tools/files.js +44 -0
  42. package/lib/tools/install.js +53 -0
  43. package/lib/tools/keyboard.js +78 -0
  44. package/lib/tools/mouse.js +99 -0
  45. package/lib/tools/navigate.js +70 -0
  46. package/lib/tools/network.js +41 -0
  47. package/lib/tools/pdf.js +40 -0
  48. package/lib/tools/screenshot.js +77 -0
  49. package/lib/tools/snapshot.js +139 -0
  50. package/lib/tools/tabs.js +87 -0
  51. package/lib/tools/tool.js +33 -0
  52. package/lib/tools/utils.js +74 -0
  53. package/lib/tools/wait.js +56 -0
  54. package/lib/tools.js +50 -0
  55. package/lib/utils.js +26 -0
  56. package/package.json +73 -0
package/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import './lib/program.js';
package/config.d.ts ADDED
@@ -0,0 +1,119 @@
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
+
17
+ import type * as playwright from 'playwright';
18
+
19
+ export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf';
20
+
21
+ export type Config = {
22
+ /**
23
+ * The browser to use.
24
+ */
25
+ browser?: {
26
+ /**
27
+ * The type of browser to use.
28
+ */
29
+ browserName?: 'chromium' | 'firefox' | 'webkit';
30
+
31
+ /**
32
+ * Keep the browser profile in memory, do not save it to disk.
33
+ */
34
+ isolated?: boolean;
35
+
36
+ /**
37
+ * Path to a user data directory for browser profile persistence.
38
+ * Temporary directory is created by default.
39
+ */
40
+ userDataDir?: string;
41
+
42
+ /**
43
+ * Launch options passed to
44
+ * @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context
45
+ *
46
+ * This is useful for settings options like `channel`, `headless`, `executablePath`, etc.
47
+ */
48
+ launchOptions?: playwright.LaunchOptions;
49
+
50
+ /**
51
+ * Context options for the browser context.
52
+ *
53
+ * This is useful for settings options like `viewport`.
54
+ */
55
+ contextOptions?: playwright.BrowserContextOptions;
56
+
57
+ /**
58
+ * Chrome DevTools Protocol endpoint to connect to an existing browser instance in case of Chromium family browsers.
59
+ */
60
+ cdpEndpoint?: string;
61
+
62
+ /**
63
+ * Remote endpoint to connect to an existing Playwright server.
64
+ */
65
+ remoteEndpoint?: string;
66
+ },
67
+
68
+ server?: {
69
+ /**
70
+ * The port to listen on for SSE or MCP transport.
71
+ */
72
+ port?: number;
73
+
74
+ /**
75
+ * The host to bind the server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
76
+ */
77
+ host?: string;
78
+ },
79
+
80
+ /**
81
+ * List of enabled tool capabilities. Possible values:
82
+ * - 'core': Core browser automation features.
83
+ * - 'pdf': PDF generation and manipulation.
84
+ * - 'vision': Coordinate-based interactions.
85
+ */
86
+ capabilities?: ToolCapability[];
87
+
88
+ /**
89
+ * Whether to save the Playwright session into the output directory.
90
+ */
91
+ saveSession?: boolean;
92
+
93
+ /**
94
+ * Whether to save the Playwright trace of the session into the output directory.
95
+ */
96
+ saveTrace?: boolean;
97
+
98
+ /**
99
+ * The directory to save output files.
100
+ */
101
+ outputDir?: string;
102
+
103
+ network?: {
104
+ /**
105
+ * List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
106
+ */
107
+ allowedOrigins?: string[];
108
+
109
+ /**
110
+ * List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
111
+ */
112
+ blockedOrigins?: string[];
113
+ };
114
+
115
+ /**
116
+ * 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.
117
+ */
118
+ imageResponses?: 'allow' | 'omit';
119
+ };
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
19
+ import type { Config } from './config.js';
20
+ import type { BrowserContext } from 'playwright';
21
+
22
+ export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Server>;
23
+ export {};
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { createConnection } from './lib/index.js';
19
+ export { createConnection };
@@ -0,0 +1,205 @@
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 net from 'net';
18
+ import path from 'path';
19
+ import * as playwright from 'playwright';
20
+ // @ts-ignore
21
+ import { registryDirectory } from 'playwright-core/lib/server/registry/index';
22
+ import { logUnhandledError, testDebug } from './log.js';
23
+ import { createHash } from './utils.js';
24
+ import { outputFile } from './config.js';
25
+ export function contextFactory(config) {
26
+ if (config.browser.remoteEndpoint)
27
+ return new RemoteContextFactory(config);
28
+ if (config.browser.cdpEndpoint)
29
+ return new CdpContextFactory(config);
30
+ if (config.browser.isolated)
31
+ return new IsolatedContextFactory(config);
32
+ return new PersistentContextFactory(config);
33
+ }
34
+ class BaseContextFactory {
35
+ name;
36
+ description;
37
+ config;
38
+ _browserPromise;
39
+ _tracesDir;
40
+ constructor(name, description, config) {
41
+ this.name = name;
42
+ this.description = description;
43
+ this.config = config;
44
+ }
45
+ async _obtainBrowser() {
46
+ if (this._browserPromise)
47
+ return this._browserPromise;
48
+ testDebug(`obtain browser (${this.name})`);
49
+ this._browserPromise = this._doObtainBrowser();
50
+ void this._browserPromise.then(browser => {
51
+ browser.on('disconnected', () => {
52
+ this._browserPromise = undefined;
53
+ });
54
+ }).catch(() => {
55
+ this._browserPromise = undefined;
56
+ });
57
+ return this._browserPromise;
58
+ }
59
+ async _doObtainBrowser() {
60
+ throw new Error('Not implemented');
61
+ }
62
+ async createContext(clientInfo) {
63
+ if (this.config.saveTrace)
64
+ this._tracesDir = await outputFile(this.config, clientInfo.rootPath, `traces-${Date.now()}`);
65
+ testDebug(`create browser context (${this.name})`);
66
+ const browser = await this._obtainBrowser();
67
+ const browserContext = await this._doCreateContext(browser);
68
+ return { browserContext, close: () => this._closeBrowserContext(browserContext, browser) };
69
+ }
70
+ async _doCreateContext(browser) {
71
+ throw new Error('Not implemented');
72
+ }
73
+ async _closeBrowserContext(browserContext, browser) {
74
+ testDebug(`close browser context (${this.name})`);
75
+ if (browser.contexts().length === 1)
76
+ this._browserPromise = undefined;
77
+ await browserContext.close().catch(logUnhandledError);
78
+ if (browser.contexts().length === 0) {
79
+ testDebug(`close browser (${this.name})`);
80
+ await browser.close().catch(logUnhandledError);
81
+ }
82
+ }
83
+ }
84
+ class IsolatedContextFactory extends BaseContextFactory {
85
+ constructor(config) {
86
+ super('isolated', 'Create a new isolated browser context', config);
87
+ }
88
+ async _doObtainBrowser() {
89
+ await injectCdpPort(this.config.browser);
90
+ const browserType = playwright[this.config.browser.browserName];
91
+ return browserType.launch({
92
+ tracesDir: this._tracesDir,
93
+ ...this.config.browser.launchOptions,
94
+ handleSIGINT: false,
95
+ handleSIGTERM: false,
96
+ }).catch(error => {
97
+ if (error.message.includes('Executable doesn\'t exist'))
98
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
99
+ throw error;
100
+ });
101
+ }
102
+ async _doCreateContext(browser) {
103
+ return browser.newContext(this.config.browser.contextOptions);
104
+ }
105
+ }
106
+ class CdpContextFactory extends BaseContextFactory {
107
+ constructor(config) {
108
+ super('cdp', 'Connect to a browser over CDP', config);
109
+ }
110
+ async _doObtainBrowser() {
111
+ return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint);
112
+ }
113
+ async _doCreateContext(browser) {
114
+ return this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
115
+ }
116
+ }
117
+ class RemoteContextFactory extends BaseContextFactory {
118
+ constructor(config) {
119
+ super('remote', 'Connect to a browser using a remote endpoint', config);
120
+ }
121
+ async _doObtainBrowser() {
122
+ const url = new URL(this.config.browser.remoteEndpoint);
123
+ url.searchParams.set('browser', this.config.browser.browserName);
124
+ if (this.config.browser.launchOptions)
125
+ url.searchParams.set('launch-options', JSON.stringify(this.config.browser.launchOptions));
126
+ return playwright[this.config.browser.browserName].connect(String(url));
127
+ }
128
+ async _doCreateContext(browser) {
129
+ return browser.newContext();
130
+ }
131
+ }
132
+ class PersistentContextFactory {
133
+ config;
134
+ name = 'persistent';
135
+ description = 'Create a new persistent browser context';
136
+ _userDataDirs = new Set();
137
+ constructor(config) {
138
+ this.config = config;
139
+ }
140
+ async createContext(clientInfo) {
141
+ await injectCdpPort(this.config.browser);
142
+ testDebug('create browser context (persistent)');
143
+ const userDataDir = this.config.browser.userDataDir ?? await this._createUserDataDir(clientInfo.rootPath);
144
+ let tracesDir;
145
+ if (this.config.saveTrace)
146
+ tracesDir = await outputFile(this.config, clientInfo.rootPath, `traces-${Date.now()}`);
147
+ this._userDataDirs.add(userDataDir);
148
+ testDebug('lock user data dir', userDataDir);
149
+ const browserType = playwright[this.config.browser.browserName];
150
+ for (let i = 0; i < 5; i++) {
151
+ try {
152
+ const browserContext = await browserType.launchPersistentContext(userDataDir, {
153
+ tracesDir,
154
+ ...this.config.browser.launchOptions,
155
+ ...this.config.browser.contextOptions,
156
+ handleSIGINT: false,
157
+ handleSIGTERM: false,
158
+ });
159
+ const close = () => this._closeBrowserContext(browserContext, userDataDir);
160
+ return { browserContext, close };
161
+ }
162
+ catch (error) {
163
+ if (error.message.includes('Executable doesn\'t exist'))
164
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
165
+ if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
166
+ // User data directory is already in use, try again.
167
+ await new Promise(resolve => setTimeout(resolve, 1000));
168
+ continue;
169
+ }
170
+ throw error;
171
+ }
172
+ }
173
+ throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
174
+ }
175
+ async _closeBrowserContext(browserContext, userDataDir) {
176
+ testDebug('close browser context (persistent)');
177
+ testDebug('release user data dir', userDataDir);
178
+ await browserContext.close().catch(() => { });
179
+ this._userDataDirs.delete(userDataDir);
180
+ testDebug('close browser context complete (persistent)');
181
+ }
182
+ async _createUserDataDir(rootPath) {
183
+ const dir = process.env.PWMCP_PROFILES_DIR_FOR_TEST ?? registryDirectory;
184
+ const browserToken = this.config.browser.launchOptions?.channel ?? this.config.browser?.browserName;
185
+ // Hesitant putting hundreds of files into the user's workspace, so using it for hashing instead.
186
+ const rootPathToken = rootPath ? `-${createHash(rootPath)}` : '';
187
+ const result = path.join(dir, `mcp-${browserToken}${rootPathToken}`);
188
+ await fs.promises.mkdir(result, { recursive: true });
189
+ return result;
190
+ }
191
+ }
192
+ async function injectCdpPort(browserConfig) {
193
+ if (browserConfig.browserName === 'chromium')
194
+ browserConfig.launchOptions.cdpPort = await findFreePort();
195
+ }
196
+ async function findFreePort() {
197
+ return new Promise((resolve, reject) => {
198
+ const server = net.createServer();
199
+ server.listen(0, () => {
200
+ const { port } = server.address();
201
+ server.close(() => resolve(port));
202
+ });
203
+ server.on('error', reject);
204
+ });
205
+ }
@@ -0,0 +1,121 @@
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 { fileURLToPath } from 'url';
17
+ import { z } from 'zod';
18
+ import { Context } from './context.js';
19
+ import { logUnhandledError } from './log.js';
20
+ import { Response } from './response.js';
21
+ import { SessionLog } from './sessionLog.js';
22
+ import { filteredTools } from './tools.js';
23
+ import { packageJSON } from './package.js';
24
+ import { defineTool } from './tools/tool.js';
25
+ export class BrowserServerBackend {
26
+ name = 'Playwright';
27
+ version = packageJSON.version;
28
+ _tools;
29
+ _context;
30
+ _sessionLog;
31
+ _config;
32
+ _browserContextFactory;
33
+ constructor(config, factories) {
34
+ this._config = config;
35
+ this._browserContextFactory = factories[0];
36
+ this._tools = filteredTools(config);
37
+ if (factories.length > 1)
38
+ this._tools.push(this._defineContextSwitchTool(factories));
39
+ }
40
+ async initialize(server) {
41
+ const capabilities = server.getClientCapabilities();
42
+ let rootPath;
43
+ if (capabilities.roots && (server.getClientVersion()?.name === 'Visual Studio Code' ||
44
+ server.getClientVersion()?.name === 'Visual Studio Code - Insiders')) {
45
+ const { roots } = await server.listRoots();
46
+ const firstRootUri = roots[0]?.uri;
47
+ const url = firstRootUri ? new URL(firstRootUri) : undefined;
48
+ rootPath = url ? fileURLToPath(url) : undefined;
49
+ }
50
+ this._sessionLog = this._config.saveSession ? await SessionLog.create(this._config, rootPath) : undefined;
51
+ this._context = new Context({
52
+ tools: this._tools,
53
+ config: this._config,
54
+ browserContextFactory: this._browserContextFactory,
55
+ sessionLog: this._sessionLog,
56
+ clientInfo: { ...server.getClientVersion(), rootPath },
57
+ });
58
+ }
59
+ tools() {
60
+ return this._tools.map(tool => tool.schema);
61
+ }
62
+ async callTool(schema, parsedArguments) {
63
+ const context = this._context;
64
+ const response = new Response(context, schema.name, parsedArguments);
65
+ const tool = this._tools.find(tool => tool.schema.name === schema.name);
66
+ context.setRunningTool(true);
67
+ try {
68
+ await tool.handle(context, parsedArguments, response);
69
+ await response.finish();
70
+ this._sessionLog?.logResponse(response);
71
+ }
72
+ catch (error) {
73
+ response.addError(String(error));
74
+ }
75
+ finally {
76
+ context.setRunningTool(false);
77
+ }
78
+ return response.serialize();
79
+ }
80
+ serverClosed() {
81
+ void this._context.dispose().catch(logUnhandledError);
82
+ }
83
+ _defineContextSwitchTool(factories) {
84
+ const self = this;
85
+ return defineTool({
86
+ capability: 'core',
87
+ schema: {
88
+ name: 'browser_connect',
89
+ title: 'Connect to a browser context',
90
+ description: [
91
+ 'Connect to a browser using one of the available methods:',
92
+ ...factories.map(factory => `- "${factory.name}": ${factory.description}`),
93
+ ].join('\n'),
94
+ inputSchema: z.object({
95
+ method: z.enum(factories.map(factory => factory.name)).default(factories[0].name).describe('The method to use to connect to the browser'),
96
+ }),
97
+ type: 'readOnly',
98
+ },
99
+ async handle(context, params, response) {
100
+ const factory = factories.find(factory => factory.name === params.method);
101
+ if (!factory) {
102
+ response.addError('Unknown connection method: ' + params.method);
103
+ return;
104
+ }
105
+ await self._setContextFactory(factory);
106
+ response.addResult('Successfully changed connection method.');
107
+ }
108
+ });
109
+ }
110
+ async _setContextFactory(newFactory) {
111
+ if (this._context) {
112
+ const options = {
113
+ ...this._context.options,
114
+ browserContextFactory: newFactory,
115
+ };
116
+ await this._context.dispose();
117
+ this._context = new Context(options);
118
+ }
119
+ this._browserContextFactory = newFactory;
120
+ }
121
+ }