Skip to main content
Glama

playwright_evaluate

Execute JavaScript code directly in the browser console to automate web interactions, extract data, or manipulate page elements through the Playwright framework.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The main implementation of the 'playwright_evaluate' tool handler. It uses page.evaluate to run the script in the browser context, overrides console methods to capture logs, and returns the 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 { 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 { content: [{ type: "text", text: `Script execution failed: ${(error as Error).message}`, }], isError: true, }; }
  • The schema definition for the 'playwright_evaluate' tool, including name, description, and input schema requiring a 'script' string.
    { name: "playwright_evaluate", description: "Execute JavaScript in the browser console", inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" }, }, required: ["script"], }, },
  • The BROWSER_TOOLS constant lists 'playwright_evaluate' among browser-requiring tools, used in the handler to conditionally launch the browser.
    export const BROWSER_TOOLS = [ "playwright_navigate", "playwright_screenshot", "playwright_click", "playwright_fill", "playwright_select", "playwright_hover", "playwright_evaluate" ];
  • Registration of all tools (including 'playwright_evaluate') via the ListToolsRequestSchema handler, which returns the list of tool definitions.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: tools, }));

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lars-hagen/mcp-playwright-cdp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server