Skip to main content
Glama

evaluate

Execute JavaScript code in browser contexts to automate interactions, extract content, or manipulate web pages programmatically.

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

  • Handler function for the 'evaluate' tool. Uses Puppeteer's page.evaluate to execute the provided JavaScript script in the browser context using new Function constructor, handles errors with evaluationError.
    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 on the MCP server within registerContentTools, specifying 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 schema defining the input parameters for the evaluate tool: script (string, 1-50000 chars) and optional tabId.
    export const evaluateSchema = z.object({ script: z.string().min(1).max(50000).describe('JavaScript code to execute'), tabId: tabIdSchema, });
  • TypeScript type alias for the input of evaluate tool inferred from the Zod schema.
    export type EvaluateInput = z.infer<typeof evaluateSchema>;
  • TypeScript interface defining the output structure of the evaluate tool: { result: unknown }.
    export interface EvaluateResult { /** Result of JavaScript evaluation (serialized) */ result: unknown; }

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