@playwright/mcp 0.0.35-alpha-2025-08-27 → 0.0.35
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/config.d.ts +1 -1
- package/lib/extension/cdpRelay.js +14 -17
- package/lib/extension/extensionContextFactory.js +2 -4
- package/lib/program.js +1 -1
- package/lib/tools.js +1 -3
- package/package.json +1 -1
- package/lib/extension/protocol.js +0 -18
- package/lib/tools/verify.js +0 -137
package/config.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
import type * as playwright from 'playwright';
|
|
18
18
|
|
|
19
|
-
export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf'
|
|
19
|
+
export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf';
|
|
20
20
|
|
|
21
21
|
export type Config = {
|
|
22
22
|
/**
|
|
@@ -26,7 +26,6 @@ import { WebSocket, WebSocketServer } from 'ws';
|
|
|
26
26
|
import { httpAddressToString } from '../mcp/http.js';
|
|
27
27
|
import { logUnhandledError } from '../utils/log.js';
|
|
28
28
|
import { ManualPromise } from '../mcp/manualPromise.js';
|
|
29
|
-
import * as protocol from './protocol.js';
|
|
30
29
|
// @ts-ignore
|
|
31
30
|
const { registry } = await import('playwright-core/lib/server/registry/index');
|
|
32
31
|
const debugLogger = debug('pw:mcp:relay');
|
|
@@ -34,7 +33,6 @@ export class CDPRelayServer {
|
|
|
34
33
|
_wsHost;
|
|
35
34
|
_browserChannel;
|
|
36
35
|
_userDataDir;
|
|
37
|
-
_executablePath;
|
|
38
36
|
_cdpPath;
|
|
39
37
|
_extensionPath;
|
|
40
38
|
_wss;
|
|
@@ -43,11 +41,10 @@ export class CDPRelayServer {
|
|
|
43
41
|
_connectedTabInfo;
|
|
44
42
|
_nextSessionId = 1;
|
|
45
43
|
_extensionConnectionPromise;
|
|
46
|
-
constructor(server, browserChannel, userDataDir
|
|
44
|
+
constructor(server, browserChannel, userDataDir) {
|
|
47
45
|
this._wsHost = httpAddressToString(server.address()).replace(/^http/, 'ws');
|
|
48
46
|
this._browserChannel = browserChannel;
|
|
49
47
|
this._userDataDir = userDataDir;
|
|
50
|
-
this._executablePath = executablePath;
|
|
51
48
|
const uuid = crypto.randomUUID();
|
|
52
49
|
this._cdpPath = `/cdp/${uuid}`;
|
|
53
50
|
this._extensionPath = `/extension/${uuid}`;
|
|
@@ -86,19 +83,15 @@ export class CDPRelayServer {
|
|
|
86
83
|
version: clientInfo.version,
|
|
87
84
|
};
|
|
88
85
|
url.searchParams.set('client', JSON.stringify(client));
|
|
89
|
-
url.searchParams.set('protocolVersion', process.env.PWMCP_TEST_PROTOCOL_VERSION ?? protocol.VERSION.toString());
|
|
90
86
|
if (toolName)
|
|
91
87
|
url.searchParams.set('newTab', String(toolName === 'browser_navigate'));
|
|
92
88
|
const href = url.toString();
|
|
93
|
-
|
|
94
|
-
if (!
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (!executablePath)
|
|
100
|
-
throw new Error(`"${this._browserChannel}" executable not found. Make sure it is installed at a standard location.`);
|
|
101
|
-
}
|
|
89
|
+
const executableInfo = registry.findExecutable(this._browserChannel);
|
|
90
|
+
if (!executableInfo)
|
|
91
|
+
throw new Error(`Unsupported channel: "${this._browserChannel}"`);
|
|
92
|
+
const executablePath = executableInfo.executablePath();
|
|
93
|
+
if (!executablePath)
|
|
94
|
+
throw new Error(`"${this._browserChannel}" executable not found. Make sure it is installed at a standard location.`);
|
|
102
95
|
const args = [];
|
|
103
96
|
if (this._userDataDir)
|
|
104
97
|
args.push(`--user-data-dir=${this._userDataDir}`);
|
|
@@ -202,6 +195,10 @@ export class CDPRelayServer {
|
|
|
202
195
|
params: params.params
|
|
203
196
|
});
|
|
204
197
|
break;
|
|
198
|
+
case 'detachedFromTab':
|
|
199
|
+
debugLogger('← Debugger detached from tab:', params);
|
|
200
|
+
this._connectedTabInfo = undefined;
|
|
201
|
+
break;
|
|
205
202
|
}
|
|
206
203
|
}
|
|
207
204
|
async _handlePlaywrightMessage(message) {
|
|
@@ -237,7 +234,7 @@ export class CDPRelayServer {
|
|
|
237
234
|
if (sessionId)
|
|
238
235
|
break;
|
|
239
236
|
// Simulate auto-attach behavior with real target info
|
|
240
|
-
const { targetInfo } = await this._extensionConnection.send('attachToTab'
|
|
237
|
+
const { targetInfo } = await this._extensionConnection.send('attachToTab');
|
|
241
238
|
this._connectedTabInfo = {
|
|
242
239
|
targetInfo,
|
|
243
240
|
sessionId: `pw-tab-${this._nextSessionId++}`,
|
|
@@ -287,11 +284,11 @@ class ExtensionConnection {
|
|
|
287
284
|
this._ws.on('close', this._onClose.bind(this));
|
|
288
285
|
this._ws.on('error', this._onError.bind(this));
|
|
289
286
|
}
|
|
290
|
-
async send(method, params) {
|
|
287
|
+
async send(method, params, sessionId) {
|
|
291
288
|
if (this._ws.readyState !== WebSocket.OPEN)
|
|
292
289
|
throw new Error(`Unexpected WebSocket state: ${this._ws.readyState}`);
|
|
293
290
|
const id = ++this._lastId;
|
|
294
|
-
this._ws.send(JSON.stringify({ id, method, params }));
|
|
291
|
+
this._ws.send(JSON.stringify({ id, method, params, sessionId }));
|
|
295
292
|
const error = new Error(`Protocol error: ${method}`);
|
|
296
293
|
return new Promise((resolve, reject) => {
|
|
297
294
|
this._callbacks.set(id, { resolve, reject, error });
|
|
@@ -21,11 +21,9 @@ const debugLogger = debug('pw:mcp:relay');
|
|
|
21
21
|
export class ExtensionContextFactory {
|
|
22
22
|
_browserChannel;
|
|
23
23
|
_userDataDir;
|
|
24
|
-
|
|
25
|
-
constructor(browserChannel, userDataDir, executablePath) {
|
|
24
|
+
constructor(browserChannel, userDataDir) {
|
|
26
25
|
this._browserChannel = browserChannel;
|
|
27
26
|
this._userDataDir = userDataDir;
|
|
28
|
-
this._executablePath = executablePath;
|
|
29
27
|
}
|
|
30
28
|
async createContext(clientInfo, abortSignal, toolName) {
|
|
31
29
|
const browser = await this._obtainBrowser(clientInfo, abortSignal, toolName);
|
|
@@ -48,7 +46,7 @@ export class ExtensionContextFactory {
|
|
|
48
46
|
httpServer.close();
|
|
49
47
|
throw new Error(abortSignal.reason);
|
|
50
48
|
}
|
|
51
|
-
const cdpRelayServer = new CDPRelayServer(httpServer, this._browserChannel, this._userDataDir
|
|
49
|
+
const cdpRelayServer = new CDPRelayServer(httpServer, this._browserChannel, this._userDataDir);
|
|
52
50
|
abortSignal.addEventListener('abort', () => cdpRelayServer.stop());
|
|
53
51
|
debugLogger(`CDP relay server started, extension endpoint: ${cdpRelayServer.extensionEndpoint()}.`);
|
|
54
52
|
return cdpRelayServer;
|
package/lib/program.js
CHANGED
|
@@ -66,7 +66,7 @@ program
|
|
|
66
66
|
}
|
|
67
67
|
const config = await resolveCLIConfig(options);
|
|
68
68
|
const browserContextFactory = contextFactory(config);
|
|
69
|
-
const extensionContextFactory = new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir
|
|
69
|
+
const extensionContextFactory = new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir);
|
|
70
70
|
if (options.extension) {
|
|
71
71
|
const serverBackendFactory = {
|
|
72
72
|
name: 'Playwright w/ extension',
|
package/lib/tools.js
CHANGED
|
@@ -21,7 +21,6 @@ import files from './tools/files.js';
|
|
|
21
21
|
import form from './tools/form.js';
|
|
22
22
|
import install from './tools/install.js';
|
|
23
23
|
import keyboard from './tools/keyboard.js';
|
|
24
|
-
import mouse from './tools/mouse.js';
|
|
25
24
|
import navigate from './tools/navigate.js';
|
|
26
25
|
import network from './tools/network.js';
|
|
27
26
|
import pdf from './tools/pdf.js';
|
|
@@ -29,7 +28,7 @@ import snapshot from './tools/snapshot.js';
|
|
|
29
28
|
import tabs from './tools/tabs.js';
|
|
30
29
|
import screenshot from './tools/screenshot.js';
|
|
31
30
|
import wait from './tools/wait.js';
|
|
32
|
-
import
|
|
31
|
+
import mouse from './tools/mouse.js';
|
|
33
32
|
export const allTools = [
|
|
34
33
|
...common,
|
|
35
34
|
...console,
|
|
@@ -47,7 +46,6 @@ export const allTools = [
|
|
|
47
46
|
...snapshot,
|
|
48
47
|
...tabs,
|
|
49
48
|
...wait,
|
|
50
|
-
...verify,
|
|
51
49
|
];
|
|
52
50
|
export function filteredTools(config) {
|
|
53
51
|
return allTools.filter(tool => tool.capability.startsWith('core') || config.capabilities?.includes(tool.capability));
|
package/package.json
CHANGED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
// Whenever the commands/events change, the version must be updated. The latest
|
|
17
|
-
// extension version should be compatible with the old MCP clients.
|
|
18
|
-
export const VERSION = 1;
|
package/lib/tools/verify.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { z } from 'zod';
|
|
17
|
-
import { defineTabTool } from './tool.js';
|
|
18
|
-
import * as javascript from '../utils/codegen.js';
|
|
19
|
-
import { generateLocator } from './utils.js';
|
|
20
|
-
const verifyElement = defineTabTool({
|
|
21
|
-
capability: 'verify',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_verify_element_visible',
|
|
24
|
-
title: 'Verify element visible',
|
|
25
|
-
description: 'Verify element is visible on the page',
|
|
26
|
-
inputSchema: z.object({
|
|
27
|
-
role: z.string().describe('ROLE of the element. Can be found in the snapshot like this: \`- {ROLE} "Accessible Name":\`'),
|
|
28
|
-
accessibleName: z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: \`- role "{ACCESSIBLE_NAME}"\`'),
|
|
29
|
-
}),
|
|
30
|
-
type: 'readOnly',
|
|
31
|
-
},
|
|
32
|
-
handle: async (tab, params, response) => {
|
|
33
|
-
const locator = tab.page.getByRole(params.role, { name: params.accessibleName });
|
|
34
|
-
if (await locator.count() === 0) {
|
|
35
|
-
response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
response.addCode(`await expect(page.getByRole(${javascript.escapeWithQuotes(params.role)}, { name: ${javascript.escapeWithQuotes(params.accessibleName)} })).toBeVisible();`);
|
|
39
|
-
response.addResult('Done');
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
const verifyText = defineTabTool({
|
|
43
|
-
capability: 'verify',
|
|
44
|
-
schema: {
|
|
45
|
-
name: 'browser_verify_text_visible',
|
|
46
|
-
title: 'Verify text visible',
|
|
47
|
-
description: `Verify text is visible on the page. Prefer ${verifyElement.schema.name} if possible.`,
|
|
48
|
-
inputSchema: z.object({
|
|
49
|
-
text: z.string().describe('TEXT to verify. Can be found in the snapshot like this: \`- role "Accessible Name": {TEXT}\` or like this: \`- text: {TEXT}\`'),
|
|
50
|
-
}),
|
|
51
|
-
type: 'readOnly',
|
|
52
|
-
},
|
|
53
|
-
handle: async (tab, params, response) => {
|
|
54
|
-
const locator = tab.page.getByText(params.text).filter({ visible: true });
|
|
55
|
-
if (await locator.count() === 0) {
|
|
56
|
-
response.addError('Text not found');
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
response.addCode(`await expect(page.getByText(${javascript.escapeWithQuotes(params.text)})).toBeVisible();`);
|
|
60
|
-
response.addResult('Done');
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
const verifyList = defineTabTool({
|
|
64
|
-
capability: 'verify',
|
|
65
|
-
schema: {
|
|
66
|
-
name: 'browser_verify_list_visible',
|
|
67
|
-
title: 'Verify list visible',
|
|
68
|
-
description: 'Verify list is visible on the page',
|
|
69
|
-
inputSchema: z.object({
|
|
70
|
-
element: z.string().describe('Human-readable list description'),
|
|
71
|
-
ref: z.string().describe('Exact target element reference that points to the list'),
|
|
72
|
-
items: z.array(z.string()).describe('Items to verify'),
|
|
73
|
-
}),
|
|
74
|
-
type: 'readOnly',
|
|
75
|
-
},
|
|
76
|
-
handle: async (tab, params, response) => {
|
|
77
|
-
const locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
78
|
-
const itemTexts = [];
|
|
79
|
-
for (const item of params.items) {
|
|
80
|
-
const itemLocator = locator.getByText(item);
|
|
81
|
-
if (await itemLocator.count() === 0) {
|
|
82
|
-
response.addError(`Item "${item}" not found`);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
itemTexts.push((await itemLocator.textContent()));
|
|
86
|
-
}
|
|
87
|
-
const ariaSnapshot = `\`
|
|
88
|
-
- list:
|
|
89
|
-
${itemTexts.map(t => ` - text: ${javascript.escapeWithQuotes(t, '"')}`).join('\n')}
|
|
90
|
-
\``;
|
|
91
|
-
response.addCode(`await expect(page.locator('body')).toMatchAriaSnapshot(${ariaSnapshot});`);
|
|
92
|
-
response.addResult('Done');
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const verifyValue = defineTabTool({
|
|
96
|
-
capability: 'verify',
|
|
97
|
-
schema: {
|
|
98
|
-
name: 'browser_verify_value',
|
|
99
|
-
title: 'Verify value',
|
|
100
|
-
description: 'Verify element value',
|
|
101
|
-
inputSchema: z.object({
|
|
102
|
-
type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the element'),
|
|
103
|
-
element: z.string().describe('Human-readable element description'),
|
|
104
|
-
ref: z.string().describe('Exact target element reference that points to the element'),
|
|
105
|
-
value: z.string().describe('Value to verify. For checkbox, use "true" or "false".'),
|
|
106
|
-
}),
|
|
107
|
-
type: 'readOnly',
|
|
108
|
-
},
|
|
109
|
-
handle: async (tab, params, response) => {
|
|
110
|
-
const locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
111
|
-
const locatorSource = `page.${await generateLocator(locator)}`;
|
|
112
|
-
if (params.type === 'textbox' || params.type === 'slider' || params.type === 'combobox') {
|
|
113
|
-
const value = await locator.inputValue();
|
|
114
|
-
if (value !== params.value) {
|
|
115
|
-
response.addError(`Expected value "${params.value}", but got "${value}"`);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
response.addCode(`await expect(${locatorSource}).toHaveValue(${javascript.quote(params.value)});`);
|
|
119
|
-
}
|
|
120
|
-
else if (params.type === 'checkbox' || params.type === 'radio') {
|
|
121
|
-
const value = await locator.isChecked();
|
|
122
|
-
if (value !== (params.value === 'true')) {
|
|
123
|
-
response.addError(`Expected value "${params.value}", but got "${value}"`);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const matcher = value ? 'toBeChecked' : 'not.toBeChecked';
|
|
127
|
-
response.addCode(`await expect(${locatorSource}).${matcher}();`);
|
|
128
|
-
}
|
|
129
|
-
response.addResult('Done');
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
export default [
|
|
133
|
-
verifyElement,
|
|
134
|
-
verifyText,
|
|
135
|
-
verifyList,
|
|
136
|
-
verifyValue,
|
|
137
|
-
];
|