Skip to main content
Glama
merajmehrabi

Puppeteer MCP Server

by merajmehrabi

puppeteer_evaluate

Execute JavaScript code directly in a browser console to automate web interactions, extract data, or manipulate page elements during browser automation sessions.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The main handler function for the puppeteer_evaluate tool, which sets up console logging, executes the provided JavaScript script in the browser page context using page.evaluate, handles serialization, captures logs, and returns the result or error.
    case "puppeteer_evaluate": try { // Set up console listener const logs: string[] = []; const consoleListener = (message: any) => { logs.push(`${message.type()}: ${message.text()}`); }; page.on('console', consoleListener); // Execute script with proper serialization logger.debug('Executing script in browser', { scriptLength: args.script.length }); // Wrap the script in a function that returns a serializable result const result = await page.evaluate(`(async () => { try { const result = (function() { ${args.script} })(); return result; } catch (e) { console.error('Script execution error:', e.message); return { error: e.message }; } })()`); // Remove the listener to avoid memory leaks page.off('console', consoleListener); logger.debug('Script execution result', { resultType: typeof result, hasResult: result !== undefined, logCount: logs.length }); return { content: [{ type: "text", text: `Execution result:\n${JSON.stringify(result, null, 2)}\n\nConsole output:\n${logs.join('\n')}`, }], isError: false, }; } catch (error) { logger.error('Script evaluation failed', { error: error instanceof Error ? error.message : String(error) }); return { content: [{ type: "text", text: `Script execution failed: ${error instanceof Error ? error.message : String(error)}\n\nPossible causes:\n- Syntax error in script\n- Execution timeout\n- Browser security restrictions\n- Serialization issues with complex objects`, }], isError: true, }; }
  • The tool definition including name, description, and input schema requiring a 'script' string 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"], }, },
  • src/server.ts:35-37 (registration)
    Registration of the puppeteer_evaluate tool as part of the TOOLS array returned in response to ListTools requests.
    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/merajmehrabi/puppeteer-mcp-server'

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