Skip to main content
Glama

evaluate

Execute JavaScript code in browser tabs to automate interactions, extract content, or manipulate web pages using Puppeteer MCP Server.

Instructions

Execute JavaScript code in the browser context and return the result

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • The handler function for the 'evaluate' tool. It retrieves the browser page for the given tab, executes the provided JavaScript script using Puppeteer's page.evaluate with new Function, and returns the result or handles errors appropriately.
    async ({ script, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Use Function constructor to evaluate the script const result = await page.evaluate((code) => { // eslint-disable-next-line no-new-func const fn = new Function(code); return fn(); }, script); return handleResult(ok({ result })); } catch (error) { if (error instanceof Error) { return handleResult(err(evaluationError(error.message))); } return handleResult(err(normalizeError(error))); } }
  • Registration of the 'evaluate' tool using server.tool, including name, description, input schema, and handler function.
    server.tool( 'evaluate', 'Execute JavaScript code in the browser context and return the result', evaluateSchema.shape, async ({ script, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Use Function constructor to evaluate the script const result = await page.evaluate((code) => { // eslint-disable-next-line no-new-func const fn = new Function(code); return fn(); }, script); return handleResult(ok({ result })); } catch (error) { if (error instanceof Error) { return handleResult(err(evaluationError(error.message))); } return handleResult(err(normalizeError(error))); } } );
  • Zod input schema for the 'evaluate' tool defining the expected parameters: script (string) and optional tabId.
    export const evaluateSchema = z.object({ script: z.string().min(1).max(50000).describe('JavaScript code to execute'), tabId: tabIdSchema, });
  • TypeScript interface defining the output type for the 'evaluate' tool result.
    */ export interface EvaluateResult { /** Result of JavaScript evaluation (serialized) */ result: unknown; }
  • src/server.ts:25-25 (registration)
    Invocation of registerContentTools which registers the 'evaluate' tool among others.
    registerContentTools(server);

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/andytango/puppeteer-mcp'

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