@playwright/mcp 0.0.27 → 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 +1 -1
- package/index.d.ts +2 -1
- package/lib/browserContextFactory.js +179 -0
- package/lib/config.js +6 -1
- package/lib/connection.js +10 -14
- package/lib/context.js +13 -76
- package/lib/index.js +17 -2
- package/lib/package.js +20 -0
- package/lib/program.js +10 -20
- package/lib/server.js +48 -0
- package/lib/transport.js +18 -24
- package/package.json +3 -1
package/README.md
CHANGED
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 {};
|
|
@@ -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) {
|
|
@@ -190,6 +191,10 @@ function mergeConfig(base, overrides) {
|
|
|
190
191
|
network: {
|
|
191
192
|
...pickDefined(base.network),
|
|
192
193
|
...pickDefined(overrides.network),
|
|
193
|
-
}
|
|
194
|
+
},
|
|
195
|
+
server: {
|
|
196
|
+
...pickDefined(base.server),
|
|
197
|
+
...pickDefined(overrides.server),
|
|
198
|
+
},
|
|
194
199
|
};
|
|
195
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
|
|
19
|
+
import { Context } from './context.js';
|
|
20
20
|
import { snapshotTools, visionTools } from './tools.js';
|
|
21
|
-
|
|
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
|
|
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
|
-
|
|
66
|
-
return connection;
|
|
66
|
+
return new Connection(server, context);
|
|
67
67
|
}
|
|
68
68
|
export class Connection {
|
|
69
69
|
server;
|
|
@@ -71,13 +71,9 @@ export class Connection {
|
|
|
71
71
|
constructor(server, context) {
|
|
72
72
|
this.server = server;
|
|
73
73
|
this.context = context;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
await new Promise(resolve => {
|
|
78
|
-
this.server.oninitialized = () => resolve();
|
|
79
|
-
});
|
|
80
|
-
this.context.clientVersion = this.server.getClientVersion();
|
|
74
|
+
this.server.oninitialized = () => {
|
|
75
|
+
this.context.clientVersion = this.server.getClientVersion();
|
|
76
|
+
};
|
|
81
77
|
}
|
|
82
78
|
async close() {
|
|
83
79
|
await this.server.close();
|
package/lib/context.js
CHANGED
|
@@ -13,28 +13,28 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import
|
|
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
32
|
clientVersion;
|
|
35
|
-
constructor(tools, config) {
|
|
33
|
+
constructor(tools, config, browserContextFactory) {
|
|
36
34
|
this.tools = tools;
|
|
37
35
|
this.config = config;
|
|
36
|
+
this._browserContextFactory = browserContextFactory;
|
|
37
|
+
testDebug('create context');
|
|
38
38
|
}
|
|
39
39
|
clientSupportsImages() {
|
|
40
40
|
if (this.config.imageResponses === 'allow')
|
|
@@ -241,14 +241,13 @@ ${code.join('\n')}
|
|
|
241
241
|
async close() {
|
|
242
242
|
if (!this._browserContextPromise)
|
|
243
243
|
return;
|
|
244
|
+
testDebug('close context');
|
|
244
245
|
const promise = this._browserContextPromise;
|
|
245
246
|
this._browserContextPromise = undefined;
|
|
246
|
-
await promise.then(async ({ browserContext,
|
|
247
|
+
await promise.then(async ({ browserContext, close }) => {
|
|
247
248
|
if (this.config.saveTrace)
|
|
248
249
|
await browserContext.tracing.stop();
|
|
249
|
-
await
|
|
250
|
-
await browser?.close();
|
|
251
|
-
}).catch(() => { });
|
|
250
|
+
await close();
|
|
252
251
|
});
|
|
253
252
|
}
|
|
254
253
|
async _setupRequestInterception(context) {
|
|
@@ -272,7 +271,9 @@ ${code.join('\n')}
|
|
|
272
271
|
return this._browserContextPromise;
|
|
273
272
|
}
|
|
274
273
|
async _setupBrowserContext() {
|
|
275
|
-
|
|
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;
|
|
276
277
|
await this._setupRequestInterception(browserContext);
|
|
277
278
|
for (const page of browserContext.pages())
|
|
278
279
|
this._onPageCreated(page);
|
|
@@ -285,70 +286,6 @@ ${code.join('\n')}
|
|
|
285
286
|
sources: false,
|
|
286
287
|
});
|
|
287
288
|
}
|
|
288
|
-
return
|
|
289
|
-
}
|
|
290
|
-
async _createBrowserContext() {
|
|
291
|
-
if (this.config.browser?.remoteEndpoint) {
|
|
292
|
-
const url = new URL(this.config.browser?.remoteEndpoint);
|
|
293
|
-
if (this.config.browser.browserName)
|
|
294
|
-
url.searchParams.set('browser', this.config.browser.browserName);
|
|
295
|
-
if (this.config.browser.launchOptions)
|
|
296
|
-
url.searchParams.set('launch-options', JSON.stringify(this.config.browser.launchOptions));
|
|
297
|
-
const browser = await playwright[this.config.browser?.browserName ?? 'chromium'].connect(String(url));
|
|
298
|
-
const browserContext = await browser.newContext();
|
|
299
|
-
return { browser, browserContext };
|
|
300
|
-
}
|
|
301
|
-
if (this.config.browser?.cdpEndpoint) {
|
|
302
|
-
const browser = await playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint);
|
|
303
|
-
const browserContext = this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
|
|
304
|
-
return { browser, browserContext };
|
|
305
|
-
}
|
|
306
|
-
return this.config.browser?.isolated ?
|
|
307
|
-
await createIsolatedContext(this.config.browser) :
|
|
308
|
-
await launchPersistentContext(this.config.browser);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
async function createIsolatedContext(browserConfig) {
|
|
312
|
-
try {
|
|
313
|
-
const browserName = browserConfig?.browserName ?? 'chromium';
|
|
314
|
-
const browserType = playwright[browserName];
|
|
315
|
-
const browser = await browserType.launch(browserConfig.launchOptions);
|
|
316
|
-
const browserContext = await browser.newContext(browserConfig.contextOptions);
|
|
317
|
-
return { browser, browserContext };
|
|
318
|
-
}
|
|
319
|
-
catch (error) {
|
|
320
|
-
if (error.message.includes('Executable doesn\'t exist'))
|
|
321
|
-
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
322
|
-
throw error;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
async function launchPersistentContext(browserConfig) {
|
|
326
|
-
try {
|
|
327
|
-
const browserName = browserConfig.browserName ?? 'chromium';
|
|
328
|
-
const userDataDir = browserConfig.userDataDir ?? await createUserDataDir({ ...browserConfig, browserName });
|
|
329
|
-
const browserType = playwright[browserName];
|
|
330
|
-
const browserContext = await browserType.launchPersistentContext(userDataDir, { ...browserConfig.launchOptions, ...browserConfig.contextOptions });
|
|
331
|
-
return { browserContext };
|
|
332
|
-
}
|
|
333
|
-
catch (error) {
|
|
334
|
-
if (error.message.includes('Executable doesn\'t exist'))
|
|
335
|
-
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
336
|
-
throw error;
|
|
289
|
+
return result;
|
|
337
290
|
}
|
|
338
291
|
}
|
|
339
|
-
async function createUserDataDir(browserConfig) {
|
|
340
|
-
let cacheDirectory;
|
|
341
|
-
if (process.platform === 'linux')
|
|
342
|
-
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
|
343
|
-
else if (process.platform === 'darwin')
|
|
344
|
-
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
345
|
-
else if (process.platform === 'win32')
|
|
346
|
-
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
347
|
-
else
|
|
348
|
-
throw new Error('Unsupported platform: ' + process.platform);
|
|
349
|
-
const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${browserConfig.launchOptions?.channel ?? browserConfig?.browserName}-profile`);
|
|
350
|
-
await fs.promises.mkdir(result, { recursive: true });
|
|
351
|
-
return result;
|
|
352
|
-
}
|
|
353
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
354
|
-
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
|
-
|
|
18
|
+
import { contextFactory } from './browserContextFactory.js';
|
|
19
|
+
export async function createConnection(userConfig = {}, contextGetter) {
|
|
19
20
|
const config = await resolveConfig(userConfig);
|
|
20
|
-
|
|
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'));
|
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 {
|
|
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)
|
|
@@ -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
|
|
53
|
-
setupExitWatchdog(
|
|
54
|
-
if (
|
|
55
|
-
startHttpTransport(
|
|
53
|
+
const server = new Server(config);
|
|
54
|
+
server.setupExitWatchdog();
|
|
55
|
+
if (config.server.port !== undefined)
|
|
56
|
+
startHttpTransport(server);
|
|
56
57
|
else
|
|
57
|
-
await startStdioTransport(
|
|
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.
|
|
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
|
+
}
|
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
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
await
|
|
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
|
-
|
|
51
|
-
|
|
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(
|
|
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
|
-
|
|
83
|
-
|
|
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(
|
|
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(
|
|
92
|
+
await handleStreamable(server, req, res, streamableSessions);
|
|
100
93
|
else
|
|
101
|
-
await handleSSE(
|
|
94
|
+
await handleSSE(server, req, res, url, sseSessions);
|
|
102
95
|
});
|
|
103
|
-
|
|
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.
|
|
3
|
+
"version": "0.0.28",
|
|
4
4
|
"description": "Playwright Tools for MCP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@modelcontextprotocol/sdk": "^1.11.0",
|
|
39
39
|
"commander": "^13.1.0",
|
|
40
|
+
"debug": "^4.4.1",
|
|
40
41
|
"playwright": "1.53.0-alpha-2025-05-27",
|
|
41
42
|
"zod-to-json-schema": "^3.24.4"
|
|
42
43
|
},
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"@eslint/js": "^9.19.0",
|
|
46
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",
|