puppeteer_evaluate
Execute JavaScript code directly in a browser console to automate web interactions, extract data, or manipulate page content through browser automation.
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)The handler logic for the puppeteer_evaluate tool. Evaluates JavaScript in the browser page context, overrides console methods to capture logs, and returns the execution result along with captured console output.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:177-183 (schema)Input schema for the puppeteer_evaluate tool, defining the required 'script' parameter as a string.inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" }, }, required: ["script"], },
- index.ts:174-184 (registration)Tool registration object in the TOOLS array, including name, description, and input schema. This array is returned by the listTools handler.{ name: "puppeteer_evaluate", description: "Execute JavaScript in the browser console", inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" }, }, required: ["script"], }, },