@cotestdev/mcp_playwright 0.0.12 → 0.0.15
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/lib/mcp/browser/browserContextFactory.js +49 -13
- package/lib/mcp/browser/browserServerBackend.js +5 -2
- package/lib/mcp/browser/config.js +95 -23
- package/lib/mcp/browser/context.js +28 -3
- package/lib/mcp/browser/response.js +240 -57
- package/lib/mcp/browser/sessionLog.js +1 -1
- package/lib/mcp/browser/tab.js +96 -69
- package/lib/mcp/browser/tools/common.js +8 -8
- package/lib/mcp/browser/tools/console.js +6 -3
- package/lib/mcp/browser/tools/dialogs.js +13 -13
- package/lib/mcp/browser/tools/evaluate.js +9 -20
- package/lib/mcp/browser/tools/files.js +10 -5
- package/lib/mcp/browser/tools/form.js +11 -22
- package/lib/mcp/browser/tools/install.js +3 -3
- package/lib/mcp/browser/tools/keyboard.js +12 -12
- package/lib/mcp/browser/tools/mouse.js +14 -14
- package/lib/mcp/browser/tools/navigate.js +5 -5
- package/lib/mcp/browser/tools/network.js +16 -5
- package/lib/mcp/browser/tools/pdf.js +7 -18
- package/lib/mcp/browser/tools/runCode.js +77 -0
- package/lib/mcp/browser/tools/screenshot.js +44 -33
- package/lib/mcp/browser/tools/snapshot.js +42 -33
- package/lib/mcp/browser/tools/tabs.js +7 -10
- package/lib/mcp/browser/tools/tool.js +8 -7
- package/lib/mcp/browser/tools/tracing.js +4 -4
- package/lib/mcp/browser/tools/utils.js +50 -52
- package/lib/mcp/browser/tools/verify.js +23 -34
- package/lib/mcp/browser/tools/wait.js +6 -6
- package/lib/mcp/browser/tools.js +4 -3
- package/lib/mcp/cli.js +17 -17
- package/lib/mcp/extension/cdpRelay.js +1 -1
- package/lib/mcp/extension/extensionContextFactory.js +4 -3
- package/lib/mcp/log.js +2 -2
- package/lib/mcp/program.js +21 -29
- package/lib/mcp/sdk/exports.js +1 -5
- package/lib/mcp/sdk/http.js +37 -50
- package/lib/mcp/sdk/server.js +61 -9
- package/lib/mcp/sdk/tool.js +5 -4
- package/lib/mcp/test/browserBackend.js +67 -61
- package/lib/mcp/test/generatorTools.js +122 -0
- package/lib/mcp/test/plannerTools.js +144 -0
- package/lib/mcp/test/seed.js +82 -0
- package/lib/mcp/test/streams.js +10 -7
- package/lib/mcp/test/testBackend.js +44 -24
- package/lib/mcp/test/testContext.js +243 -14
- package/lib/mcp/test/testTools.js +23 -109
- package/lib/util.js +12 -6
- package/package.json +1 -1
package/lib/mcp/sdk/exports.js
CHANGED
|
@@ -16,17 +16,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
16
|
var exports_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(exports_exports);
|
|
18
18
|
__reExport(exports_exports, require("./inProcessTransport"), module.exports);
|
|
19
|
-
__reExport(exports_exports, require("./proxyBackend"), module.exports);
|
|
20
19
|
__reExport(exports_exports, require("./server"), module.exports);
|
|
21
20
|
__reExport(exports_exports, require("./tool"), module.exports);
|
|
22
21
|
__reExport(exports_exports, require("./http"), module.exports);
|
|
23
|
-
__reExport(exports_exports, require("./mdb"), module.exports);
|
|
24
22
|
// Annotate the CommonJS export names for ESM import in node:
|
|
25
23
|
0 && (module.exports = {
|
|
26
24
|
...require("./inProcessTransport"),
|
|
27
|
-
...require("./proxyBackend"),
|
|
28
25
|
...require("./server"),
|
|
29
26
|
...require("./tool"),
|
|
30
|
-
...require("./http")
|
|
31
|
-
...require("./mdb")
|
|
27
|
+
...require("./http")
|
|
32
28
|
});
|
package/lib/mcp/sdk/http.js
CHANGED
|
@@ -28,61 +28,63 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var http_exports = {};
|
|
30
30
|
__export(http_exports, {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
startHttpServer: () => startHttpServer
|
|
31
|
+
addressToString: () => addressToString,
|
|
32
|
+
startMcpHttpServer: () => startMcpHttpServer
|
|
34
33
|
});
|
|
35
34
|
module.exports = __toCommonJS(http_exports);
|
|
36
35
|
var import_assert = __toESM(require("assert"));
|
|
37
|
-
var import_http = __toESM(require("http"));
|
|
38
36
|
var import_crypto = __toESM(require("crypto"));
|
|
39
37
|
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
40
|
-
var mcpBundle = __toESM(require("
|
|
38
|
+
var mcpBundle = __toESM(require("playwright-core/lib/mcpBundle"));
|
|
39
|
+
var import_utils = require("playwright-core/lib/utils");
|
|
41
40
|
var mcpServer = __toESM(require("./server"));
|
|
42
41
|
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
|
|
43
|
-
async function
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
await new Promise((resolve, reject) => {
|
|
48
|
-
httpServer.on("error", reject);
|
|
49
|
-
abortSignal?.addEventListener("abort", () => {
|
|
50
|
-
httpServer.close();
|
|
51
|
-
reject(new Error("Aborted"));
|
|
52
|
-
});
|
|
53
|
-
httpServer.listen(port, host, () => {
|
|
54
|
-
resolve();
|
|
55
|
-
httpServer.removeListener("error", reject);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
return httpServer;
|
|
42
|
+
async function startMcpHttpServer(config, serverBackendFactory, allowedHosts) {
|
|
43
|
+
const httpServer = (0, import_utils.createHttpServer)();
|
|
44
|
+
await (0, import_utils.startHttpServer)(httpServer, config);
|
|
45
|
+
return await installHttpTransport(httpServer, serverBackendFactory, allowedHosts);
|
|
59
46
|
}
|
|
60
|
-
function
|
|
47
|
+
function addressToString(address, options) {
|
|
61
48
|
(0, import_assert.default)(address, "Could not bind server socket");
|
|
62
49
|
if (typeof address === "string")
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return `http://${resolvedHost}:${resolvedPort}`;
|
|
50
|
+
throw new Error("Unexpected address type: " + address);
|
|
51
|
+
let host = address.family === "IPv4" ? address.address : `[${address.address}]`;
|
|
52
|
+
if (options.normalizeLoopback && (host === "0.0.0.0" || host === "[::]" || host === "[::1]" || host === "127.0.0.1"))
|
|
53
|
+
host = "localhost";
|
|
54
|
+
return `${options.protocol}://${host}:${address.port}`;
|
|
69
55
|
}
|
|
70
|
-
async function installHttpTransport(httpServer, serverBackendFactory) {
|
|
56
|
+
async function installHttpTransport(httpServer, serverBackendFactory, allowedHosts) {
|
|
57
|
+
const url = addressToString(httpServer.address(), { protocol: "http", normalizeLoopback: true });
|
|
58
|
+
const host = new URL(url).host;
|
|
59
|
+
allowedHosts = (allowedHosts || [host]).map((h) => h.toLowerCase());
|
|
60
|
+
const allowAnyHost = allowedHosts.includes("*");
|
|
71
61
|
const sseSessions = /* @__PURE__ */ new Map();
|
|
72
62
|
const streamableSessions = /* @__PURE__ */ new Map();
|
|
73
63
|
httpServer.on("request", async (req, res) => {
|
|
74
|
-
|
|
75
|
-
|
|
64
|
+
if (!allowAnyHost) {
|
|
65
|
+
const host2 = req.headers.host?.toLowerCase();
|
|
66
|
+
if (!host2) {
|
|
67
|
+
res.statusCode = 400;
|
|
68
|
+
return res.end("Missing host");
|
|
69
|
+
}
|
|
70
|
+
if (!allowedHosts.includes(host2)) {
|
|
71
|
+
res.statusCode = 403;
|
|
72
|
+
return res.end("Access is only allowed at " + allowedHosts.join(", "));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const url2 = new URL(`http://localhost${req.url}`);
|
|
76
|
+
if (url2.pathname === "/killkillkill" && req.method === "GET") {
|
|
76
77
|
res.statusCode = 200;
|
|
77
78
|
res.end("Killing process");
|
|
78
79
|
process.emit("SIGINT");
|
|
79
80
|
return;
|
|
80
81
|
}
|
|
81
|
-
if (
|
|
82
|
-
await handleSSE(serverBackendFactory, req, res,
|
|
82
|
+
if (url2.pathname.startsWith("/sse"))
|
|
83
|
+
await handleSSE(serverBackendFactory, req, res, url2, sseSessions);
|
|
83
84
|
else
|
|
84
85
|
await handleStreamable(serverBackendFactory, req, res, streamableSessions);
|
|
85
86
|
});
|
|
87
|
+
return url;
|
|
86
88
|
}
|
|
87
89
|
async function handleSSE(serverBackendFactory, req, res, url, sessions) {
|
|
88
90
|
if (req.method === "POST") {
|
|
@@ -143,23 +145,8 @@ async function handleStreamable(serverBackendFactory, req, res, sessions) {
|
|
|
143
145
|
res.statusCode = 400;
|
|
144
146
|
res.end("Invalid request");
|
|
145
147
|
}
|
|
146
|
-
function decorateServer(server) {
|
|
147
|
-
const sockets = /* @__PURE__ */ new Set();
|
|
148
|
-
server.on("connection", (socket) => {
|
|
149
|
-
sockets.add(socket);
|
|
150
|
-
socket.once("close", () => sockets.delete(socket));
|
|
151
|
-
});
|
|
152
|
-
const close = server.close;
|
|
153
|
-
server.close = (callback) => {
|
|
154
|
-
for (const socket of sockets)
|
|
155
|
-
socket.destroy();
|
|
156
|
-
sockets.clear();
|
|
157
|
-
return close.call(server, callback);
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
148
|
// Annotate the CommonJS export names for ESM import in node:
|
|
161
149
|
0 && (module.exports = {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
startHttpServer
|
|
150
|
+
addressToString,
|
|
151
|
+
startMcpHttpServer
|
|
165
152
|
});
|
package/lib/mcp/sdk/server.js
CHANGED
|
@@ -32,23 +32,33 @@ __export(server_exports, {
|
|
|
32
32
|
createServer: () => createServer,
|
|
33
33
|
firstRootPath: () => firstRootPath,
|
|
34
34
|
start: () => start,
|
|
35
|
+
wrapInClient: () => wrapInClient,
|
|
35
36
|
wrapInProcess: () => wrapInProcess
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(server_exports);
|
|
38
39
|
var import_url = require("url");
|
|
39
40
|
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
40
|
-
var mcpBundle = __toESM(require("
|
|
41
|
+
var mcpBundle = __toESM(require("playwright-core/lib/mcpBundle"));
|
|
41
42
|
var import_http = require("./http");
|
|
42
43
|
var import_inProcessTransport = require("./inProcessTransport");
|
|
43
44
|
const serverDebug = (0, import_utilsBundle.debug)("pw:mcp:server");
|
|
45
|
+
const serverDebugResponse = (0, import_utilsBundle.debug)("pw:mcp:server:response");
|
|
44
46
|
async function connect(factory, transport, runHeartbeat) {
|
|
45
47
|
const server = createServer(factory.name, factory.version, factory.create(), runHeartbeat);
|
|
46
48
|
await server.connect(transport);
|
|
47
49
|
}
|
|
48
|
-
|
|
50
|
+
function wrapInProcess(backend) {
|
|
49
51
|
const server = createServer("Internal", "0.0.0", backend, false);
|
|
50
52
|
return new import_inProcessTransport.InProcessTransport(server);
|
|
51
53
|
}
|
|
54
|
+
async function wrapInClient(backend, options) {
|
|
55
|
+
const server = createServer("Internal", "0.0.0", backend, false);
|
|
56
|
+
const transport = new import_inProcessTransport.InProcessTransport(server);
|
|
57
|
+
const client = new mcpBundle.Client({ name: options.name, version: options.version });
|
|
58
|
+
await client.connect(transport);
|
|
59
|
+
await client.ping();
|
|
60
|
+
return client;
|
|
61
|
+
}
|
|
52
62
|
function createServer(name, version, backend, runHeartbeat) {
|
|
53
63
|
const server = new mcpBundle.Server({ name, version }, {
|
|
54
64
|
capabilities: {
|
|
@@ -61,13 +71,30 @@ function createServer(name, version, backend, runHeartbeat) {
|
|
|
61
71
|
return { tools };
|
|
62
72
|
});
|
|
63
73
|
let initializePromise;
|
|
64
|
-
server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request) => {
|
|
74
|
+
server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request, extra) => {
|
|
65
75
|
serverDebug("callTool", request);
|
|
76
|
+
const progressToken = request.params._meta?.progressToken;
|
|
77
|
+
let progressCounter = 0;
|
|
78
|
+
const progress = progressToken ? (params) => {
|
|
79
|
+
extra.sendNotification({
|
|
80
|
+
method: "notifications/progress",
|
|
81
|
+
params: {
|
|
82
|
+
progressToken,
|
|
83
|
+
progress: params.progress ?? ++progressCounter,
|
|
84
|
+
total: params.total,
|
|
85
|
+
message: params.message
|
|
86
|
+
}
|
|
87
|
+
}).catch(serverDebug);
|
|
88
|
+
} : () => {
|
|
89
|
+
};
|
|
66
90
|
try {
|
|
67
91
|
if (!initializePromise)
|
|
68
92
|
initializePromise = initializeServer(server, backend, runHeartbeat);
|
|
69
93
|
await initializePromise;
|
|
70
|
-
|
|
94
|
+
const toolResult = await backend.callTool(request.params.name, request.params.arguments || {}, progress);
|
|
95
|
+
const mergedResult = mergeTextParts(toolResult);
|
|
96
|
+
serverDebugResponse("callResult", mergedResult);
|
|
97
|
+
return mergedResult;
|
|
71
98
|
} catch (error) {
|
|
72
99
|
return {
|
|
73
100
|
content: [{ type: "text", text: "### Result\n" + String(error) }],
|
|
@@ -94,7 +121,7 @@ const initializeServer = async (server, backend, runHeartbeat) => {
|
|
|
94
121
|
roots: clientRoots,
|
|
95
122
|
timestamp: Date.now()
|
|
96
123
|
};
|
|
97
|
-
await backend.initialize?.(
|
|
124
|
+
await backend.initialize?.(clientInfo);
|
|
98
125
|
if (runHeartbeat)
|
|
99
126
|
startHeartbeat(server);
|
|
100
127
|
};
|
|
@@ -123,9 +150,7 @@ async function start(serverBackendFactory, options) {
|
|
|
123
150
|
await connect(serverBackendFactory, new mcpBundle.StdioServerTransport(), false);
|
|
124
151
|
return;
|
|
125
152
|
}
|
|
126
|
-
const
|
|
127
|
-
await (0, import_http.installHttpTransport)(httpServer, serverBackendFactory);
|
|
128
|
-
const url = (0, import_http.httpAddressToString)(httpServer.address());
|
|
153
|
+
const url = await (0, import_http.startMcpHttpServer)(options, serverBackendFactory, options.allowedHosts);
|
|
129
154
|
const mcpConfig = { mcpServers: {} };
|
|
130
155
|
mcpConfig.mcpServers[serverBackendFactory.nameInConfig] = {
|
|
131
156
|
url: `${url}/mcp`
|
|
@@ -143,7 +168,33 @@ function firstRootPath(clientInfo) {
|
|
|
143
168
|
return void 0;
|
|
144
169
|
const firstRootUri = clientInfo.roots[0]?.uri;
|
|
145
170
|
const url = firstRootUri ? new URL(firstRootUri) : void 0;
|
|
146
|
-
|
|
171
|
+
try {
|
|
172
|
+
return url ? (0, import_url.fileURLToPath)(url) : void 0;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
serverDebug(error);
|
|
175
|
+
return void 0;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function mergeTextParts(result) {
|
|
179
|
+
const content = [];
|
|
180
|
+
const testParts = [];
|
|
181
|
+
for (const part of result.content) {
|
|
182
|
+
if (part.type === "text") {
|
|
183
|
+
testParts.push(part.text);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (testParts.length > 0) {
|
|
187
|
+
content.push({ type: "text", text: testParts.join("\n") });
|
|
188
|
+
testParts.length = 0;
|
|
189
|
+
}
|
|
190
|
+
content.push(part);
|
|
191
|
+
}
|
|
192
|
+
if (testParts.length > 0)
|
|
193
|
+
content.push({ type: "text", text: testParts.join("\n") });
|
|
194
|
+
return {
|
|
195
|
+
...result,
|
|
196
|
+
content
|
|
197
|
+
};
|
|
147
198
|
}
|
|
148
199
|
// Annotate the CommonJS export names for ESM import in node:
|
|
149
200
|
0 && (module.exports = {
|
|
@@ -151,5 +202,6 @@ function firstRootPath(clientInfo) {
|
|
|
151
202
|
createServer,
|
|
152
203
|
firstRootPath,
|
|
153
204
|
start,
|
|
205
|
+
wrapInClient,
|
|
154
206
|
wrapInProcess
|
|
155
207
|
});
|
package/lib/mcp/sdk/tool.js
CHANGED
|
@@ -22,16 +22,17 @@ __export(tool_exports, {
|
|
|
22
22
|
toMcpTool: () => toMcpTool
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(tool_exports);
|
|
25
|
-
var
|
|
25
|
+
var import_mcpBundle = require("playwright-core/lib/mcpBundle");
|
|
26
26
|
function toMcpTool(tool) {
|
|
27
|
+
const readOnly = tool.type === "readOnly" || tool.type === "assertion";
|
|
27
28
|
return {
|
|
28
29
|
name: tool.name,
|
|
29
30
|
description: tool.description,
|
|
30
|
-
inputSchema: (0,
|
|
31
|
+
inputSchema: (0, import_mcpBundle.zodToJsonSchema)(tool.inputSchema, { strictUnions: true }),
|
|
31
32
|
annotations: {
|
|
32
33
|
title: tool.title,
|
|
33
|
-
readOnlyHint:
|
|
34
|
-
destructiveHint:
|
|
34
|
+
readOnlyHint: readOnly,
|
|
35
|
+
destructiveHint: !readOnly,
|
|
35
36
|
openWorldHint: true
|
|
36
37
|
}
|
|
37
38
|
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,76 +15,84 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var browserBackend_exports = {};
|
|
30
20
|
__export(browserBackend_exports, {
|
|
31
|
-
|
|
32
|
-
runBrowserBackendOnError: () => runBrowserBackendOnError
|
|
21
|
+
createCustomMessageHandler: () => createCustomMessageHandler
|
|
33
22
|
});
|
|
34
23
|
module.exports = __toCommonJS(browserBackend_exports);
|
|
35
|
-
var mcp = __toESM(require("../sdk/exports"));
|
|
36
|
-
var import_globals = require("../../common/globals");
|
|
37
|
-
var import_util = require("../../util");
|
|
38
24
|
var import_config = require("../browser/config");
|
|
39
25
|
var import_browserServerBackend = require("../browser/browserServerBackend");
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
${snapshot}`;
|
|
71
|
-
const config = {
|
|
72
|
-
...import_config.defaultConfig,
|
|
73
|
-
capabilities: ["testing"]
|
|
26
|
+
var import_tab = require("../browser/tab");
|
|
27
|
+
var import_util = require("../../util");
|
|
28
|
+
var import_browserContextFactory = require("../browser/browserContextFactory");
|
|
29
|
+
function createCustomMessageHandler(testInfo, context) {
|
|
30
|
+
let backend;
|
|
31
|
+
return async (data) => {
|
|
32
|
+
if (data.initialize) {
|
|
33
|
+
if (backend)
|
|
34
|
+
throw new Error("MCP backend is already initialized");
|
|
35
|
+
backend = new import_browserServerBackend.BrowserServerBackend({ ...import_config.defaultConfig, capabilities: ["testing"] }, (0, import_browserContextFactory.identityBrowserContextFactory)(context));
|
|
36
|
+
await backend.initialize(data.initialize.clientInfo);
|
|
37
|
+
const pausedMessage = await generatePausedMessage(testInfo, context);
|
|
38
|
+
return { initialize: { pausedMessage } };
|
|
39
|
+
}
|
|
40
|
+
if (data.listTools) {
|
|
41
|
+
if (!backend)
|
|
42
|
+
throw new Error("MCP backend is not initialized");
|
|
43
|
+
return { listTools: await backend.listTools() };
|
|
44
|
+
}
|
|
45
|
+
if (data.callTool) {
|
|
46
|
+
if (!backend)
|
|
47
|
+
throw new Error("MCP backend is not initialized");
|
|
48
|
+
return { callTool: await backend.callTool(data.callTool.name, data.callTool.arguments) };
|
|
49
|
+
}
|
|
50
|
+
if (data.close) {
|
|
51
|
+
backend?.serverClosed();
|
|
52
|
+
backend = void 0;
|
|
53
|
+
return { close: {} };
|
|
54
|
+
}
|
|
55
|
+
throw new Error("Unknown MCP request");
|
|
74
56
|
};
|
|
75
|
-
await mcp.runOnPauseBackendLoop(new import_browserServerBackend.BrowserServerBackend(config, identityFactory(context)), introMessage);
|
|
76
57
|
}
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
58
|
+
async function generatePausedMessage(testInfo, context) {
|
|
59
|
+
const lines = [];
|
|
60
|
+
if (testInfo.errors.length) {
|
|
61
|
+
lines.push(`### Paused on error:`);
|
|
62
|
+
for (const error of testInfo.errors)
|
|
63
|
+
lines.push((0, import_util.stripAnsiEscapes)(error.message || ""));
|
|
64
|
+
} else {
|
|
65
|
+
lines.push(`### Paused at end of test. ready for interaction`);
|
|
66
|
+
}
|
|
67
|
+
for (let i = 0; i < context.pages().length; i++) {
|
|
68
|
+
const page = context.pages()[i];
|
|
69
|
+
const stateSuffix = context.pages().length > 1 ? i + 1 + " of " + context.pages().length : "state";
|
|
70
|
+
lines.push(
|
|
71
|
+
"",
|
|
72
|
+
`### Page ${stateSuffix}`,
|
|
73
|
+
`- Page URL: ${page.url()}`,
|
|
74
|
+
`- Page Title: ${await page.title()}`.trim()
|
|
75
|
+
);
|
|
76
|
+
let console = testInfo.errors.length ? await import_tab.Tab.collectConsoleMessages(page) : [];
|
|
77
|
+
console = console.filter((msg) => msg.type === "error");
|
|
78
|
+
if (console.length) {
|
|
79
|
+
lines.push("- Console Messages:");
|
|
80
|
+
for (const message of console)
|
|
81
|
+
lines.push(` - ${message.toString()}`);
|
|
85
82
|
}
|
|
86
|
-
|
|
83
|
+
lines.push(
|
|
84
|
+
`- Page Snapshot:`,
|
|
85
|
+
"```yaml",
|
|
86
|
+
(await page._snapshotForAI()).full,
|
|
87
|
+
"```"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
lines.push("");
|
|
91
|
+
if (testInfo.errors.length)
|
|
92
|
+
lines.push(`### Task`, `Try recovering from the error prior to continuing`);
|
|
93
|
+
return lines.join("\n");
|
|
87
94
|
}
|
|
88
95
|
// Annotate the CommonJS export names for ESM import in node:
|
|
89
96
|
0 && (module.exports = {
|
|
90
|
-
|
|
91
|
-
runBrowserBackendOnError
|
|
97
|
+
createCustomMessageHandler
|
|
92
98
|
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var generatorTools_exports = {};
|
|
30
|
+
__export(generatorTools_exports, {
|
|
31
|
+
generatorReadLog: () => generatorReadLog,
|
|
32
|
+
generatorWriteTest: () => generatorWriteTest,
|
|
33
|
+
setupPage: () => setupPage
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(generatorTools_exports);
|
|
36
|
+
var import_fs = __toESM(require("fs"));
|
|
37
|
+
var import_path = __toESM(require("path"));
|
|
38
|
+
var import_mcpBundle = require("playwright-core/lib/mcpBundle");
|
|
39
|
+
var import_testTool = require("./testTool");
|
|
40
|
+
var import_testContext = require("./testContext");
|
|
41
|
+
const setupPage = (0, import_testTool.defineTestTool)({
|
|
42
|
+
schema: {
|
|
43
|
+
name: "generator_setup_page",
|
|
44
|
+
title: "Setup generator page",
|
|
45
|
+
description: "Setup the page for test.",
|
|
46
|
+
inputSchema: import_mcpBundle.z.object({
|
|
47
|
+
plan: import_mcpBundle.z.string().describe("The plan for the test. This should be the actual test plan with all the steps."),
|
|
48
|
+
project: import_mcpBundle.z.string().optional().describe('Project to use for setup. For example: "chromium", if no project is provided uses the first project in the config.'),
|
|
49
|
+
seedFile: import_mcpBundle.z.string().optional().describe('A seed file contains a single test that is used to setup the page for testing, for example: "tests/seed.spec.ts". If no seed file is provided, a default seed file is created.')
|
|
50
|
+
}),
|
|
51
|
+
type: "readOnly"
|
|
52
|
+
},
|
|
53
|
+
handle: async (context, params) => {
|
|
54
|
+
const seed = await context.getOrCreateSeedFile(params.seedFile, params.project);
|
|
55
|
+
context.generatorJournal = new import_testContext.GeneratorJournal(context.rootPath, params.plan, seed);
|
|
56
|
+
const { output, status } = await context.runSeedTest(seed.file, seed.projectName);
|
|
57
|
+
return { content: [{ type: "text", text: output }], isError: status !== "paused" };
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const generatorReadLog = (0, import_testTool.defineTestTool)({
|
|
61
|
+
schema: {
|
|
62
|
+
name: "generator_read_log",
|
|
63
|
+
title: "Retrieve test log",
|
|
64
|
+
description: "Retrieve the performed test log",
|
|
65
|
+
inputSchema: import_mcpBundle.z.object({}),
|
|
66
|
+
type: "readOnly"
|
|
67
|
+
},
|
|
68
|
+
handle: async (context) => {
|
|
69
|
+
if (!context.generatorJournal)
|
|
70
|
+
throw new Error(`Please setup page using "${setupPage.schema.name}" first.`);
|
|
71
|
+
const result = context.generatorJournal.journal();
|
|
72
|
+
return { content: [{
|
|
73
|
+
type: "text",
|
|
74
|
+
text: result
|
|
75
|
+
}] };
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const generatorWriteTest = (0, import_testTool.defineTestTool)({
|
|
79
|
+
schema: {
|
|
80
|
+
name: "generator_write_test",
|
|
81
|
+
title: "Write test",
|
|
82
|
+
description: "Write the generated test to the test file",
|
|
83
|
+
inputSchema: import_mcpBundle.z.object({
|
|
84
|
+
fileName: import_mcpBundle.z.string().describe("The file to write the test to"),
|
|
85
|
+
code: import_mcpBundle.z.string().describe("The generated test code")
|
|
86
|
+
}),
|
|
87
|
+
type: "readOnly"
|
|
88
|
+
},
|
|
89
|
+
handle: async (context, params) => {
|
|
90
|
+
if (!context.generatorJournal)
|
|
91
|
+
throw new Error(`Please setup page using "${setupPage.schema.name}" first.`);
|
|
92
|
+
const testRunner = context.existingTestRunner();
|
|
93
|
+
if (!testRunner)
|
|
94
|
+
throw new Error("No test runner found, please setup page and perform actions first.");
|
|
95
|
+
const config = await testRunner.loadConfig();
|
|
96
|
+
const dirs = [];
|
|
97
|
+
for (const project of config.projects) {
|
|
98
|
+
const testDir = import_path.default.relative(context.rootPath, project.project.testDir).replace(/\\/g, "/");
|
|
99
|
+
const fileName = params.fileName.replace(/\\/g, "/");
|
|
100
|
+
if (fileName.startsWith(testDir)) {
|
|
101
|
+
const resolvedFile = import_path.default.resolve(context.rootPath, fileName);
|
|
102
|
+
await import_fs.default.promises.mkdir(import_path.default.dirname(resolvedFile), { recursive: true });
|
|
103
|
+
await import_fs.default.promises.writeFile(resolvedFile, params.code);
|
|
104
|
+
return {
|
|
105
|
+
content: [{
|
|
106
|
+
type: "text",
|
|
107
|
+
text: `### Result
|
|
108
|
+
Test written to ${params.fileName}`
|
|
109
|
+
}]
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
dirs.push(testDir);
|
|
113
|
+
}
|
|
114
|
+
throw new Error(`Test file did not match any of the test dirs: ${dirs.join(", ")}`);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
118
|
+
0 && (module.exports = {
|
|
119
|
+
generatorReadLog,
|
|
120
|
+
generatorWriteTest,
|
|
121
|
+
setupPage
|
|
122
|
+
});
|