@playwright/mcp 0.0.31 → 0.0.33

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 (52) hide show
  1. package/README.md +27 -6
  2. package/config.d.ts +5 -0
  3. package/index.d.ts +1 -6
  4. package/lib/browserContextFactory.js +64 -54
  5. package/lib/browserServerBackend.js +121 -0
  6. package/lib/config.js +10 -9
  7. package/lib/context.js +107 -182
  8. package/lib/extension/cdpRelay.js +346 -0
  9. package/lib/extension/extensionContextFactory.js +56 -0
  10. package/lib/extension/main.js +26 -0
  11. package/lib/httpServer.js +20 -182
  12. package/lib/index.js +6 -3
  13. package/lib/loop/loop.js +69 -0
  14. package/lib/loop/loopClaude.js +152 -0
  15. package/lib/loop/loopOpenAI.js +141 -0
  16. package/lib/loop/main.js +60 -0
  17. package/lib/loopTools/context.js +66 -0
  18. package/lib/loopTools/main.js +49 -0
  19. package/lib/loopTools/perform.js +32 -0
  20. package/lib/loopTools/snapshot.js +29 -0
  21. package/lib/loopTools/tool.js +18 -0
  22. package/lib/mcp/inProcessTransport.js +72 -0
  23. package/lib/mcp/server.js +93 -0
  24. package/lib/{transport.js → mcp/transport.js} +30 -42
  25. package/lib/package.js +3 -3
  26. package/lib/program.js +39 -9
  27. package/lib/response.js +165 -0
  28. package/lib/sessionLog.js +121 -0
  29. package/lib/tab.js +138 -24
  30. package/lib/tools/common.js +10 -23
  31. package/lib/tools/console.js +4 -15
  32. package/lib/tools/dialogs.js +12 -17
  33. package/lib/tools/evaluate.js +12 -21
  34. package/lib/tools/files.js +9 -16
  35. package/lib/tools/install.js +3 -7
  36. package/lib/tools/keyboard.js +28 -42
  37. package/lib/tools/mouse.js +27 -50
  38. package/lib/tools/navigate.js +12 -35
  39. package/lib/tools/network.js +5 -15
  40. package/lib/tools/pdf.js +7 -16
  41. package/lib/tools/screenshot.js +35 -33
  42. package/lib/tools/snapshot.js +44 -69
  43. package/lib/tools/tabs.js +10 -41
  44. package/lib/tools/tool.js +15 -0
  45. package/lib/tools/utils.js +2 -9
  46. package/lib/tools/wait.js +3 -6
  47. package/lib/tools.js +3 -0
  48. package/lib/utils.js +26 -0
  49. package/package.json +11 -6
  50. package/lib/connection.js +0 -81
  51. package/lib/pageSnapshot.js +0 -43
  52. package/lib/server.js +0 -48
@@ -14,11 +14,11 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool } from './tool.js';
18
18
  const elementSchema = z.object({
19
19
  element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
20
20
  });
21
- const mouseMove = defineTool({
21
+ const mouseMove = defineTabTool({
22
22
  capability: 'vision',
23
23
  schema: {
24
24
  name: 'browser_mouse_move_xy',
@@ -30,22 +30,15 @@ const mouseMove = defineTool({
30
30
  }),
31
31
  type: 'readOnly',
32
32
  },
33
- handle: async (context, params) => {
34
- const tab = context.currentTabOrDie();
35
- const code = [
36
- `// Move mouse to (${params.x}, ${params.y})`,
37
- `await page.mouse.move(${params.x}, ${params.y});`,
38
- ];
39
- const action = () => tab.page.mouse.move(params.x, params.y);
40
- return {
41
- code,
42
- action,
43
- captureSnapshot: false,
44
- waitForNetwork: false
45
- };
33
+ handle: async (tab, params, response) => {
34
+ response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
35
+ response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
36
+ await tab.waitForCompletion(async () => {
37
+ await tab.page.mouse.move(params.x, params.y);
38
+ });
46
39
  },
47
40
  });
48
- const mouseClick = defineTool({
41
+ const mouseClick = defineTabTool({
49
42
  capability: 'vision',
50
43
  schema: {
51
44
  name: 'browser_mouse_click_xy',
@@ -57,28 +50,20 @@ const mouseClick = defineTool({
57
50
  }),
58
51
  type: 'destructive',
59
52
  },
60
- handle: async (context, params) => {
61
- const tab = context.currentTabOrDie();
62
- const code = [
63
- `// Click mouse at coordinates (${params.x}, ${params.y})`,
64
- `await page.mouse.move(${params.x}, ${params.y});`,
65
- `await page.mouse.down();`,
66
- `await page.mouse.up();`,
67
- ];
68
- const action = async () => {
53
+ handle: async (tab, params, response) => {
54
+ response.setIncludeSnapshot();
55
+ response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
56
+ response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
57
+ response.addCode(`await page.mouse.down();`);
58
+ response.addCode(`await page.mouse.up();`);
59
+ await tab.waitForCompletion(async () => {
69
60
  await tab.page.mouse.move(params.x, params.y);
70
61
  await tab.page.mouse.down();
71
62
  await tab.page.mouse.up();
72
- };
73
- return {
74
- code,
75
- action,
76
- captureSnapshot: false,
77
- waitForNetwork: true,
78
- };
63
+ });
79
64
  },
80
65
  });
81
- const mouseDrag = defineTool({
66
+ const mouseDrag = defineTabTool({
82
67
  capability: 'vision',
83
68
  schema: {
84
69
  name: 'browser_mouse_drag_xy',
@@ -92,27 +77,19 @@ const mouseDrag = defineTool({
92
77
  }),
93
78
  type: 'destructive',
94
79
  },
95
- handle: async (context, params) => {
96
- const tab = context.currentTabOrDie();
97
- const code = [
98
- `// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`,
99
- `await page.mouse.move(${params.startX}, ${params.startY});`,
100
- `await page.mouse.down();`,
101
- `await page.mouse.move(${params.endX}, ${params.endY});`,
102
- `await page.mouse.up();`,
103
- ];
104
- const action = async () => {
80
+ handle: async (tab, params, response) => {
81
+ response.setIncludeSnapshot();
82
+ response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
83
+ response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
84
+ response.addCode(`await page.mouse.down();`);
85
+ response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
86
+ response.addCode(`await page.mouse.up();`);
87
+ await tab.waitForCompletion(async () => {
105
88
  await tab.page.mouse.move(params.startX, params.startY);
106
89
  await tab.page.mouse.down();
107
90
  await tab.page.mouse.move(params.endX, params.endY);
108
91
  await tab.page.mouse.up();
109
- };
110
- return {
111
- code,
112
- action,
113
- captureSnapshot: false,
114
- waitForNetwork: true,
115
- };
92
+ });
116
93
  },
117
94
  });
118
95
  export default [
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTool, defineTabTool } from './tool.js';
18
18
  const navigate = defineTool({
19
19
  capability: 'core',
20
20
  schema: {
@@ -26,21 +26,14 @@ const navigate = defineTool({
26
26
  }),
27
27
  type: 'destructive',
28
28
  },
29
- handle: async (context, params) => {
29
+ handle: async (context, params, response) => {
30
30
  const tab = await context.ensureTab();
31
31
  await tab.navigate(params.url);
32
- const code = [
33
- `// Navigate to ${params.url}`,
34
- `await page.goto('${params.url}');`,
35
- ];
36
- return {
37
- code,
38
- captureSnapshot: true,
39
- waitForNetwork: false,
40
- };
32
+ response.setIncludeSnapshot();
33
+ response.addCode(`await page.goto('${params.url}');`);
41
34
  },
42
35
  });
43
- const goBack = defineTool({
36
+ const goBack = defineTabTool({
44
37
  capability: 'core',
45
38
  schema: {
46
39
  name: 'browser_navigate_back',
@@ -49,21 +42,13 @@ const goBack = defineTool({
49
42
  inputSchema: z.object({}),
50
43
  type: 'readOnly',
51
44
  },
52
- handle: async (context) => {
53
- const tab = await context.ensureTab();
45
+ handle: async (tab, params, response) => {
54
46
  await tab.page.goBack();
55
- const code = [
56
- `// Navigate back`,
57
- `await page.goBack();`,
58
- ];
59
- return {
60
- code,
61
- captureSnapshot: true,
62
- waitForNetwork: false,
63
- };
47
+ response.setIncludeSnapshot();
48
+ response.addCode(`await page.goBack();`);
64
49
  },
65
50
  });
66
- const goForward = defineTool({
51
+ const goForward = defineTabTool({
67
52
  capability: 'core',
68
53
  schema: {
69
54
  name: 'browser_navigate_forward',
@@ -72,18 +57,10 @@ const goForward = defineTool({
72
57
  inputSchema: z.object({}),
73
58
  type: 'readOnly',
74
59
  },
75
- handle: async (context) => {
76
- const tab = context.currentTabOrDie();
60
+ handle: async (tab, params, response) => {
77
61
  await tab.page.goForward();
78
- const code = [
79
- `// Navigate forward`,
80
- `await page.goForward();`,
81
- ];
82
- return {
83
- code,
84
- captureSnapshot: true,
85
- waitForNetwork: false,
86
- };
62
+ response.setIncludeSnapshot();
63
+ response.addCode(`await page.goForward();`);
87
64
  },
88
65
  });
89
66
  export default [
@@ -14,8 +14,8 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- const requests = defineTool({
17
+ import { defineTabTool } from './tool.js';
18
+ const requests = defineTabTool({
19
19
  capability: 'core',
20
20
  schema: {
21
21
  name: 'browser_network_requests',
@@ -24,19 +24,9 @@ const requests = defineTool({
24
24
  inputSchema: z.object({}),
25
25
  type: 'readOnly',
26
26
  },
27
- handle: async (context) => {
28
- const requests = context.currentTabOrDie().requests();
29
- const log = [...requests.entries()].map(([request, response]) => renderRequest(request, response)).join('\n');
30
- return {
31
- code: [`// <internal code to list network requests>`],
32
- action: async () => {
33
- return {
34
- content: [{ type: 'text', text: log }]
35
- };
36
- },
37
- captureSnapshot: false,
38
- waitForNetwork: false,
39
- };
27
+ handle: async (tab, params, response) => {
28
+ const requests = tab.requests();
29
+ [...requests.entries()].forEach(([req, res]) => response.addResult(renderRequest(req, res)));
40
30
  },
41
31
  });
42
32
  function renderRequest(request, response) {
package/lib/tools/pdf.js CHANGED
@@ -14,13 +14,12 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
- import { outputFile } from '../config.js';
20
19
  const pdfSchema = z.object({
21
20
  filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
22
21
  });
23
- const pdf = defineTool({
22
+ const pdf = defineTabTool({
24
23
  capability: 'pdf',
25
24
  schema: {
26
25
  name: 'browser_pdf_save',
@@ -29,19 +28,11 @@ const pdf = defineTool({
29
28
  inputSchema: pdfSchema,
30
29
  type: 'readOnly',
31
30
  },
32
- handle: async (context, params) => {
33
- const tab = context.currentTabOrDie();
34
- const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
35
- const code = [
36
- `// Save page as ${fileName}`,
37
- `await page.pdf(${javascript.formatObject({ path: fileName })});`,
38
- ];
39
- return {
40
- code,
41
- action: async () => tab.page.pdf({ path: fileName }).then(() => { }),
42
- captureSnapshot: false,
43
- waitForNetwork: false,
44
- };
31
+ handle: async (tab, params, response) => {
32
+ const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.pdf`);
33
+ response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
34
+ response.addResult(`Saved page as ${fileName}`);
35
+ await tab.page.pdf({ path: fileName });
45
36
  },
46
37
  });
47
38
  export default [
@@ -14,22 +14,27 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
- import { outputFile } from '../config.js';
20
19
  import { generateLocator } from './utils.js';
21
20
  const screenshotSchema = z.object({
22
- raw: z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
21
+ type: z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
23
22
  filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
24
23
  element: 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.'),
25
24
  ref: 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.'),
25
+ fullPage: 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.'),
26
26
  }).refine(data => {
27
27
  return !!data.element === !!data.ref;
28
28
  }, {
29
29
  message: 'Both element and ref must be provided or neither.',
30
30
  path: ['ref', 'element']
31
+ }).refine(data => {
32
+ return !(data.fullPage && (data.element || data.ref));
33
+ }, {
34
+ message: 'fullPage cannot be used with element screenshots.',
35
+ path: ['fullPage']
31
36
  });
32
- const screenshot = defineTool({
37
+ const screenshot = defineTabTool({
33
38
  capability: 'core',
34
39
  schema: {
35
40
  name: 'browser_take_screenshot',
@@ -38,38 +43,35 @@ const screenshot = defineTool({
38
43
  inputSchema: screenshotSchema,
39
44
  type: 'readOnly',
40
45
  },
41
- handle: async (context, params) => {
42
- const tab = context.currentTabOrDie();
43
- const snapshot = tab.snapshotOrDie();
44
- const fileType = params.raw ? 'png' : 'jpeg';
45
- const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
46
- const options = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
46
+ handle: async (tab, params, response) => {
47
+ const fileType = params.type || 'png';
48
+ const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
49
+ const options = {
50
+ type: fileType,
51
+ quality: fileType === 'png' ? undefined : 90,
52
+ scale: 'css',
53
+ path: fileName,
54
+ ...(params.fullPage !== undefined && { fullPage: params.fullPage })
55
+ };
47
56
  const isElementScreenshot = params.element && params.ref;
48
- const code = [
49
- `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
50
- ];
51
- const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null;
57
+ const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
58
+ response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
59
+ // Only get snapshot when element screenshot is needed
60
+ const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
52
61
  if (locator)
53
- code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
62
+ response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
54
63
  else
55
- code.push(`await page.screenshot(${javascript.formatObject(options)});`);
56
- const includeBase64 = context.clientSupportsImages();
57
- const action = async () => {
58
- const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
59
- return {
60
- content: includeBase64 ? [{
61
- type: 'image',
62
- data: screenshot.toString('base64'),
63
- mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg',
64
- }] : []
65
- };
66
- };
67
- return {
68
- code,
69
- action,
70
- captureSnapshot: false,
71
- waitForNetwork: false,
72
- };
64
+ response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
65
+ const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
66
+ response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
67
+ // https://github.com/microsoft/playwright-mcp/issues/817
68
+ // Never return large images to LLM, saving them to the file system is enough.
69
+ if (!params.fullPage) {
70
+ response.addImage({
71
+ contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
72
+ data: buffer
73
+ });
74
+ }
73
75
  }
74
76
  });
75
77
  export default [
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool, defineTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
19
  import { generateLocator } from './utils.js';
20
20
  const snapshot = defineTool({
@@ -26,13 +26,9 @@ const snapshot = defineTool({
26
26
  inputSchema: z.object({}),
27
27
  type: 'readOnly',
28
28
  },
29
- handle: async (context) => {
29
+ handle: async (context, params, response) => {
30
30
  await context.ensureTab();
31
- return {
32
- code: [`// <internal code to capture accessibility snapshot>`],
33
- captureSnapshot: true,
34
- waitForNetwork: false,
35
- };
31
+ response.setIncludeSnapshot();
36
32
  },
37
33
  });
38
34
  export const elementSchema = z.object({
@@ -43,7 +39,7 @@ const clickSchema = elementSchema.extend({
43
39
  doubleClick: z.boolean().optional().describe('Whether to perform a double click instead of a single click'),
44
40
  button: z.enum(['left', 'right', 'middle']).optional().describe('Button to click, defaults to left'),
45
41
  });
46
- const click = defineTool({
42
+ const click = defineTabTool({
47
43
  capability: 'core',
48
44
  schema: {
49
45
  name: 'browser_click',
@@ -52,29 +48,24 @@ const click = defineTool({
52
48
  inputSchema: clickSchema,
53
49
  type: 'destructive',
54
50
  },
55
- handle: async (context, params) => {
56
- const tab = context.currentTabOrDie();
57
- const locator = tab.snapshotOrDie().refLocator(params);
51
+ handle: async (tab, params, response) => {
52
+ response.setIncludeSnapshot();
53
+ const locator = await tab.refLocator(params);
58
54
  const button = params.button;
59
55
  const buttonAttr = button ? `{ button: '${button}' }` : '';
60
- const code = [];
61
- if (params.doubleClick) {
62
- code.push(`// Double click ${params.element}`);
63
- code.push(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
64
- }
65
- else {
66
- code.push(`// Click ${params.element}`);
67
- code.push(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
68
- }
69
- return {
70
- code,
71
- action: () => params.doubleClick ? locator.dblclick({ button }) : locator.click({ button }),
72
- captureSnapshot: true,
73
- waitForNetwork: true,
74
- };
56
+ if (params.doubleClick)
57
+ response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
58
+ else
59
+ response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
60
+ await tab.waitForCompletion(async () => {
61
+ if (params.doubleClick)
62
+ await locator.dblclick({ button });
63
+ else
64
+ await locator.click({ button });
65
+ });
75
66
  },
76
67
  });
77
- const drag = defineTool({
68
+ const drag = defineTabTool({
78
69
  capability: 'core',
79
70
  schema: {
80
71
  name: 'browser_drag',
@@ -88,23 +79,19 @@ const drag = defineTool({
88
79
  }),
89
80
  type: 'destructive',
90
81
  },
91
- handle: async (context, params) => {
92
- const snapshot = context.currentTabOrDie().snapshotOrDie();
93
- const startLocator = snapshot.refLocator({ ref: params.startRef, element: params.startElement });
94
- const endLocator = snapshot.refLocator({ ref: params.endRef, element: params.endElement });
95
- const code = [
96
- `// Drag ${params.startElement} to ${params.endElement}`,
97
- `await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`
98
- ];
99
- return {
100
- code,
101
- action: () => startLocator.dragTo(endLocator),
102
- captureSnapshot: true,
103
- waitForNetwork: true,
104
- };
82
+ handle: async (tab, params, response) => {
83
+ response.setIncludeSnapshot();
84
+ const [startLocator, endLocator] = await tab.refLocators([
85
+ { ref: params.startRef, element: params.startElement },
86
+ { ref: params.endRef, element: params.endElement },
87
+ ]);
88
+ await tab.waitForCompletion(async () => {
89
+ await startLocator.dragTo(endLocator);
90
+ });
91
+ response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
105
92
  },
106
93
  });
107
- const hover = defineTool({
94
+ const hover = defineTabTool({
108
95
  capability: 'core',
109
96
  schema: {
110
97
  name: 'browser_hover',
@@ -113,25 +100,19 @@ const hover = defineTool({
113
100
  inputSchema: elementSchema,
114
101
  type: 'readOnly',
115
102
  },
116
- handle: async (context, params) => {
117
- const snapshot = context.currentTabOrDie().snapshotOrDie();
118
- const locator = snapshot.refLocator(params);
119
- const code = [
120
- `// Hover over ${params.element}`,
121
- `await page.${await generateLocator(locator)}.hover();`
122
- ];
123
- return {
124
- code,
125
- action: () => locator.hover(),
126
- captureSnapshot: true,
127
- waitForNetwork: true,
128
- };
103
+ handle: async (tab, params, response) => {
104
+ response.setIncludeSnapshot();
105
+ const locator = await tab.refLocator(params);
106
+ response.addCode(`await page.${await generateLocator(locator)}.hover();`);
107
+ await tab.waitForCompletion(async () => {
108
+ await locator.hover();
109
+ });
129
110
  },
130
111
  });
131
112
  const selectOptionSchema = elementSchema.extend({
132
113
  values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
133
114
  });
134
- const selectOption = defineTool({
115
+ const selectOption = defineTabTool({
135
116
  capability: 'core',
136
117
  schema: {
137
118
  name: 'browser_select_option',
@@ -140,19 +121,13 @@ const selectOption = defineTool({
140
121
  inputSchema: selectOptionSchema,
141
122
  type: 'destructive',
142
123
  },
143
- handle: async (context, params) => {
144
- const snapshot = context.currentTabOrDie().snapshotOrDie();
145
- const locator = snapshot.refLocator(params);
146
- const code = [
147
- `// Select options [${params.values.join(', ')}] in ${params.element}`,
148
- `await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`
149
- ];
150
- return {
151
- code,
152
- action: () => locator.selectOption(params.values).then(() => { }),
153
- captureSnapshot: true,
154
- waitForNetwork: true,
155
- };
124
+ handle: async (tab, params, response) => {
125
+ response.setIncludeSnapshot();
126
+ const locator = await tab.refLocator(params);
127
+ response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
128
+ await tab.waitForCompletion(async () => {
129
+ await locator.selectOption(params.values);
130
+ });
156
131
  },
157
132
  });
158
133
  export default [
package/lib/tools/tabs.js CHANGED
@@ -24,19 +24,9 @@ const listTabs = defineTool({
24
24
  inputSchema: z.object({}),
25
25
  type: 'readOnly',
26
26
  },
27
- handle: async (context) => {
27
+ handle: async (context, params, response) => {
28
28
  await context.ensureTab();
29
- return {
30
- code: [`// <internal code to list tabs>`],
31
- captureSnapshot: false,
32
- waitForNetwork: false,
33
- resultOverride: {
34
- content: [{
35
- type: 'text',
36
- text: await context.listTabsMarkdown(),
37
- }],
38
- },
39
- };
29
+ response.setIncludeTabs();
40
30
  },
41
31
  });
42
32
  const selectTab = defineTool({
@@ -50,16 +40,9 @@ const selectTab = defineTool({
50
40
  }),
51
41
  type: 'readOnly',
52
42
  },
53
- handle: async (context, params) => {
43
+ handle: async (context, params, response) => {
54
44
  await context.selectTab(params.index);
55
- const code = [
56
- `// <internal code to select tab ${params.index}>`,
57
- ];
58
- return {
59
- code,
60
- captureSnapshot: true,
61
- waitForNetwork: false
62
- };
45
+ response.setIncludeSnapshot();
63
46
  },
64
47
  });
65
48
  const newTab = defineTool({
@@ -73,18 +56,11 @@ const newTab = defineTool({
73
56
  }),
74
57
  type: 'readOnly',
75
58
  },
76
- handle: async (context, params) => {
77
- await context.newTab();
59
+ handle: async (context, params, response) => {
60
+ const tab = await context.newTab();
78
61
  if (params.url)
79
- await context.currentTabOrDie().navigate(params.url);
80
- const code = [
81
- `// <internal code to open a new tab>`,
82
- ];
83
- return {
84
- code,
85
- captureSnapshot: true,
86
- waitForNetwork: false
87
- };
62
+ await tab.navigate(params.url);
63
+ response.setIncludeSnapshot();
88
64
  },
89
65
  });
90
66
  const closeTab = defineTool({
@@ -98,16 +74,9 @@ const closeTab = defineTool({
98
74
  }),
99
75
  type: 'destructive',
100
76
  },
101
- handle: async (context, params) => {
77
+ handle: async (context, params, response) => {
102
78
  await context.closeTab(params.index);
103
- const code = [
104
- `// <internal code to close tab ${params.index}>`,
105
- ];
106
- return {
107
- code,
108
- captureSnapshot: true,
109
- waitForNetwork: false
110
- };
79
+ response.setIncludeSnapshot();
111
80
  },
112
81
  });
113
82
  export default [
package/lib/tools/tool.js CHANGED
@@ -16,3 +16,18 @@
16
16
  export function defineTool(tool) {
17
17
  return tool;
18
18
  }
19
+ export function defineTabTool(tool) {
20
+ return {
21
+ ...tool,
22
+ handle: async (context, params, response) => {
23
+ const tab = context.currentTabOrDie();
24
+ const modalStates = tab.modalStates().map(state => state.type);
25
+ if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
26
+ response.addError(`Error: The tool "${tool.schema.name}" can only be used when there is related modal state present.\n` + tab.modalStatesMarkdown().join('\n'));
27
+ else if (!tool.clearsModalState && modalStates.length)
28
+ response.addError(`Error: Tool "${tool.schema.name}" does not handle the modal state.\n` + tab.modalStatesMarkdown().join('\n'));
29
+ else
30
+ return tool.handle(tab, params, response);
31
+ },
32
+ };
33
+ }