@playwright/mcp 0.0.27 → 0.0.29
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 +30 -2
- package/config.d.ts +5 -0
- package/index.d.ts +3 -4
- package/lib/browserContextFactory.js +227 -0
- package/lib/browserServer.js +151 -0
- package/lib/config.js +9 -17
- package/lib/connection.js +10 -14
- package/lib/context.js +14 -77
- package/lib/fileUtils.js +32 -0
- package/lib/httpServer.js +201 -0
- package/lib/index.js +17 -2
- package/lib/package.js +20 -0
- package/lib/program.js +11 -20
- package/lib/server.js +48 -0
- package/lib/tab.js +1 -1
- package/lib/transport.js +18 -24
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -52,6 +52,12 @@ After installation, the Playwright MCP server will be available for use with you
|
|
|
52
52
|
<details>
|
|
53
53
|
<summary><b>Install in Cursor</b></summary>
|
|
54
54
|
|
|
55
|
+
#### Click the button to install:
|
|
56
|
+
|
|
57
|
+
[](https://cursor.com/install-mcp?name=playwright&config=eyJjb21tYW5kIjoibnB4IEBwbGF5d3JpZ2h0L21jcEBsYXRlc3QifQ%3D%3D)
|
|
58
|
+
|
|
59
|
+
#### Or install manually:
|
|
60
|
+
|
|
55
61
|
Go to `Cursor Settings` -> `MCP` -> `Add new MCP Server`. Name to your liking, use `command` type with the command `npx @playwright/mcp`. You can also verify config or add command like arguments via clicking `Edit`.
|
|
56
62
|
|
|
57
63
|
```js
|
|
@@ -106,6 +112,27 @@ Follow the MCP install [guide](https://modelcontextprotocol.io/quickstart/user),
|
|
|
106
112
|
```
|
|
107
113
|
</details>
|
|
108
114
|
|
|
115
|
+
<details>
|
|
116
|
+
<summary><b>Install in Qodo Gen</b></summary>
|
|
117
|
+
|
|
118
|
+
Open [Qodo Gen](https://docs.qodo.ai/qodo-documentation/qodo-gen) chat panel in VSCode or IntelliJ → Connect more tools → + Add new MCP → Paste the following configuration:
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
{
|
|
122
|
+
"mcpServers": {
|
|
123
|
+
"playwright": {
|
|
124
|
+
"command": "npx",
|
|
125
|
+
"args": [
|
|
126
|
+
"@playwright/mcp@latest"
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Click <code>Save</code>.
|
|
134
|
+
</details>
|
|
135
|
+
|
|
109
136
|
### Configuration
|
|
110
137
|
|
|
111
138
|
Playwright MCP server supports following arguments. They can be provided in the JSON configuration above, as a part of the `"args"` list:
|
|
@@ -124,6 +151,7 @@ Playwright MCP server supports following arguments. They can be provided in the
|
|
|
124
151
|
--block-service-workers block service workers
|
|
125
152
|
--browser <browser> browser or chrome channel to use, possible
|
|
126
153
|
values: chrome, firefox, webkit, msedge.
|
|
154
|
+
--browser-agent <endpoint> Use browser agent (experimental).
|
|
127
155
|
--caps <caps> comma-separated list of capabilities to enable,
|
|
128
156
|
possible values: tabs, pdf, history, wait, files,
|
|
129
157
|
install. Default is all.
|
|
@@ -199,7 +227,7 @@ state [here](https://playwright.dev/docs/auth).
|
|
|
199
227
|
"args": [
|
|
200
228
|
"@playwright/mcp@latest",
|
|
201
229
|
"--isolated",
|
|
202
|
-
"--storage-state={path/to/storage.json}
|
|
230
|
+
"--storage-state={path/to/storage.json}"
|
|
203
231
|
]
|
|
204
232
|
}
|
|
205
233
|
}
|
|
@@ -354,7 +382,7 @@ http.createServer(async (req, res) => {
|
|
|
354
382
|
// Creates a headless Playwright MCP server with SSE transport
|
|
355
383
|
const connection = await createConnection({ browser: { launchOptions: { headless: true } } });
|
|
356
384
|
const transport = new SSEServerTransport('/messages', res);
|
|
357
|
-
await connection.connect(transport);
|
|
385
|
+
await connection.sever.connect(transport);
|
|
358
386
|
|
|
359
387
|
// ...
|
|
360
388
|
});
|
package/config.d.ts
CHANGED
package/index.d.ts
CHANGED
|
@@ -16,14 +16,13 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
19
|
-
import type { Config } from './config';
|
|
20
|
-
import type {
|
|
19
|
+
import type { Config } from './config.js';
|
|
20
|
+
import type { BrowserContext } from 'playwright';
|
|
21
21
|
|
|
22
22
|
export type Connection = {
|
|
23
23
|
server: Server;
|
|
24
|
-
connect(transport: Transport): Promise<void>;
|
|
25
24
|
close(): Promise<void>;
|
|
26
25
|
};
|
|
27
26
|
|
|
28
|
-
export declare function createConnection(config?: Config): Promise<Connection>;
|
|
27
|
+
export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Connection>;
|
|
29
28
|
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
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 'playwright';
|
|
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
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
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 'playwright';
|
|
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
CHANGED
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import fs from 'fs';
|
|
17
|
-
import net from 'net';
|
|
18
17
|
import os from 'os';
|
|
19
18
|
import path from 'path';
|
|
20
19
|
import { devices } from 'playwright';
|
|
@@ -35,6 +34,7 @@ const defaultConfig = {
|
|
|
35
34
|
allowedOrigins: undefined,
|
|
36
35
|
blockedOrigins: undefined,
|
|
37
36
|
},
|
|
37
|
+
server: {},
|
|
38
38
|
outputDir: path.join(os.tmpdir(), 'playwright-mcp-output', sanitizeForFilePath(new Date().toISOString())),
|
|
39
39
|
};
|
|
40
40
|
export async function resolveConfig(config) {
|
|
@@ -47,8 +47,6 @@ export async function resolveCLIConfig(cliOptions) {
|
|
|
47
47
|
// Derive artifact output directory from config.outputDir
|
|
48
48
|
if (result.saveTrace)
|
|
49
49
|
result.browser.launchOptions.tracesDir = path.join(result.outputDir, 'traces');
|
|
50
|
-
if (result.browser.browserName === 'chromium')
|
|
51
|
-
result.browser.launchOptions.cdpPort = await findFreePort();
|
|
52
50
|
return result;
|
|
53
51
|
}
|
|
54
52
|
export async function configFromCLIOptions(cliOptions) {
|
|
@@ -113,6 +111,7 @@ export async function configFromCLIOptions(cliOptions) {
|
|
|
113
111
|
contextOptions.serviceWorkers = 'block';
|
|
114
112
|
const result = {
|
|
115
113
|
browser: {
|
|
114
|
+
browserAgent: cliOptions.browserAgent ?? process.env.PW_BROWSER_AGENT,
|
|
116
115
|
browserName,
|
|
117
116
|
isolated: cliOptions.isolated,
|
|
118
117
|
userDataDir: cliOptions.userDataDir,
|
|
@@ -136,16 +135,6 @@ export async function configFromCLIOptions(cliOptions) {
|
|
|
136
135
|
};
|
|
137
136
|
return result;
|
|
138
137
|
}
|
|
139
|
-
async function findFreePort() {
|
|
140
|
-
return new Promise((resolve, reject) => {
|
|
141
|
-
const server = net.createServer();
|
|
142
|
-
server.listen(0, () => {
|
|
143
|
-
const { port } = server.address();
|
|
144
|
-
server.close(() => resolve(port));
|
|
145
|
-
});
|
|
146
|
-
server.on('error', reject);
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
138
|
async function loadConfig(configFile) {
|
|
150
139
|
if (!configFile)
|
|
151
140
|
return {};
|
|
@@ -166,6 +155,8 @@ function pickDefined(obj) {
|
|
|
166
155
|
}
|
|
167
156
|
function mergeConfig(base, overrides) {
|
|
168
157
|
const browser = {
|
|
158
|
+
...pickDefined(base.browser),
|
|
159
|
+
...pickDefined(overrides.browser),
|
|
169
160
|
browserName: overrides.browser?.browserName ?? base.browser?.browserName ?? 'chromium',
|
|
170
161
|
isolated: overrides.browser?.isolated ?? base.browser?.isolated ?? false,
|
|
171
162
|
launchOptions: {
|
|
@@ -177,9 +168,6 @@ function mergeConfig(base, overrides) {
|
|
|
177
168
|
...pickDefined(base.browser?.contextOptions),
|
|
178
169
|
...pickDefined(overrides.browser?.contextOptions),
|
|
179
170
|
},
|
|
180
|
-
userDataDir: overrides.browser?.userDataDir ?? base.browser?.userDataDir,
|
|
181
|
-
cdpEndpoint: overrides.browser?.cdpEndpoint ?? base.browser?.cdpEndpoint,
|
|
182
|
-
remoteEndpoint: overrides.browser?.remoteEndpoint ?? base.browser?.remoteEndpoint,
|
|
183
171
|
};
|
|
184
172
|
if (browser.browserName !== 'chromium' && browser.launchOptions)
|
|
185
173
|
delete browser.launchOptions.channel;
|
|
@@ -190,6 +178,10 @@ function mergeConfig(base, overrides) {
|
|
|
190
178
|
network: {
|
|
191
179
|
...pickDefined(base.network),
|
|
192
180
|
...pickDefined(overrides.network),
|
|
193
|
-
}
|
|
181
|
+
},
|
|
182
|
+
server: {
|
|
183
|
+
...pickDefined(base.server),
|
|
184
|
+
...pickDefined(overrides.server),
|
|
185
|
+
},
|
|
194
186
|
};
|
|
195
187
|
}
|
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')
|
|
@@ -67,7 +67,7 @@ export class Context {
|
|
|
67
67
|
}
|
|
68
68
|
currentTabOrDie() {
|
|
69
69
|
if (!this._currentTab)
|
|
70
|
-
throw new Error('No current snapshot available. Capture a snapshot
|
|
70
|
+
throw new Error('No current snapshot available. Capture a snapshot or navigate to a new location first.');
|
|
71
71
|
return this._currentTab;
|
|
72
72
|
}
|
|
73
73
|
async newTab() {
|
|
@@ -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/fileUtils.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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 os from 'node:os';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
export function cacheDir() {
|
|
19
|
+
let cacheDirectory;
|
|
20
|
+
if (process.platform === 'linux')
|
|
21
|
+
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
|
22
|
+
else if (process.platform === 'darwin')
|
|
23
|
+
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
24
|
+
else if (process.platform === 'win32')
|
|
25
|
+
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
26
|
+
else
|
|
27
|
+
throw new Error('Unsupported platform: ' + process.platform);
|
|
28
|
+
return path.join(cacheDirectory, 'ms-playwright');
|
|
29
|
+
}
|
|
30
|
+
export async function userDataDir(browserConfig) {
|
|
31
|
+
return path.join(cacheDir(), 'ms-playwright', `mcp-${browserConfig.launchOptions?.channel ?? browserConfig?.browserName}-profile`);
|
|
32
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
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 path from 'path';
|
|
18
|
+
import http from 'http';
|
|
19
|
+
import mime from 'mime';
|
|
20
|
+
import { ManualPromise } from './manualPromise.js';
|
|
21
|
+
export class HttpServer {
|
|
22
|
+
_server;
|
|
23
|
+
_urlPrefixPrecise = '';
|
|
24
|
+
_urlPrefixHumanReadable = '';
|
|
25
|
+
_port = 0;
|
|
26
|
+
_routes = [];
|
|
27
|
+
constructor() {
|
|
28
|
+
this._server = http.createServer(this._onRequest.bind(this));
|
|
29
|
+
decorateServer(this._server);
|
|
30
|
+
}
|
|
31
|
+
server() {
|
|
32
|
+
return this._server;
|
|
33
|
+
}
|
|
34
|
+
routePrefix(prefix, handler) {
|
|
35
|
+
this._routes.push({ prefix, handler });
|
|
36
|
+
}
|
|
37
|
+
routePath(path, handler) {
|
|
38
|
+
this._routes.push({ exact: path, handler });
|
|
39
|
+
}
|
|
40
|
+
port() {
|
|
41
|
+
return this._port;
|
|
42
|
+
}
|
|
43
|
+
async _tryStart(port, host) {
|
|
44
|
+
const errorPromise = new ManualPromise();
|
|
45
|
+
const errorListener = (error) => errorPromise.reject(error);
|
|
46
|
+
this._server.on('error', errorListener);
|
|
47
|
+
try {
|
|
48
|
+
this._server.listen(port, host);
|
|
49
|
+
await Promise.race([
|
|
50
|
+
new Promise(cb => this._server.once('listening', cb)),
|
|
51
|
+
errorPromise,
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
this._server.removeListener('error', errorListener);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async start(options = {}) {
|
|
59
|
+
const host = options.host || 'localhost';
|
|
60
|
+
if (options.preferredPort) {
|
|
61
|
+
try {
|
|
62
|
+
await this._tryStart(options.preferredPort, host);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
if (!e || !e.message || !e.message.includes('EADDRINUSE'))
|
|
66
|
+
throw e;
|
|
67
|
+
await this._tryStart(undefined, host);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
await this._tryStart(options.port, host);
|
|
72
|
+
}
|
|
73
|
+
const address = this._server.address();
|
|
74
|
+
if (typeof address === 'string') {
|
|
75
|
+
this._urlPrefixPrecise = address;
|
|
76
|
+
this._urlPrefixHumanReadable = address;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this._port = address.port;
|
|
80
|
+
const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
|
|
81
|
+
this._urlPrefixPrecise = `http://${resolvedHost}:${address.port}`;
|
|
82
|
+
this._urlPrefixHumanReadable = `http://${host}:${address.port}`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async stop() {
|
|
86
|
+
await new Promise(cb => this._server.close(cb));
|
|
87
|
+
}
|
|
88
|
+
urlPrefix(purpose) {
|
|
89
|
+
return purpose === 'human-readable' ? this._urlPrefixHumanReadable : this._urlPrefixPrecise;
|
|
90
|
+
}
|
|
91
|
+
serveFile(request, response, absoluteFilePath, headers) {
|
|
92
|
+
try {
|
|
93
|
+
for (const [name, value] of Object.entries(headers || {}))
|
|
94
|
+
response.setHeader(name, value);
|
|
95
|
+
if (request.headers.range)
|
|
96
|
+
this._serveRangeFile(request, response, absoluteFilePath);
|
|
97
|
+
else
|
|
98
|
+
this._serveFile(response, absoluteFilePath);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
_serveFile(response, absoluteFilePath) {
|
|
106
|
+
const content = fs.readFileSync(absoluteFilePath);
|
|
107
|
+
response.statusCode = 200;
|
|
108
|
+
const contentType = mime.getType(path.extname(absoluteFilePath)) || 'application/octet-stream';
|
|
109
|
+
response.setHeader('Content-Type', contentType);
|
|
110
|
+
response.setHeader('Content-Length', content.byteLength);
|
|
111
|
+
response.end(content);
|
|
112
|
+
}
|
|
113
|
+
_serveRangeFile(request, response, absoluteFilePath) {
|
|
114
|
+
const range = request.headers.range;
|
|
115
|
+
if (!range || !range.startsWith('bytes=') || range.includes(', ') || [...range].filter(char => char === '-').length !== 1) {
|
|
116
|
+
response.statusCode = 400;
|
|
117
|
+
return response.end('Bad request');
|
|
118
|
+
}
|
|
119
|
+
// Parse the range header: https://datatracker.ietf.org/doc/html/rfc7233#section-2.1
|
|
120
|
+
const [startStr, endStr] = range.replace(/bytes=/, '').split('-');
|
|
121
|
+
// Both start and end (when passing to fs.createReadStream) and the range header are inclusive and start counting at 0.
|
|
122
|
+
let start;
|
|
123
|
+
let end;
|
|
124
|
+
const size = fs.statSync(absoluteFilePath).size;
|
|
125
|
+
if (startStr !== '' && endStr === '') {
|
|
126
|
+
// No end specified: use the whole file
|
|
127
|
+
start = +startStr;
|
|
128
|
+
end = size - 1;
|
|
129
|
+
}
|
|
130
|
+
else if (startStr === '' && endStr !== '') {
|
|
131
|
+
// No start specified: calculate start manually
|
|
132
|
+
start = size - +endStr;
|
|
133
|
+
end = size - 1;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
start = +startStr;
|
|
137
|
+
end = +endStr;
|
|
138
|
+
}
|
|
139
|
+
// Handle unavailable range request
|
|
140
|
+
if (Number.isNaN(start) || Number.isNaN(end) || start >= size || end >= size || start > end) {
|
|
141
|
+
// Return the 416 Range Not Satisfiable: https://datatracker.ietf.org/doc/html/rfc7233#section-4.4
|
|
142
|
+
response.writeHead(416, {
|
|
143
|
+
'Content-Range': `bytes */${size}`
|
|
144
|
+
});
|
|
145
|
+
return response.end();
|
|
146
|
+
}
|
|
147
|
+
// Sending Partial Content: https://datatracker.ietf.org/doc/html/rfc7233#section-4.1
|
|
148
|
+
response.writeHead(206, {
|
|
149
|
+
'Content-Range': `bytes ${start}-${end}/${size}`,
|
|
150
|
+
'Accept-Ranges': 'bytes',
|
|
151
|
+
'Content-Length': end - start + 1,
|
|
152
|
+
'Content-Type': mime.getType(path.extname(absoluteFilePath)),
|
|
153
|
+
});
|
|
154
|
+
const readable = fs.createReadStream(absoluteFilePath, { start, end });
|
|
155
|
+
readable.pipe(response);
|
|
156
|
+
}
|
|
157
|
+
_onRequest(request, response) {
|
|
158
|
+
if (request.method === 'OPTIONS') {
|
|
159
|
+
response.writeHead(200);
|
|
160
|
+
response.end();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
request.on('error', () => response.end());
|
|
164
|
+
try {
|
|
165
|
+
if (!request.url) {
|
|
166
|
+
response.end();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const url = new URL('http://localhost' + request.url);
|
|
170
|
+
for (const route of this._routes) {
|
|
171
|
+
if (route.exact && url.pathname === route.exact) {
|
|
172
|
+
route.handler(request, response);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (route.prefix && url.pathname.startsWith(route.prefix)) {
|
|
176
|
+
route.handler(request, response);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
response.statusCode = 404;
|
|
181
|
+
response.end();
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
response.end();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function decorateServer(server) {
|
|
189
|
+
const sockets = new Set();
|
|
190
|
+
server.on('connection', socket => {
|
|
191
|
+
sockets.add(socket);
|
|
192
|
+
socket.once('close', () => sockets.delete(socket));
|
|
193
|
+
});
|
|
194
|
+
const close = server.close;
|
|
195
|
+
server.close = (callback) => {
|
|
196
|
+
for (const socket of sockets)
|
|
197
|
+
socket.destroy();
|
|
198
|
+
sockets.clear();
|
|
199
|
+
return close.call(server, callback);
|
|
200
|
+
};
|
|
201
|
+
}
|
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)
|
|
@@ -26,6 +27,7 @@ program
|
|
|
26
27
|
.option('--blocked-origins <origins>', 'semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.', semicolonSeparatedList)
|
|
27
28
|
.option('--block-service-workers', 'block service workers')
|
|
28
29
|
.option('--browser <browser>', 'browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
|
|
30
|
+
.option('--browser-agent <endpoint>', 'Use browser agent (experimental).')
|
|
29
31
|
.option('--caps <caps>', 'comma-separated list of capabilities to enable, possible values: tabs, pdf, history, wait, files, install. Default is all.')
|
|
30
32
|
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
|
|
31
33
|
.option('--config <path>', 'path to the configuration file.')
|
|
@@ -49,12 +51,12 @@ program
|
|
|
49
51
|
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
|
50
52
|
.action(async (options) => {
|
|
51
53
|
const config = await resolveCLIConfig(options);
|
|
52
|
-
const
|
|
53
|
-
setupExitWatchdog(
|
|
54
|
-
if (
|
|
55
|
-
startHttpTransport(
|
|
54
|
+
const server = new Server(config);
|
|
55
|
+
server.setupExitWatchdog();
|
|
56
|
+
if (config.server.port !== undefined)
|
|
57
|
+
startHttpTransport(server);
|
|
56
58
|
else
|
|
57
|
-
await startStdioTransport(
|
|
59
|
+
await startStdioTransport(server);
|
|
58
60
|
if (config.saveTrace) {
|
|
59
61
|
const server = await startTraceViewerServer();
|
|
60
62
|
const urlPrefix = server.urlPrefix('human-readable');
|
|
@@ -63,18 +65,7 @@ program
|
|
|
63
65
|
console.error('\nTrace viewer listening on ' + url);
|
|
64
66
|
}
|
|
65
67
|
});
|
|
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
68
|
function semicolonSeparatedList(value) {
|
|
78
69
|
return value.split(';').map(v => v.trim());
|
|
79
70
|
}
|
|
80
|
-
program.
|
|
71
|
+
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/tab.js
CHANGED
|
@@ -73,7 +73,7 @@ export class Tab {
|
|
|
73
73
|
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
|
74
74
|
const download = await Promise.race([
|
|
75
75
|
downloadEvent,
|
|
76
|
-
new Promise(resolve => setTimeout(resolve,
|
|
76
|
+
new Promise(resolve => setTimeout(resolve, 1000)),
|
|
77
77
|
]);
|
|
78
78
|
if (!download)
|
|
79
79
|
throw e;
|
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.29",
|
|
4
4
|
"description": "Playwright Tools for MCP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"ctest": "playwright test --project=chrome",
|
|
25
25
|
"ftest": "playwright test --project=firefox",
|
|
26
26
|
"wtest": "playwright test --project=webkit",
|
|
27
|
+
"run-server": "node lib/browserServer.js",
|
|
27
28
|
"clean": "rm -rf lib",
|
|
28
29
|
"npm-publish": "npm run clean && npm run build && npm run test && npm publish"
|
|
29
30
|
},
|
|
@@ -37,14 +38,17 @@
|
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"@modelcontextprotocol/sdk": "^1.11.0",
|
|
39
40
|
"commander": "^13.1.0",
|
|
40
|
-
"
|
|
41
|
+
"debug": "^4.4.1",
|
|
42
|
+
"mime": "^4.0.7",
|
|
43
|
+
"playwright": "1.53.0",
|
|
41
44
|
"zod-to-json-schema": "^3.24.4"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"@eslint/eslintrc": "^3.2.0",
|
|
45
48
|
"@eslint/js": "^9.19.0",
|
|
46
|
-
"@playwright/test": "1.53.0
|
|
49
|
+
"@playwright/test": "1.53.0",
|
|
47
50
|
"@stylistic/eslint-plugin": "^3.0.1",
|
|
51
|
+
"@types/debug": "^4.1.12",
|
|
48
52
|
"@types/node": "^22.13.10",
|
|
49
53
|
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
50
54
|
"@typescript-eslint/parser": "^8.26.1",
|