Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(mdb): use intent sniffing instead of explicit logging (#37674)
Pavel Feldman6714207c936a
diff --git a/examples/todomvc/.claude/agents/playwright-test-generator.md b/examples/todomvc/.claude/agents/playwright-test-generator.mdindex ade2f9c0a3..0296e3e5e8 100644--- a/examples/todomvc/.claude/agents/playwright-test-generator.md+++ b/examples/todomvc/.claude/agents/playwright-test-generator.md@@ -1,7 +1,7 @@---name: playwright-test-generatordescription: Use this agent when you need to create automated browser tests using Playwright. Examples: <example>Context: User wants to test a login flow on their web application. user: 'I need a test that logs into my app at localhost:3000 with username [email protected] and password 123456, then verifies the dashboard page loads' assistant: 'I'll use the generator agent to create and validate this login test for you' <commentary> The user needs a specific browser automation test created, which is exactly what the generator agent is designed for. </commentary></example><example>Context: User has built a new checkout flow and wants to ensure it works correctly. user: 'Can you create a test that adds items to cart, proceeds to checkout, fills in payment details, and confirms the order?' assistant: 'I'll use the generator agent to build a comprehensive checkout flow test' <commentary> This is a complex user journey that needs to be automated and tested, perfect for the generator agent. </commentary></example>-tools: Glob, Grep, Read, mcp__playwright-test__browser_click, mcp__playwright-test__browser_drag, mcp__playwright-test__browser_evaluate, mcp__playwright-test__browser_file_upload, mcp__playwright-test__browser_handle_dialog, mcp__playwright-test__browser_hover, mcp__playwright-test__browser_navigate, mcp__playwright-test__browser_press_key, mcp__playwright-test__browser_select_option, mcp__playwright-test__browser_snapshot, mcp__playwright-test__browser_type, mcp__playwright-test__browser_verify_element_visible, mcp__playwright-test__browser_verify_list_visible, mcp__playwright-test__browser_verify_text_visible, mcp__playwright-test__browser_verify_value, mcp__playwright-test__browser_wait_for, mcp__playwright-test__generator_log_step, mcp__playwright-test__generator_read_log, mcp__playwright-test__generator_setup_page, mcp__playwright-test__generator_write_test+tools: Glob, Grep, Read, mcp__playwright-test__browser_click, mcp__playwright-test__browser_drag, mcp__playwright-test__browser_evaluate, mcp__playwright-test__browser_file_upload, mcp__playwright-test__browser_handle_dialog, mcp__playwright-test__browser_hover, mcp__playwright-test__browser_navigate, mcp__playwright-test__browser_press_key, mcp__playwright-test__browser_select_option, mcp__playwright-test__browser_snapshot, mcp__playwright-test__browser_type, mcp__playwright-test__browser_verify_element_visible, mcp__playwright-test__browser_verify_list_visible, mcp__playwright-test__browser_verify_text_visible, mcp__playwright-test__browser_verify_value, mcp__playwright-test__browser_wait_for, mcp__playwright-test__generator_read_log, mcp__playwright-test__generator_setup_page, mcp__playwright-test__generator_write_testmodel: sonnetcolor: blue---@@ -15,14 +15,15 @@ application behavior.- Run the `generator_setup_page` tool to set up page for the scenario- For each step and verification in the scenario, do the following:- Use Playwright tool to manually execute it in real-time.- - Immediately after each tool execution, log the step via running `generator_log_step` tool.+ - Use the step description as the intent for each Playwright tool call.- Retrieve generator log via `generator_read_log`- Immediately after reading the test log, invoke `generator_write_test` with the generated source code- File should contain single test- File name must be fs-friendly scenario name- Test must be placed in a describe matching the top-level test plan item- Test title must match the scenario name- - Includes a comment with the step text before each step execution+ - Includes a comment with the step text before each step execution. Do not duplicate comments if step requires+ multiple actions.<example-generation>For following plan:diff --git "a/examples/todomvc/.github/chatmodes/\360\237\216\255 generator.chatmode.md" "b/examples/todomvc/.github/chatmodes/\360\237\216\255 generator.chatmode.md"index 7a594a877a..88947303d7 100644--- "a/examples/todomvc/.github/chatmodes/\360\237\216\255 generator.chatmode.md"+++ "b/examples/todomvc/.github/chatmodes/\360\237\216\255 generator.chatmode.md"@@ -1,6 +1,6 @@---description: Use this agent when you need to create automated browser tests using Playwright.-tools: ['search/fileSearch', 'search/textSearch', 'search/listDirectory', 'search/readFile', 'playwright-test/browser_click', 'playwright-test/browser_drag', 'playwright-test/browser_evaluate', 'playwright-test/browser_file_upload', 'playwright-test/browser_handle_dialog', 'playwright-test/browser_hover', 'playwright-test/browser_navigate', 'playwright-test/browser_press_key', 'playwright-test/browser_select_option', 'playwright-test/browser_snapshot', 'playwright-test/browser_type', 'playwright-test/browser_verify_element_visible', 'playwright-test/browser_verify_list_visible', 'playwright-test/browser_verify_text_visible', 'playwright-test/browser_verify_value', 'playwright-test/browser_wait_for', 'playwright-test/generator_log_step', 'playwright-test/generator_read_log', 'playwright-test/generator_setup_page', 'playwright-test/generator_write_test']+tools: ['search/fileSearch', 'search/textSearch', 'search/listDirectory', 'search/readFile', 'playwright-test/browser_click', 'playwright-test/browser_drag', 'playwright-test/browser_evaluate', 'playwright-test/browser_file_upload', 'playwright-test/browser_handle_dialog', 'playwright-test/browser_hover', 'playwright-test/browser_navigate', 'playwright-test/browser_press_key', 'playwright-test/browser_select_option', 'playwright-test/browser_snapshot', 'playwright-test/browser_type', 'playwright-test/browser_verify_element_visible', 'playwright-test/browser_verify_list_visible', 'playwright-test/browser_verify_text_visible', 'playwright-test/browser_verify_value', 'playwright-test/browser_wait_for', 'playwright-test/generator_read_log', 'playwright-test/generator_setup_page', 'playwright-test/generator_write_test']---You are a Playwright Test Generator, an expert in browser automation and end-to-end testing.@@ -12,14 +12,15 @@ application behavior.- Run the `generator_setup_page` tool to set up page for the scenario- For each step and verification in the scenario, do the following:- Use Playwright tool to manually execute it in real-time.- - Immediately after each tool execution, log the step via running `generator_log_step` tool.+ - Use the step description as the intent for each Playwright tool call.- Retrieve generator log via `generator_read_log`- Immediately after reading the test log, invoke `generator_write_test` with the generated source code- File should contain single test- File name must be fs-friendly scenario name- Test must be placed in a describe matching the top-level test plan item- Test title must match the scenario name- - Includes a comment with the step text before each step execution+ - Includes a comment with the step text before each step execution. Do not duplicate comments if step requires+ multiple actions.<example-generation>For following plan:diff --git a/packages/playwright/src/agents/generator.md b/packages/playwright/src/agents/generator.mdindex 2ce327d15f..dabbb46f14 100644--- a/packages/playwright/src/agents/generator.md+++ b/packages/playwright/src/agents/generator.md@@ -23,7 +23,6 @@ tools:- playwright-test/browser_verify_text_visible- playwright-test/browser_verify_value- playwright-test/browser_wait_for- - playwright-test/generator_log_step- playwright-test/generator_read_log- playwright-test/generator_setup_page- playwright-test/generator_write_test@@ -38,14 +37,15 @@ application behavior.- Run the `generator_setup_page` tool to set up page for the scenario- For each step and verification in the scenario, do the following:- Use Playwright tool to manually execute it in real-time.- - Immediately after each tool execution, log the step via running `generator_log_step` tool.+ - Use the step description as the intent for each Playwright tool call.- Retrieve generator log via `generator_read_log`- Immediately after reading the test log, invoke `generator_write_test` with the generated source code- File should contain single test- File name must be fs-friendly scenario name- Test must be placed in a describe matching the top-level test plan item- Test title must match the scenario name- - Includes a comment with the step text before each step execution+ - Includes a comment with the step text before each step execution. Do not duplicate comments if step requires+ multiple actions.<example-generation>For following plan:diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.tsindex 3308dd827d..bdf7d2a882 100644--- a/packages/playwright/src/mcp/browser/response.ts+++ b/packages/playwright/src/mcp/browser/response.ts@@ -18,7 +18,7 @@ import { debug } from 'playwright-core/lib/utilsBundle';import { renderModalStates } from './tab';import type { Tab, TabSnapshot } from './tab';-import type { ImageContent, TextContent } from '@modelcontextprotocol/sdk/types.js';+import type { CallToolResult, ImageContent, TextContent } from '@modelcontextprotocol/sdk/types.js';import type { Context } from './context';export const requestDebug = debug('pw:mcp:request');@@ -225,3 +225,50 @@ function trim(text: string, maxLength: number) {return text;return text.slice(0, maxLength) + '...';}++function parseSections(text: string): Map<string, string> {+ const sections = new Map<string, string>();+ const sectionHeaders = text.split(/^### /m).slice(1); // Remove empty first element++ for (const section of sectionHeaders) {+ const firstNewlineIndex = section.indexOf('\n');+ if (firstNewlineIndex === -1)+ continue;++ const sectionName = section.substring(0, firstNewlineIndex);+ const sectionContent = section.substring(firstNewlineIndex + 1).trim();+ sections.set(sectionName, sectionContent);+ }++ return sections;+}++export function parseResponse(response: CallToolResult) {+ if (response.content?.[0].type !== 'text')+ return undefined;+ const text = response.content[0].text;++ const sections = parseSections(text);+ const result = sections.get('Result');+ const code = sections.get('Ran Playwright code');+ const tabs = sections.get('Open tabs');+ const pageState = sections.get('Page state');+ const consoleMessages = sections.get('New console messages');+ const modalState = sections.get('Modal state');+ const downloads = sections.get('Downloads');+ const codeNoFrame = code?.replace(/^```js\n/, '').replace(/\n```$/, '');+ const isError = response.isError;+ const attachments = response.content.slice(1);++ return {+ result,+ code: codeNoFrame,+ tabs,+ pageState,+ consoleMessages,+ modalState,+ downloads,+ isError,+ attachments,+ };+}diff --git a/packages/playwright/src/mcp/browser/tools/common.ts b/packages/playwright/src/mcp/browser/tools/common.tsindex 62f175ecd7..d5d620d83a 100644--- a/packages/playwright/src/mcp/browser/tools/common.ts+++ b/packages/playwright/src/mcp/browser/tools/common.ts@@ -25,7 +25,7 @@ const close = defineTool({title: 'Close browser',description: 'Close the page',inputSchema: z.object({}),- type: 'readOnly',+ type: 'action',},handle: async (context, params, response) => {@@ -45,7 +45,7 @@ const resize = defineTabTool({width: z.number().describe('Width of the browser window'),height: z.number().describe('Height of the browser window'),}),- type: 'readOnly',+ type: 'action',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/dialogs.ts b/packages/playwright/src/mcp/browser/tools/dialogs.tsindex d606433f1a..dfc15bfd2f 100644--- a/packages/playwright/src/mcp/browser/tools/dialogs.ts+++ b/packages/playwright/src/mcp/browser/tools/dialogs.ts@@ -28,7 +28,7 @@ export const handleDialog = defineTabTool({accept: z.boolean().describe('Whether to accept the dialog.'),promptText: z.string().optional().describe('The text of the prompt in case of a prompt dialog.'),}),- type: 'destructive',+ type: 'action',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/evaluate.ts b/packages/playwright/src/mcp/browser/tools/evaluate.tsindex b7c7645c77..f952230ecd 100644--- a/packages/playwright/src/mcp/browser/tools/evaluate.ts+++ b/packages/playwright/src/mcp/browser/tools/evaluate.ts@@ -34,7 +34,7 @@ const evaluate = defineTabTool({title: 'Evaluate JavaScript',description: 'Evaluate JavaScript expression on page or element',inputSchema: evaluateSchema,- type: 'destructive',+ type: 'action',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/files.ts b/packages/playwright/src/mcp/browser/tools/files.tsindex fcd77ec4c4..b540bf05e7 100644--- a/packages/playwright/src/mcp/browser/tools/files.ts+++ b/packages/playwright/src/mcp/browser/tools/files.ts@@ -27,7 +27,7 @@ export const uploadFile = defineTabTool({inputSchema: z.object({paths: z.array(z.string()).optional().describe('The absolute paths to the files to upload. Can be single file or multiple files. If omitted, file chooser is cancelled.'),}),- type: 'destructive',+ type: 'action',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/form.ts b/packages/playwright/src/mcp/browser/tools/form.tsindex 4f7beadced..b2d957e99f 100644--- a/packages/playwright/src/mcp/browser/tools/form.ts+++ b/packages/playwright/src/mcp/browser/tools/form.ts@@ -34,7 +34,7 @@ const fillForm = defineTabTool({value: z.string().describe('Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.'),})).describe('Fields to fill in'),}),- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/install.ts b/packages/playwright/src/mcp/browser/tools/install.tsindex 54d67a7d06..d7081dcc44 100644--- a/packages/playwright/src/mcp/browser/tools/install.ts+++ b/packages/playwright/src/mcp/browser/tools/install.ts@@ -27,7 +27,7 @@ const install = defineTool({title: 'Install the browser specified in the config',description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',inputSchema: z.object({}),- type: 'destructive',+ type: 'action',},handle: async (context, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/keyboard.ts b/packages/playwright/src/mcp/browser/tools/keyboard.tsindex 8b08a1ec68..898e18c467 100644--- a/packages/playwright/src/mcp/browser/tools/keyboard.ts+++ b/packages/playwright/src/mcp/browser/tools/keyboard.ts@@ -29,7 +29,7 @@ const pressKey = defineTabTool({inputSchema: z.object({key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),}),- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {@@ -56,7 +56,7 @@ const type = defineTabTool({title: 'Type text',description: 'Type text into editable element',inputSchema: typeSchema,- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/mouse.ts b/packages/playwright/src/mcp/browser/tools/mouse.tsindex 0e0fc88e1a..a525202ccf 100644--- a/packages/playwright/src/mcp/browser/tools/mouse.ts+++ b/packages/playwright/src/mcp/browser/tools/mouse.ts@@ -31,7 +31,7 @@ const mouseMove = defineTabTool({x: z.number().describe('X coordinate'),y: z.number().describe('Y coordinate'),}),- type: 'readOnly',+ type: 'input',},handle: async (tab, params, response) => {@@ -54,7 +54,7 @@ const mouseClick = defineTabTool({x: z.number().describe('X coordinate'),y: z.number().describe('Y coordinate'),}),- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {@@ -85,7 +85,7 @@ const mouseDrag = defineTabTool({endX: z.number().describe('End X coordinate'),endY: z.number().describe('End Y coordinate'),}),- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/navigate.ts b/packages/playwright/src/mcp/browser/tools/navigate.tsindex 9998756e57..e2b507a6d3 100644--- a/packages/playwright/src/mcp/browser/tools/navigate.ts+++ b/packages/playwright/src/mcp/browser/tools/navigate.ts@@ -27,7 +27,7 @@ const navigate = defineTool({inputSchema: z.object({url: z.string().describe('The URL to navigate to'),}),- type: 'destructive',+ type: 'action',},handle: async (context, params, response) => {@@ -46,7 +46,7 @@ const goBack = defineTabTool({title: 'Go back',description: 'Go back to the previous page',inputSchema: z.object({}),- type: 'readOnly',+ type: 'action',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/snapshot.ts b/packages/playwright/src/mcp/browser/tools/snapshot.tsindex bea116ee8f..5900ada32b 100644--- a/packages/playwright/src/mcp/browser/tools/snapshot.ts+++ b/packages/playwright/src/mcp/browser/tools/snapshot.ts@@ -53,7 +53,7 @@ const click = defineTabTool({title: 'Click',description: 'Perform click on a web page',inputSchema: clickSchema,- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {@@ -93,7 +93,7 @@ const drag = defineTabTool({endElement: z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),endRef: z.string().describe('Exact target element reference from the page snapshot'),}),- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {@@ -119,7 +119,7 @@ const hover = defineTabTool({title: 'Hover mouse',description: 'Hover over element on page',inputSchema: elementSchema,- type: 'readOnly',+ type: 'input',},handle: async (tab, params, response) => {@@ -145,7 +145,7 @@ const selectOption = defineTabTool({title: 'Select option',description: 'Select an option in a dropdown',inputSchema: selectOptionSchema,- type: 'destructive',+ type: 'input',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/tabs.ts b/packages/playwright/src/mcp/browser/tools/tabs.tsindex 77ad91ff90..768db50e50 100644--- a/packages/playwright/src/mcp/browser/tools/tabs.ts+++ b/packages/playwright/src/mcp/browser/tools/tabs.ts@@ -28,7 +28,7 @@ const browserTabs = defineTool({action: z.enum(['list', 'new', 'close', 'select']).describe('Operation to perform'),index: z.number().optional().describe('Tab index, used for close/select. If omitted for close, current tab is closed.'),}),- type: 'destructive',+ type: 'action',},handle: async (context, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/verify.ts b/packages/playwright/src/mcp/browser/tools/verify.tsindex 2d3d324ca6..fa4d0ba0da 100644--- a/packages/playwright/src/mcp/browser/tools/verify.ts+++ b/packages/playwright/src/mcp/browser/tools/verify.ts@@ -29,7 +29,7 @@ const verifyElement = defineTabTool({role: z.string().describe('ROLE of the element. Can be found in the snapshot like this: \`- {ROLE} "Accessible Name":\`'),accessibleName: z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: \`- role "{ACCESSIBLE_NAME}"\`'),}),- type: 'readOnly',+ type: 'assertion',},handle: async (tab, params, response) => {@@ -53,7 +53,7 @@ const verifyText = defineTabTool({inputSchema: z.object({text: z.string().describe('TEXT to verify. Can be found in the snapshot like this: \`- role "Accessible Name": {TEXT}\` or like this: \`- text: {TEXT}\`'),}),- type: 'readOnly',+ type: 'assertion',},handle: async (tab, params, response) => {@@ -79,7 +79,7 @@ const verifyList = defineTabTool({ref: z.string().describe('Exact target element reference that points to the list'),items: z.array(z.string()).describe('Items to verify'),}),- type: 'readOnly',+ type: 'assertion',},handle: async (tab, params, response) => {@@ -114,7 +114,7 @@ const verifyValue = defineTabTool({ref: z.string().describe('Exact target element reference that points to the element'),value: z.string().describe('Value to verify. For checkbox, use "true" or "false".'),}),- type: 'readOnly',+ type: 'assertion',},handle: async (tab, params, response) => {diff --git a/packages/playwright/src/mcp/browser/tools/wait.ts b/packages/playwright/src/mcp/browser/tools/wait.tsindex a0f5382b6a..da51f81b21 100644--- a/packages/playwright/src/mcp/browser/tools/wait.ts+++ b/packages/playwright/src/mcp/browser/tools/wait.ts@@ -29,7 +29,7 @@ const wait = defineTool({text: z.string().optional().describe('The text to wait for'),textGone: z.string().optional().describe('The text to wait for to disappear'),}),- type: 'readOnly',+ type: 'assertion',},handle: async (context, params, response) => {diff --git a/packages/playwright/src/mcp/sdk/mdb.ts b/packages/playwright/src/mcp/sdk/mdb.tsindex af4969ec3d..88aa423584 100644--- a/packages/playwright/src/mcp/sdk/mdb.ts+++ b/packages/playwright/src/mcp/sdk/mdb.ts@@ -62,23 +62,19 @@ export class MDBBackend implements mcpServer.ServerBackend {return { content: [{ type: 'text', text: 'Tools pushed' }] };}- const disposition = await this._mainBackend.beforeCallTool?.(name, args);- if (disposition?.resetOnPause) {- await this._onPauseClient?.transport.terminateSession().catch(errorsDebug);- await this._onPauseClient?.client.close().catch(errorsDebug);- this._onPauseClient = undefined;- }-- if (disposition?.fallbackToOnPause) {- if (!this._onPauseClient)- throw new Error(`Can't call tool while not in on-pause mode`);-- return await this._onPauseClient.client.callTool({+ if (this._onPauseClient?.tools.find(tool => tool.name === name)) {+ const result = await this._onPauseClient.client.callTool({name,arguments: args,}) as mcpServer.CallToolResult;+ await this._mainBackend.afterCallTool?.(name, args, result);+ return result;}+ await this._onPauseClient?.transport.terminateSession().catch(errorsDebug);+ await this._onPauseClient?.client.close().catch(errorsDebug);+ this._onPauseClient = undefined;+const resultPromise = new ManualPromise<mcpServer.CallToolResult>();const interruptPromise = new ManualPromise<mcpServer.CallToolResult>();this._interruptPromise = interruptPromise;diff --git a/packages/playwright/src/mcp/sdk/server.ts b/packages/playwright/src/mcp/sdk/server.tsindex 416f214e5c..3239cfa201 100644--- a/packages/playwright/src/mcp/sdk/server.ts+++ b/packages/playwright/src/mcp/sdk/server.ts@@ -43,7 +43,7 @@ export type ProgressCallback = (params: ProgressParams) => void;export interface ServerBackend {initialize?(server: Server, clientInfo: ClientInfo): Promise<void>;listTools(): Promise<Tool[]>;- beforeCallTool?(name: string, args: CallToolRequest['params']['arguments']): Promise<{ fallbackToOnPause?: boolean, resetOnPause?: boolean }>;+ afterCallTool?(name: string, args: CallToolRequest['params']['arguments'], result: CallToolResult): Promise<void>;callTool(name: string, args: CallToolRequest['params']['arguments'], progress: ProgressCallback): Promise<CallToolResult>;serverClosed?(server: Server): void;}diff --git a/packages/playwright/src/mcp/sdk/tool.ts b/packages/playwright/src/mcp/sdk/tool.tsindex bd94547a30..6441ff787e 100644--- a/packages/playwright/src/mcp/sdk/tool.ts+++ b/packages/playwright/src/mcp/sdk/tool.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import { zodToJsonSchema } from '../sdk/bundle';+import { z as zod, zodToJsonSchema } from '../sdk/bundle';import type { z } from 'zod';import type * as mcpServer from './server';@@ -24,18 +24,24 @@ export type ToolSchema<Input extends z.Schema> = {title: string;description: string;inputSchema: Input;- type: 'readOnly' | 'destructive';+ type: 'input' | 'assertion' | 'action' | 'readOnly';};-export function toMcpTool(tool: ToolSchema<any>): mcpServer.Tool {+const typesWithIntent = ['action', 'assertion', 'input'];++export function toMcpTool(tool: ToolSchema<any>, options?: { addIntent?: boolean }): mcpServer.Tool {+ const inputSchema = options?.addIntent && typesWithIntent.includes(tool.type) ? tool.inputSchema.extend({+ intent: zod.string().describe('The intent of the call, for example the test step description plan idea')+ }) : tool.inputSchema;+ const readOnly = tool.type === 'readOnly' || tool.type === 'assertion';return {name: tool.name,description: tool.description,- inputSchema: zodToJsonSchema(tool.inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'],+ inputSchema: zodToJsonSchema(inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'],annotations: {title: tool.title,- readOnlyHint: tool.type === 'readOnly',- destructiveHint: tool.type === 'destructive',+ readOnlyHint: readOnly,+ destructiveHint: !readOnly,openWorldHint: true,},};diff --git a/packages/playwright/src/mcp/test/DEPS.list b/packages/playwright/src/mcp/test/DEPS.listindex 4c8362b509..23b556217d 100644--- a/packages/playwright/src/mcp/test/DEPS.list+++ b/packages/playwright/src/mcp/test/DEPS.list@@ -2,6 +2,7 @@../sdk/../browser/config.ts../browser/browserServerBackend.ts+../browser/response.ts../browser/tab.ts../browser/tools/../../reportersdiff --git a/packages/playwright/src/mcp/test/generatorTools.ts b/packages/playwright/src/mcp/test/generatorTools.tsindex 318762c922..fbb6be740c 100644--- a/packages/playwright/src/mcp/test/generatorTools.ts+++ b/packages/playwright/src/mcp/test/generatorTools.ts@@ -42,26 +42,6 @@ export const setupPage = defineTestTool({},});-export const generatorLogStep = defineTestTool({- schema: {- name: 'generator_log_step',- title: 'Log a test step',- description: 'Log a successful test step.',- inputSchema: z.object({- title: z.string().describe('Title of the test step'),- code: z.string().describe('The code for the last step only, as a markdown code block'),- }),- type: 'readOnly',- },-- handle: async (context, params) => {- if (!context.generatorJournal)- throw new Error(`Please setup page using "${setupPage.schema.name}" first.`);- context.generatorJournal.logStep(params.title, params.code);- return { content: [] };- },-});-export const generatorReadLog = defineTestTool({schema: {name: 'generator_read_log',@@ -91,7 +71,7 @@ export const generatorWriteTest = defineTestTool({fileName: z.string().describe('The file to write the test to'),code: z.string().describe('The generated test code'),}),- type: 'destructive',+ type: 'readOnly',},handle: async (context, params) => {@@ -108,7 +88,9 @@ export const generatorWriteTest = defineTestTool({const testDir = path.relative(context.rootPath, project.project.testDir).replace(/\\/g, '/');const fileName = params.fileName.replace(/\\/g, '/');if (fileName.startsWith(testDir)) {- await fs.promises.writeFile(path.resolve(context.rootPath, fileName), params.code);+ const resolvedFile = path.resolve(context.rootPath, fileName);+ await fs.promises.mkdir(path.dirname(resolvedFile), { recursive: true });+ await fs.promises.writeFile(resolvedFile, params.code);return {content: [{type: 'text',diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.tsindex 66aa27c070..3338c08b94 100644--- a/packages/playwright/src/mcp/test/testBackend.ts+++ b/packages/playwright/src/mcp/test/testBackend.ts@@ -21,6 +21,7 @@ import * as generatorTools from './generatorTools.js';import * as plannerTools from './plannerTools.js';import { browserTools } from '../browser/tools';import { resolveConfigLocation } from '../../common/configLoader';+import { parseResponse } from '../browser/response';import type { TestTool } from './testTool';@@ -30,7 +31,6 @@ export class TestServerBackend implements mcp.ServerBackend {private _tools: TestTool<any>[] = [plannerTools.setupPage,generatorTools.setupPage,- generatorTools.generatorLogStep,generatorTools.generatorReadLog,generatorTools.generatorWriteTest,testTools.listTests,@@ -64,16 +64,16 @@ export class TestServerBackend implements mcp.ServerBackend {async listTools(): Promise<mcp.Tool[]> {return [...this._tools.map(tool => mcp.toMcpTool(tool.schema)),- ...browserTools.map(tool => mcp.toMcpTool(tool.schema)),+ ...browserTools.map(tool => mcp.toMcpTool(tool.schema, { addIntent: true })),];}- async beforeCallTool(name: string, args: mcp.CallToolRequest['params']['arguments']) {- if (browserTools.find(tool => tool.schema.name === name))- return { fallbackToOnPause: true };- if (name !== generatorTools.generatorLogStep.schema.name)- return { resetOnPause: true };- return {};+ async afterCallTool(name: string, args: mcp.CallToolRequest['params']['arguments'], result: mcp.CallToolResult) {+ if (!browserTools.find(tool => tool.schema.name === name))+ return;+ const response = parseResponse(result);+ if (response && !response.isError && response.code && typeof args?.['intent'] === 'string')+ this._context.generatorJournal?.logStep(args['intent'], response.code);}async callTool(name: string, args: mcp.CallToolRequest['params']['arguments'], progress: mcp.ProgressCallback): Promise<mcp.CallToolResult> {diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.tsindex fc9271939e..6148425616 100644--- a/packages/playwright/src/mcp/test/testContext.ts+++ b/packages/playwright/src/mcp/test/testContext.ts@@ -47,8 +47,9 @@ export class GeneratorJournal {this._steps = [];}- logStep(title: string, code: string) {- this._steps.push({ title, code });+ logStep(title: string | undefined, code: string) {+ if (title)+ this._steps.push({ title, code });}journal() {@@ -60,7 +61,10 @@ export class GeneratorJournal {result.push(this._seed.content);result.push('```');result.push(`# Steps`);- result.push(this._steps.map(step => `### ${step.title}\n${step.code}`).join('\n\n'));+ result.push(this._steps.map(step => `### ${step.title}+\`\`\`ts+${step.code}+\`\`\``).join('\n\n'));return result.join('\n\n');}}diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.tsindex aae9a3f588..28e296e324 100644--- a/tests/mcp/fixtures.ts+++ b/tests/mcp/fixtures.ts@@ -24,6 +24,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';import { ListRootsRequestSchema } from '@modelcontextprotocol/sdk/types.js';import { TestServer } from '../config/testserver';import { serverFixtures } from '../config/serverFixtures';+import { parseResponse } from '../../packages/playwright/lib/mcp/browser/response';import type { Config } from '../../packages/playwright/src/mcp/config';import type { BrowserContext } from 'playwright';@@ -31,6 +32,8 @@ import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';import type { Stream } from 'stream';import type { ServerFixtures, ServerWorkerOptions } from '../config/serverFixtures';+export { parseResponse };+export type TestOptions = {mcpArgs: string[] | undefined;mcpBrowser: string | undefined;@@ -254,51 +257,6 @@ export function formatOutput(output: string): string[] {return output.split('\n').map(line => line.replace(/^pw:mcp:test /, '').replace(/user data dir.*/, 'user data dir').trim()).filter(Boolean);}-export function parseResponse(response: any) {- const text = response.content[0].text;- const sections = parseSections(text);-- const result = sections.get('Result');- const code = sections.get('Ran Playwright code');- const tabs = sections.get('Open tabs');- const pageState = sections.get('Page state');- const consoleMessages = sections.get('New console messages');- const modalState = sections.get('Modal state');- const downloads = sections.get('Downloads');- const codeNoFrame = code?.replace(/^```js\n/, '').replace(/\n```$/, '');- const isError = response.isError;- const attachments = response.content.slice(1);-- return {- result,- code: codeNoFrame,- tabs,- pageState,- consoleMessages,- modalState,- downloads,- isError,- attachments,- };-}--function parseSections(text: string): Map<string, string> {- const sections = new Map<string, string>();- const sectionHeaders = text.split(/^### /m).slice(1); // Remove empty first element-- for (const section of sectionHeaders) {- const firstNewlineIndex = section.indexOf('\n');- if (firstNewlineIndex === -1)- continue;-- const sectionName = section.substring(0, firstNewlineIndex);- const sectionContent = section.substring(firstNewlineIndex + 1).trim();- sections.set(sectionName, sectionContent);- }-- return sections;-}-export const mcpServerPath = [path.join(__dirname, '../../packages/playwright/cli.js'), 'run-mcp-server'];export const testMcpServerPath = [path.join(__dirname, '../../packages/playwright-test/cli.js'), 'run-test-mcp-server'];diff --git a/tests/mcp/generator.spec.ts b/tests/mcp/generator.spec.tsindex ae9609ccc4..1ec5ca47ee 100644--- a/tests/mcp/generator.spec.ts+++ b/tests/mcp/generator.spec.ts@@ -20,6 +20,43 @@ import { test, expect, writeFiles } from './fixtures';test.use({ mcpServerType: 'test-mcp' });+test('generator tools intent', async ({ startClient }) => {+ const { client } = await startClient();+ const { tools } = await client.listTools();+ const toolsWithIntent: string[] = [];+ for (const tool of tools) {+ if (tool.inputSchema.properties?.intent)+ toolsWithIntent.push(tool.name);+ }++ expect(toolsWithIntent).toEqual([+ 'browser_close',+ 'browser_resize',+ 'browser_handle_dialog',+ 'browser_evaluate',+ 'browser_file_upload',+ 'browser_fill_form',+ 'browser_install',+ 'browser_press_key',+ 'browser_type',+ 'browser_navigate',+ 'browser_navigate_back',+ 'browser_mouse_move_xy',+ 'browser_mouse_click_xy',+ 'browser_mouse_drag_xy',+ 'browser_click',+ 'browser_drag',+ 'browser_hover',+ 'browser_select_option',+ 'browser_tabs',+ 'browser_wait_for',+ 'browser_verify_element_visible',+ 'browser_verify_text_visible',+ 'browser_verify_list_visible',+ 'browser_verify_value',+ ]);+});+test('generator_setup_page', async ({ startClient }) => {await writeFiles({'a.test.ts': `@@ -53,12 +90,11 @@ test('generator_setup_page', async ({ startClient }) => {`));await client.callTool({- name: 'generator_log_step',+ name: 'browser_click',arguments: {- title: 'Click submit button',- code: `\`\`\`ts-await page.getByRole('button', { name: 'Submit' }).click();-\`\`\``,+ element: 'Submit button',+ ref: 'e2',+ intent: 'Click submit button',},});@@ -113,21 +149,12 @@ test('click after generator_log_action', async ({ startClient }) => {},});- await client.callTool({- name: 'generator_log_step',- arguments: {- title: 'Click submit button',- code: `\`\`\`ts-await page.getByRole('button', { name: 'Submit' }).click();-\`\`\``,- },- });-expect(await client.callTool({name: 'browser_click',arguments: {element: 'Submit button',ref: 'e2',+ intent: 'Click submit button',},})).toHaveResponse({code: `await page.getByRole('button', { name: 'Submit' }).click();`,@@ -178,19 +205,6 @@ test('generator_setup_page is required', async ({ startClient }) => {const { client } = await startClient();- expect(await client.callTool({- name: 'generator_log_step',- arguments: {- title: 'Click submit button',- code: `\`\`\`ts-await page.getByRole('button', { name: 'Submit' }).click();-\`\`\``,- },- })).toEqual({- content: [{ type: 'text', text: `Error: Please setup page using "generator_setup_page" first.` }],- isError: true,- });-expect(await client.callTool({name: 'generator_read_log',arguments: {},diff --git a/tests/mcp/mdb.spec.ts b/tests/mcp/mdb.spec.tsindex 94a293c5e2..317266cb5b 100644--- a/tests/mcp/mdb.spec.ts+++ b/tests/mcp/mdb.spec.ts@@ -144,13 +144,11 @@ test('reset on pause tools', async () => {await expect.poll(() => log).toEqual(['CLI: initialize',- 'CLI: beforeCallTool cli_pause_in_gdb','CLI: callTool cli_pause_in_gdb','GDB: listTools',- 'CLI: beforeCallTool gdb_bt','GDB: initialize','GDB: callTool gdb_bt',- 'CLI: beforeCallTool cli_echo',+ 'CLI: afterCallTool gdb_bt','GDB: serverClosed','CLI: callTool cli_echo',]);@@ -226,11 +224,8 @@ class CLIBackend {}];}- async beforeCallTool(name: string, args: any) {- this._log.push(`CLI: beforeCallTool ${name}`);- if (name.startsWith('gdb_'))- return { fallbackToOnPause: true };- return { resetOnPause: true };+ async afterCallTool(name: string, args: any) {+ this._log.push(`CLI: afterCallTool ${name}`);}async callTool(name: string, args: any) {