Skip to main content
Glama
merajmehrabi

Puppeteer MCP Server

by merajmehrabi

puppeteer_evaluate

Run custom JavaScript in the browser console to automate tasks, extract data, or interact with web elements using MCP server’s Puppeteer-based browser automation capabilities.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The switch case in handleToolCall function that implements the puppeteer_evaluate tool. It sets up console logging, executes the provided JavaScript script using page.evaluate with error handling and serialization, collects console output, and returns the result.
    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 for puppeteer_evaluate, exported in the TOOLS array.
    { 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:34-41 (registration)
    Registration of MCP tool request handlers. ListToolsRequestSchema returns the TOOLS array (including puppeteer_evaluate schema), and CallToolRequestSchema invokes the handleToolCall dispatcher.
    // Setup tool handlers server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS, })); server.setRequestHandler(CallToolRequestSchema, async (request) => handleToolCall(request.params.name, request.params.arguments ?? {}, state, server) );

Other Tools

Related 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