playwright_evaluate
Execute JavaScript in the browser console for automation tasks with Playwright. Simplify web interactions by running user-defined scripts directly within the browser environment.
Instructions
Execute JavaScript in the browser console
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/toolsHandler.ts:274-318 (handler)Handler function for 'playwright_evaluate' tool: executes JavaScript using page.evaluate, overrides console methods to capture logs, returns execution result and console output.case "playwright_evaluate": try { const result = await page!.evaluate((script) => { const logs: string[] = []; const originalConsole = { ...console }; ['log', 'info', 'warn', 'error'].forEach(method => { (console as any)[method] = (...args: any[]) => { logs.push(`[${method}] ${args.join(' ')}`); (originalConsole as any)[method](...args); }; }); try { const result = eval(script); Object.assign(console, originalConsole); return { result, logs }; } catch (error) { Object.assign(console, originalConsole); throw error; } }, args.script); return { toolResult: { content: [ { type: "text", text: `Execution result:\n${JSON.stringify(result.result, null, 2)}\n\nConsole output:\n${result.logs.join('\n')}`, }, ], isError: false, }, }; } catch (error) { return { toolResult: { content: [{ type: "text", text: `Script execution failed: ${(error as Error).message}`, }], isError: true, }, }; }
- src/tools.ts:79-89 (schema)Tool schema definition including name, description, and input schema requiring a 'script' string parameter.{ 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/tools.ts:152-160 (helper)Helper constant listing browser tools including 'playwright_evaluate' to conditionally launch browser in handleToolCall.export const BROWSER_TOOLS = [ "playwright_navigate", "playwright_screenshot", "playwright_click", "playwright_fill", "playwright_select", "playwright_hover", "playwright_evaluate" ];
- src/index.ts:22-26 (registration)Registration of all tools (including 'playwright_evaluate') via createToolDefinitions and setupRequestHandlers on the MCP server.// Create tool definitions const TOOLS = createToolDefinitions(); // Setup request handlers setupRequestHandlers(server, TOOLS);