playwright_evaluate
Execute JavaScript code directly in a browser console to automate web interactions, scrape content, or test functionality using Playwright's browser automation capabilities.
Instructions
Execute JavaScript in the browser console
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/tools/browser/interaction.ts:155-178 (handler)The EvaluateTool class implements the core logic for the playwright_evaluate tool. It executes JavaScript using page.evaluate() on the browser page and formats the result for display.export class EvaluateTool extends BrowserToolBase { /** * Execute the evaluate tool */ async execute(args: any, context: ToolContext): Promise<ToolResponse> { return this.safeExecute(context, async (page) => { const result = await page.evaluate(args.script); // Convert result to string for display let resultStr: string; try { resultStr = JSON.stringify(result, null, 2); } catch (error) { resultStr = String(result); } return createSuccessResponse([ `Executed JavaScript:`, `${args.script}`, `Result:`, `${resultStr}` ]); }); }
- src/tools.ts:196-205 (schema)Defines the tool name, description, and input schema (requiring a 'script' string) for registration in the MCP tools list.{ name: "playwright_evaluate", description: "Execute JavaScript in the browser console", inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" }, }, required: ["script"], },
- src/toolHandler.ts:505-506 (registration)Switch case in the main tool handler that dispatches calls to the EvaluateTool instance.case "playwright_evaluate": return await evaluateTool.execute(args, context);
- src/toolHandler.ts:328-328 (registration)Instantiation of the EvaluateTool class instance used for handling tool calls.if (!evaluateTool) evaluateTool = new EvaluateTool(server);
- src/tools.ts:460-460 (registration)Inclusion in BROWSER_TOOLS array for conditional browser launch logic."playwright_evaluate",