@playwright/mcp 0.0.31 → 0.0.32

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 (49) hide show
  1. package/README.md +25 -4
  2. package/config.d.ts +5 -0
  3. package/index.d.ts +1 -6
  4. package/lib/browserServerBackend.js +54 -0
  5. package/lib/config.js +2 -1
  6. package/lib/context.js +48 -171
  7. package/lib/extension/cdpRelay.js +370 -0
  8. package/lib/extension/main.js +33 -0
  9. package/lib/httpServer.js +20 -182
  10. package/lib/index.js +3 -2
  11. package/lib/loop/loop.js +69 -0
  12. package/lib/loop/loopClaude.js +152 -0
  13. package/lib/loop/loopOpenAI.js +141 -0
  14. package/lib/loop/main.js +60 -0
  15. package/lib/loopTools/context.js +66 -0
  16. package/lib/loopTools/main.js +49 -0
  17. package/lib/loopTools/perform.js +32 -0
  18. package/lib/loopTools/snapshot.js +29 -0
  19. package/lib/loopTools/tool.js +18 -0
  20. package/lib/mcp/inProcessTransport.js +72 -0
  21. package/lib/mcp/server.js +88 -0
  22. package/lib/{transport.js → mcp/transport.js} +30 -42
  23. package/lib/package.js +3 -3
  24. package/lib/program.js +38 -9
  25. package/lib/response.js +98 -0
  26. package/lib/sessionLog.js +70 -0
  27. package/lib/tab.js +133 -22
  28. package/lib/tools/common.js +11 -23
  29. package/lib/tools/console.js +4 -15
  30. package/lib/tools/dialogs.js +12 -17
  31. package/lib/tools/evaluate.js +12 -21
  32. package/lib/tools/files.js +10 -16
  33. package/lib/tools/install.js +3 -7
  34. package/lib/tools/keyboard.js +30 -42
  35. package/lib/tools/mouse.js +27 -50
  36. package/lib/tools/navigate.js +15 -35
  37. package/lib/tools/network.js +5 -15
  38. package/lib/tools/pdf.js +8 -15
  39. package/lib/tools/screenshot.js +29 -30
  40. package/lib/tools/snapshot.js +45 -65
  41. package/lib/tools/tabs.js +10 -41
  42. package/lib/tools/tool.js +14 -0
  43. package/lib/tools/utils.js +2 -2
  44. package/lib/tools/wait.js +3 -6
  45. package/lib/tools.js +3 -0
  46. package/package.json +9 -3
  47. package/lib/connection.js +0 -81
  48. package/lib/pageSnapshot.js +0 -43
  49. 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,15 @@ 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(`// Navigate to ${params.url}`);
34
+ response.addCode(`await page.goto('${params.url}');`);
41
35
  },
42
36
  });
43
- const goBack = defineTool({
37
+ const goBack = defineTabTool({
44
38
  capability: 'core',
45
39
  schema: {
46
40
  name: 'browser_navigate_back',
@@ -49,21 +43,14 @@ const goBack = defineTool({
49
43
  inputSchema: z.object({}),
50
44
  type: 'readOnly',
51
45
  },
52
- handle: async (context) => {
53
- const tab = await context.ensureTab();
46
+ handle: async (tab, params, response) => {
54
47
  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
- };
48
+ response.setIncludeSnapshot();
49
+ response.addCode(`// Navigate back`);
50
+ response.addCode(`await page.goBack();`);
64
51
  },
65
52
  });
66
- const goForward = defineTool({
53
+ const goForward = defineTabTool({
67
54
  capability: 'core',
68
55
  schema: {
69
56
  name: 'browser_navigate_forward',
@@ -72,18 +59,11 @@ const goForward = defineTool({
72
59
  inputSchema: z.object({}),
73
60
  type: 'readOnly',
74
61
  },
75
- handle: async (context) => {
76
- const tab = context.currentTabOrDie();
62
+ handle: async (tab, params, response) => {
77
63
  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
- };
64
+ response.setIncludeSnapshot();
65
+ response.addCode(`// Navigate forward`);
66
+ response.addCode(`await page.goForward();`);
87
67
  },
88
68
  });
89
69
  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,13 @@
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
19
  import { outputFile } from '../config.js';
20
20
  const pdfSchema = z.object({
21
21
  filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
22
22
  });
23
- const pdf = defineTool({
23
+ const pdf = defineTabTool({
24
24
  capability: 'pdf',
25
25
  schema: {
26
26
  name: 'browser_pdf_save',
@@ -29,19 +29,12 @@ const pdf = defineTool({
29
29
  inputSchema: pdfSchema,
30
30
  type: 'readOnly',
31
31
  },
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
- };
32
+ handle: async (tab, params, response) => {
33
+ const fileName = await outputFile(tab.context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
34
+ response.addCode(`// Save page as ${fileName}`);
35
+ response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
36
+ response.addResult(`Saved page as ${fileName}`);
37
+ await tab.page.pdf({ path: fileName });
45
38
  },
46
39
  });
47
40
  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 } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
19
  import { outputFile } from '../config.js';
20
20
  import { generateLocator } from './utils.js';
@@ -23,13 +23,19 @@ const screenshotSchema = z.object({
23
23
  filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
24
24
  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
25
  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.'),
26
+ 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
27
  }).refine(data => {
27
28
  return !!data.element === !!data.ref;
28
29
  }, {
29
30
  message: 'Both element and ref must be provided or neither.',
30
31
  path: ['ref', 'element']
32
+ }).refine(data => {
33
+ return !(data.fullPage && (data.element || data.ref));
34
+ }, {
35
+ message: 'fullPage cannot be used with element screenshots.',
36
+ path: ['fullPage']
31
37
  });
32
- const screenshot = defineTool({
38
+ const screenshot = defineTabTool({
33
39
  capability: 'core',
34
40
  schema: {
35
41
  name: 'browser_take_screenshot',
@@ -38,38 +44,31 @@ const screenshot = defineTool({
38
44
  inputSchema: screenshotSchema,
39
45
  type: 'readOnly',
40
46
  },
41
- handle: async (context, params) => {
42
- const tab = context.currentTabOrDie();
43
- const snapshot = tab.snapshotOrDie();
47
+ handle: async (tab, params, response) => {
44
48
  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 };
49
+ const fileName = await outputFile(tab.context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
50
+ const options = {
51
+ type: fileType,
52
+ quality: fileType === 'png' ? undefined : 50,
53
+ scale: 'css',
54
+ path: fileName,
55
+ ...(params.fullPage !== undefined && { fullPage: params.fullPage })
56
+ };
47
57
  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;
58
+ const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
59
+ response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
60
+ // Only get snapshot when element screenshot is needed
61
+ const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
52
62
  if (locator)
53
- code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
63
+ response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
54
64
  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
- };
65
+ response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
66
+ const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
67
+ response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
68
+ response.addImage({
69
+ contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
70
+ data: buffer
71
+ });
73
72
  }
74
73
  });
75
74
  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,28 @@ 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
56
  if (params.doubleClick) {
62
- code.push(`// Double click ${params.element}`);
63
- code.push(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
57
+ response.addCode(`// Double click ${params.element}`);
58
+ response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
64
59
  }
65
60
  else {
66
- code.push(`// Click ${params.element}`);
67
- code.push(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
61
+ response.addCode(`// Click ${params.element}`);
62
+ response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
68
63
  }
69
- return {
70
- code,
71
- action: () => params.doubleClick ? locator.dblclick({ button }) : locator.click({ button }),
72
- captureSnapshot: true,
73
- waitForNetwork: true,
74
- };
64
+ await tab.waitForCompletion(async () => {
65
+ if (params.doubleClick)
66
+ await locator.dblclick({ button });
67
+ else
68
+ await locator.click({ button });
69
+ });
75
70
  },
76
71
  });
77
- const drag = defineTool({
72
+ const drag = defineTabTool({
78
73
  capability: 'core',
79
74
  schema: {
80
75
  name: 'browser_drag',
@@ -88,23 +83,19 @@ const drag = defineTool({
88
83
  }),
89
84
  type: 'destructive',
90
85
  },
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
- };
86
+ handle: async (tab, params, response) => {
87
+ response.setIncludeSnapshot();
88
+ const [startLocator, endLocator] = await tab.refLocators([
89
+ { ref: params.startRef, element: params.startElement },
90
+ { ref: params.endRef, element: params.endElement },
91
+ ]);
92
+ await tab.waitForCompletion(async () => {
93
+ await startLocator.dragTo(endLocator);
94
+ });
95
+ response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
105
96
  },
106
97
  });
107
- const hover = defineTool({
98
+ const hover = defineTabTool({
108
99
  capability: 'core',
109
100
  schema: {
110
101
  name: 'browser_hover',
@@ -113,25 +104,19 @@ const hover = defineTool({
113
104
  inputSchema: elementSchema,
114
105
  type: 'readOnly',
115
106
  },
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
- };
107
+ handle: async (tab, params, response) => {
108
+ response.setIncludeSnapshot();
109
+ const locator = await tab.refLocator(params);
110
+ response.addCode(`await page.${await generateLocator(locator)}.hover();`);
111
+ await tab.waitForCompletion(async () => {
112
+ await locator.hover();
113
+ });
129
114
  },
130
115
  });
131
116
  const selectOptionSchema = elementSchema.extend({
132
117
  values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
133
118
  });
134
- const selectOption = defineTool({
119
+ const selectOption = defineTabTool({
135
120
  capability: 'core',
136
121
  schema: {
137
122
  name: 'browser_select_option',
@@ -140,19 +125,14 @@ const selectOption = defineTool({
140
125
  inputSchema: selectOptionSchema,
141
126
  type: 'destructive',
142
127
  },
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
- };
128
+ handle: async (tab, params, response) => {
129
+ response.setIncludeSnapshot();
130
+ const locator = await tab.refLocator(params);
131
+ response.addCode(`// Select options [${params.values.join(', ')}] in ${params.element}`);
132
+ response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
133
+ await tab.waitForCompletion(async () => {
134
+ await locator.selectOption(params.values);
135
+ });
156
136
  },
157
137
  });
158
138
  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,17 @@
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
+ throw new Error(`The tool "${tool.schema.name}" can only be used when there is related modal state present.\n` + tab.modalStatesMarkdown().join('\n'));
27
+ if (!tool.clearsModalState && modalStates.length)
28
+ throw new Error(`Tool "${tool.schema.name}" does not handle the modal state.\n` + tab.modalStatesMarkdown().join('\n'));
29
+ return tool.handle(tab, params, response);
30
+ },
31
+ };
32
+ }
@@ -15,7 +15,7 @@
15
15
  */
16
16
  // @ts-ignore
17
17
  import { asLocator } from 'playwright-core/lib/utils';
18
- export async function waitForCompletion(context, tab, callback) {
18
+ export async function waitForCompletion(tab, callback) {
19
19
  const requests = new Set();
20
20
  let frameNavigated = false;
21
21
  let waitCallback = () => { };
@@ -53,7 +53,7 @@ export async function waitForCompletion(context, tab, callback) {
53
53
  if (!requests.size && !frameNavigated)
54
54
  waitCallback();
55
55
  await waitBarrier;
56
- await context.waitForTimeout(1000);
56
+ await tab.waitForTimeout(1000);
57
57
  return result;
58
58
  }
59
59
  finally {