puppeteer_evaluate
Execute JavaScript in a browser console to automate web interactions, extract data, or manipulate web pages using the MCP Puppeteer Linux Server.
Instructions
Execute JavaScript in the browser console
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- index.ts:343-382 (handler)Handler for puppeteer_evaluate: evaluates the provided JavaScript script in the browser page context, captures console logs by overriding console methods, and returns the result along with captured logs.case "puppeteer_evaluate": try { const result = await page.evaluate((script) => { const logs = []; const originalConsole = { ...console }; ['log', 'info', 'warn', 'error'].forEach(method => { console[method] = (...args) => { logs.push(`[${method}] ${args.join(' ')}`); originalConsole[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.message}`, }], isError: true, }; }
- index.ts:174-184 (schema)Input schema definition for the puppeteer_evaluate tool, specifying the 'script' parameter.{ name: "puppeteer_evaluate", description: "Execute JavaScript in the browser console", inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" }, }, required: ["script"], }, },
- index.ts:451-451 (registration)Registration of the generic tool call handler that dispatches to handleToolCall based on tool name, including puppeteer_evaluate.server.setRequestHandler(CallToolRequestSchema, async (request) => handleToolCall(request.params.name, request.params.arguments ?? {}));
- index.ts:447-449 (registration)Registration of the list tools handler that exposes the TOOLS array containing puppeteer_evaluate.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS, }));