@playwright/mcp 0.0.36-alpha-2025-09-03 → 0.0.36-alpha-2025-09-04

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/cli.js +1 -1
  2. package/index.d.ts +1 -1
  3. package/index.js +2 -2
  4. package/lib/{browserContextFactory.js → browser/browserContextFactory.js} +76 -36
  5. package/lib/{browserServerBackend.js → browser/browserServerBackend.js} +24 -22
  6. package/lib/{utils → browser}/codegen.js +8 -3
  7. package/lib/{config.js → browser/config.js} +43 -26
  8. package/lib/{context.js → browser/context.js} +27 -30
  9. package/lib/{response.js → browser/response.js} +14 -14
  10. package/lib/{sessionLog.js → browser/sessionLog.js} +23 -18
  11. package/lib/{tab.js → browser/tab.js} +29 -27
  12. package/lib/{tools → browser/tools}/common.js +11 -9
  13. package/lib/{tools → browser/tools}/console.js +7 -5
  14. package/lib/{tools → browser/tools}/dialogs.js +9 -7
  15. package/lib/browser/tools/evaluate.js +88 -0
  16. package/lib/{tools → browser/tools}/files.js +8 -6
  17. package/lib/browser/tools/form.js +92 -0
  18. package/lib/{tools → browser/tools}/install.js +18 -14
  19. package/lib/browser/tools/keyboard.js +113 -0
  20. package/lib/{tools → browser/tools}/mouse.js +18 -16
  21. package/lib/{tools → browser/tools}/navigate.js +10 -8
  22. package/lib/{tools → browser/tools}/network.js +7 -5
  23. package/lib/browser/tools/pdf.js +76 -0
  24. package/lib/browser/tools/screenshot.js +115 -0
  25. package/lib/browser/tools/snapshot.js +175 -0
  26. package/lib/{tools → browser/tools}/tabs.js +9 -7
  27. package/lib/{tools → browser/tools}/tool.js +6 -2
  28. package/lib/{tools → browser/tools}/utils.js +10 -5
  29. package/lib/{tools → browser/tools}/verify.js +59 -24
  30. package/lib/{tools → browser/tools}/wait.js +10 -8
  31. package/lib/browser/tools.js +61 -0
  32. package/lib/extension/cdpRelay.js +85 -48
  33. package/lib/extension/extensionContextFactory.js +48 -11
  34. package/lib/extension/protocol.js +4 -1
  35. package/lib/index.js +47 -12
  36. package/lib/{utils/log.js → log.js} +11 -4
  37. package/lib/{utils/package.js → package.js} +9 -5
  38. package/lib/program.js +68 -39
  39. package/lib/sdk/bundle.js +79 -0
  40. package/lib/{mcp → sdk}/http.js +57 -17
  41. package/lib/{mcp → sdk}/inProcessTransport.js +15 -20
  42. package/lib/{mcp → sdk}/manualPromise.js +11 -9
  43. package/lib/{mcp → sdk}/mdb.js +77 -38
  44. package/lib/{mcp → sdk}/proxyBackend.js +53 -16
  45. package/lib/sdk/server.js +164 -0
  46. package/lib/{mcp → sdk}/tool.js +8 -4
  47. package/lib/vscode/host.js +64 -35
  48. package/lib/vscode/main.js +48 -13
  49. package/package.json +6 -7
  50. package/lib/loop/loop.js +0 -69
  51. package/lib/loop/loopClaude.js +0 -152
  52. package/lib/loop/loopOpenAI.js +0 -141
  53. package/lib/loop/main.js +0 -60
  54. package/lib/loopTools/context.js +0 -67
  55. package/lib/loopTools/main.js +0 -54
  56. package/lib/loopTools/perform.js +0 -32
  57. package/lib/loopTools/snapshot.js +0 -29
  58. package/lib/loopTools/tool.js +0 -18
  59. package/lib/mcp/server.js +0 -123
  60. package/lib/tools/evaluate.js +0 -53
  61. package/lib/tools/form.js +0 -57
  62. package/lib/tools/keyboard.js +0 -78
  63. package/lib/tools/pdf.js +0 -40
  64. package/lib/tools/screenshot.js +0 -79
  65. package/lib/tools/snapshot.js +0 -139
  66. package/lib/tools.js +0 -54
  67. package/lib/utils/fileUtils.js +0 -36
  68. package/lib/utils/guid.js +0 -22
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /**
2
3
  * Copyright (c) Microsoft Corporation.
3
4
  *
@@ -13,30 +14,33 @@
13
14
  * See the License for the specific language governing permissions and
14
15
  * limitations under the License.
15
16
  */
16
- import { fork } from 'child_process';
17
- import path from 'path';
18
- import { fileURLToPath } from 'url';
19
- import { z } from 'zod';
20
- import { defineTool } from './tool.js';
21
- const install = defineTool({
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ const child_process_1 = require("child_process");
22
+ const path_1 = __importDefault(require("path"));
23
+ const bundle_1 = require("../../sdk/bundle");
24
+ const tool_1 = require("./tool");
25
+ const install = (0, tool_1.defineTool)({
22
26
  capability: 'core-install',
23
27
  schema: {
24
28
  name: 'browser_install',
25
29
  title: 'Install the browser specified in the config',
26
30
  description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',
27
- inputSchema: z.object({}),
31
+ inputSchema: bundle_1.z.object({}),
28
32
  type: 'destructive',
29
33
  },
30
34
  handle: async (context, params, response) => {
31
- const channel = context.config.browser?.launchOptions?.channel ?? context.config.browser?.browserName ?? 'chrome';
32
- const cliUrl = import.meta.resolve('playwright/package.json');
33
- const cliPath = path.join(fileURLToPath(cliUrl), '..', 'cli.js');
34
- const child = fork(cliPath, ['install', channel], {
35
+ var _a, _b, _c, _d, _e, _f, _g;
36
+ const channel = (_e = (_c = (_b = (_a = context.config.browser) === null || _a === void 0 ? void 0 : _a.launchOptions) === null || _b === void 0 ? void 0 : _b.channel) !== null && _c !== void 0 ? _c : (_d = context.config.browser) === null || _d === void 0 ? void 0 : _d.browserName) !== null && _e !== void 0 ? _e : 'chrome';
37
+ const cliPath = path_1.default.join(require.resolve('playwright/package.json'), '../cli.js');
38
+ const child = (0, child_process_1.fork)(cliPath, ['install', channel], {
35
39
  stdio: 'pipe',
36
40
  });
37
41
  const output = [];
38
- child.stdout?.on('data', data => output.push(data.toString()));
39
- child.stderr?.on('data', data => output.push(data.toString()));
42
+ (_f = child.stdout) === null || _f === void 0 ? void 0 : _f.on('data', data => output.push(data.toString()));
43
+ (_g = child.stderr) === null || _g === void 0 ? void 0 : _g.on('data', data => output.push(data.toString()));
40
44
  await new Promise((resolve, reject) => {
41
45
  child.on('close', code => {
42
46
  if (code === 0)
@@ -48,6 +52,6 @@ const install = defineTool({
48
52
  response.setIncludeTabs();
49
53
  },
50
54
  });
51
- export default [
55
+ exports.default = [
52
56
  install,
53
57
  ];
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || (function () {
34
+ var ownKeys = function(o) {
35
+ ownKeys = Object.getOwnPropertyNames || function (o) {
36
+ var ar = [];
37
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
38
+ return ar;
39
+ };
40
+ return ownKeys(o);
41
+ };
42
+ return function (mod) {
43
+ if (mod && mod.__esModule) return mod;
44
+ var result = {};
45
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
46
+ __setModuleDefault(result, mod);
47
+ return result;
48
+ };
49
+ })();
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ const bundle_1 = require("../../sdk/bundle");
52
+ const tool_1 = require("./tool");
53
+ const snapshot_1 = require("./snapshot");
54
+ const utils_1 = require("./utils");
55
+ const javascript = __importStar(require("../codegen"));
56
+ const pressKey = (0, tool_1.defineTabTool)({
57
+ capability: 'core',
58
+ schema: {
59
+ name: 'browser_press_key',
60
+ title: 'Press a key',
61
+ description: 'Press a key on the keyboard',
62
+ inputSchema: bundle_1.z.object({
63
+ key: bundle_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
64
+ }),
65
+ type: 'destructive',
66
+ },
67
+ handle: async (tab, params, response) => {
68
+ response.setIncludeSnapshot();
69
+ response.addCode(`// Press ${params.key}`);
70
+ response.addCode(`await page.keyboard.press('${params.key}');`);
71
+ await tab.waitForCompletion(async () => {
72
+ await tab.page.keyboard.press(params.key);
73
+ });
74
+ },
75
+ });
76
+ const typeSchema = snapshot_1.elementSchema.extend({
77
+ text: bundle_1.z.string().describe('Text to type into the element'),
78
+ submit: bundle_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
79
+ slowly: bundle_1.z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
80
+ });
81
+ const type = (0, tool_1.defineTabTool)({
82
+ capability: 'core',
83
+ schema: {
84
+ name: 'browser_type',
85
+ title: 'Type text',
86
+ description: 'Type text into editable element',
87
+ inputSchema: typeSchema,
88
+ type: 'destructive',
89
+ },
90
+ handle: async (tab, params, response) => {
91
+ const locator = await tab.refLocator(params);
92
+ await tab.waitForCompletion(async () => {
93
+ if (params.slowly) {
94
+ response.setIncludeSnapshot();
95
+ response.addCode(`await page.${await (0, utils_1.generateLocator)(locator)}.pressSequentially(${javascript.quote(params.text)});`);
96
+ await locator.pressSequentially(params.text);
97
+ }
98
+ else {
99
+ response.addCode(`await page.${await (0, utils_1.generateLocator)(locator)}.fill(${javascript.quote(params.text)});`);
100
+ await locator.fill(params.text);
101
+ }
102
+ if (params.submit) {
103
+ response.setIncludeSnapshot();
104
+ response.addCode(`await page.${await (0, utils_1.generateLocator)(locator)}.press('Enter');`);
105
+ await locator.press('Enter');
106
+ }
107
+ });
108
+ },
109
+ });
110
+ exports.default = [
111
+ pressKey,
112
+ type,
113
+ ];
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /**
2
3
  * Copyright (c) Microsoft Corporation.
3
4
  *
@@ -13,20 +14,21 @@
13
14
  * See the License for the specific language governing permissions and
14
15
  * limitations under the License.
15
16
  */
16
- import { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- const elementSchema = z.object({
19
- element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const bundle_1 = require("../../sdk/bundle");
19
+ const tool_1 = require("./tool");
20
+ const elementSchema = bundle_1.z.object({
21
+ element: bundle_1.z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
20
22
  });
21
- const mouseMove = defineTabTool({
23
+ const mouseMove = (0, tool_1.defineTabTool)({
22
24
  capability: 'vision',
23
25
  schema: {
24
26
  name: 'browser_mouse_move_xy',
25
27
  title: 'Move mouse',
26
28
  description: 'Move mouse to a given position',
27
29
  inputSchema: elementSchema.extend({
28
- x: z.number().describe('X coordinate'),
29
- y: z.number().describe('Y coordinate'),
30
+ x: bundle_1.z.number().describe('X coordinate'),
31
+ y: bundle_1.z.number().describe('Y coordinate'),
30
32
  }),
31
33
  type: 'readOnly',
32
34
  },
@@ -38,15 +40,15 @@ const mouseMove = defineTabTool({
38
40
  });
39
41
  },
40
42
  });
41
- const mouseClick = defineTabTool({
43
+ const mouseClick = (0, tool_1.defineTabTool)({
42
44
  capability: 'vision',
43
45
  schema: {
44
46
  name: 'browser_mouse_click_xy',
45
47
  title: 'Click',
46
48
  description: 'Click left mouse button at a given position',
47
49
  inputSchema: elementSchema.extend({
48
- x: z.number().describe('X coordinate'),
49
- y: z.number().describe('Y coordinate'),
50
+ x: bundle_1.z.number().describe('X coordinate'),
51
+ y: bundle_1.z.number().describe('Y coordinate'),
50
52
  }),
51
53
  type: 'destructive',
52
54
  },
@@ -63,17 +65,17 @@ const mouseClick = defineTabTool({
63
65
  });
64
66
  },
65
67
  });
66
- const mouseDrag = defineTabTool({
68
+ const mouseDrag = (0, tool_1.defineTabTool)({
67
69
  capability: 'vision',
68
70
  schema: {
69
71
  name: 'browser_mouse_drag_xy',
70
72
  title: 'Drag mouse',
71
73
  description: 'Drag left mouse button to a given position',
72
74
  inputSchema: elementSchema.extend({
73
- startX: z.number().describe('Start X coordinate'),
74
- startY: z.number().describe('Start Y coordinate'),
75
- endX: z.number().describe('End X coordinate'),
76
- endY: z.number().describe('End Y coordinate'),
75
+ startX: bundle_1.z.number().describe('Start X coordinate'),
76
+ startY: bundle_1.z.number().describe('Start Y coordinate'),
77
+ endX: bundle_1.z.number().describe('End X coordinate'),
78
+ endY: bundle_1.z.number().describe('End Y coordinate'),
77
79
  }),
78
80
  type: 'destructive',
79
81
  },
@@ -92,7 +94,7 @@ const mouseDrag = defineTabTool({
92
94
  });
93
95
  },
94
96
  });
95
- export default [
97
+ exports.default = [
96
98
  mouseMove,
97
99
  mouseClick,
98
100
  mouseDrag,
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /**
2
3
  * Copyright (c) Microsoft Corporation.
3
4
  *
@@ -13,16 +14,17 @@
13
14
  * See the License for the specific language governing permissions and
14
15
  * limitations under the License.
15
16
  */
16
- import { z } from 'zod';
17
- import { defineTool, defineTabTool } from './tool.js';
18
- const navigate = defineTool({
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const bundle_1 = require("../../sdk/bundle");
19
+ const tool_1 = require("./tool");
20
+ const navigate = (0, tool_1.defineTool)({
19
21
  capability: 'core',
20
22
  schema: {
21
23
  name: 'browser_navigate',
22
24
  title: 'Navigate to a URL',
23
25
  description: 'Navigate to a URL',
24
- inputSchema: z.object({
25
- url: z.string().describe('The URL to navigate to'),
26
+ inputSchema: bundle_1.z.object({
27
+ url: bundle_1.z.string().describe('The URL to navigate to'),
26
28
  }),
27
29
  type: 'destructive',
28
30
  },
@@ -33,13 +35,13 @@ const navigate = defineTool({
33
35
  response.addCode(`await page.goto('${params.url}');`);
34
36
  },
35
37
  });
36
- const goBack = defineTabTool({
38
+ const goBack = (0, tool_1.defineTabTool)({
37
39
  capability: 'core',
38
40
  schema: {
39
41
  name: 'browser_navigate_back',
40
42
  title: 'Go back',
41
43
  description: 'Go back to the previous page',
42
- inputSchema: z.object({}),
44
+ inputSchema: bundle_1.z.object({}),
43
45
  type: 'readOnly',
44
46
  },
45
47
  handle: async (tab, params, response) => {
@@ -48,7 +50,7 @@ const goBack = defineTabTool({
48
50
  response.addCode(`await page.goBack();`);
49
51
  },
50
52
  });
51
- export default [
53
+ exports.default = [
52
54
  navigate,
53
55
  goBack,
54
56
  ];
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /**
2
3
  * Copyright (c) Microsoft Corporation.
3
4
  *
@@ -13,15 +14,16 @@
13
14
  * See the License for the specific language governing permissions and
14
15
  * limitations under the License.
15
16
  */
16
- import { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- const requests = defineTabTool({
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const bundle_1 = require("../../sdk/bundle");
19
+ const tool_1 = require("./tool");
20
+ const requests = (0, tool_1.defineTabTool)({
19
21
  capability: 'core',
20
22
  schema: {
21
23
  name: 'browser_network_requests',
22
24
  title: 'List network requests',
23
25
  description: 'Returns all network requests since loading the page',
24
- inputSchema: z.object({}),
26
+ inputSchema: bundle_1.z.object({}),
25
27
  type: 'readOnly',
26
28
  },
27
29
  handle: async (tab, params, response) => {
@@ -36,6 +38,6 @@ function renderRequest(request, response) {
36
38
  result.push(`=> [${response.status()}] ${response.statusText()}`);
37
39
  return result.join(' ');
38
40
  }
39
- export default [
41
+ exports.default = [
40
42
  requests,
41
43
  ];
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || (function () {
34
+ var ownKeys = function(o) {
35
+ ownKeys = Object.getOwnPropertyNames || function (o) {
36
+ var ar = [];
37
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
38
+ return ar;
39
+ };
40
+ return ownKeys(o);
41
+ };
42
+ return function (mod) {
43
+ if (mod && mod.__esModule) return mod;
44
+ var result = {};
45
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
46
+ __setModuleDefault(result, mod);
47
+ return result;
48
+ };
49
+ })();
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ const bundle_1 = require("../../sdk/bundle");
52
+ const tool_1 = require("./tool");
53
+ const javascript = __importStar(require("../codegen"));
54
+ const pdfSchema = bundle_1.z.object({
55
+ filename: bundle_1.z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
56
+ });
57
+ const pdf = (0, tool_1.defineTabTool)({
58
+ capability: 'pdf',
59
+ schema: {
60
+ name: 'browser_pdf_save',
61
+ title: 'Save as PDF',
62
+ description: 'Save page as PDF',
63
+ inputSchema: pdfSchema,
64
+ type: 'readOnly',
65
+ },
66
+ handle: async (tab, params, response) => {
67
+ var _a;
68
+ const fileName = await tab.context.outputFile((_a = params.filename) !== null && _a !== void 0 ? _a : `page-${new Date().toISOString()}.pdf`);
69
+ response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
70
+ response.addResult(`Saved page as ${fileName}`);
71
+ await tab.page.pdf({ path: fileName });
72
+ },
73
+ });
74
+ exports.default = [
75
+ pdf,
76
+ ];
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || (function () {
34
+ var ownKeys = function(o) {
35
+ ownKeys = Object.getOwnPropertyNames || function (o) {
36
+ var ar = [];
37
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
38
+ return ar;
39
+ };
40
+ return ownKeys(o);
41
+ };
42
+ return function (mod) {
43
+ if (mod && mod.__esModule) return mod;
44
+ var result = {};
45
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
46
+ __setModuleDefault(result, mod);
47
+ return result;
48
+ };
49
+ })();
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ const bundle_1 = require("../../sdk/bundle");
52
+ const tool_1 = require("./tool");
53
+ const javascript = __importStar(require("../codegen"));
54
+ const utils_1 = require("./utils");
55
+ const screenshotSchema = bundle_1.z.object({
56
+ type: bundle_1.z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
57
+ filename: bundle_1.z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
58
+ element: bundle_1.z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
59
+ ref: bundle_1.z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
60
+ fullPage: bundle_1.z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
61
+ }).refine(data => {
62
+ return !!data.element === !!data.ref;
63
+ }, {
64
+ message: 'Both element and ref must be provided or neither.',
65
+ path: ['ref', 'element']
66
+ }).refine(data => {
67
+ return !(data.fullPage && (data.element || data.ref));
68
+ }, {
69
+ message: 'fullPage cannot be used with element screenshots.',
70
+ path: ['fullPage']
71
+ });
72
+ const screenshot = (0, tool_1.defineTabTool)({
73
+ capability: 'core',
74
+ schema: {
75
+ name: 'browser_take_screenshot',
76
+ title: 'Take a screenshot',
77
+ description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
78
+ inputSchema: screenshotSchema,
79
+ type: 'readOnly',
80
+ },
81
+ handle: async (tab, params, response) => {
82
+ var _a;
83
+ const fileType = params.type || 'png';
84
+ const fileName = await tab.context.outputFile((_a = params.filename) !== null && _a !== void 0 ? _a : `page-${new Date().toISOString()}.${fileType}`);
85
+ const options = {
86
+ type: fileType,
87
+ quality: fileType === 'png' ? undefined : 90,
88
+ scale: 'css',
89
+ path: fileName,
90
+ ...(params.fullPage !== undefined && { fullPage: params.fullPage })
91
+ };
92
+ const isElementScreenshot = params.element && params.ref;
93
+ const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
94
+ response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
95
+ // Only get snapshot when element screenshot is needed
96
+ const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
97
+ if (locator)
98
+ response.addCode(`await page.${await (0, utils_1.generateLocator)(locator)}.screenshot(${javascript.formatObject(options)});`);
99
+ else
100
+ response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
101
+ const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
102
+ response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
103
+ // https://github.com/microsoft/playwright-mcp/issues/817
104
+ // Never return large images to LLM, saving them to the file system is enough.
105
+ if (!params.fullPage) {
106
+ response.addImage({
107
+ contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
108
+ data: buffer
109
+ });
110
+ }
111
+ }
112
+ });
113
+ exports.default = [
114
+ screenshot,
115
+ ];