Skip to main content
Glama
andytango
by andytango

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;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions execution in the browser context and returning a result, but lacks details on error handling, execution timeouts, security implications, or whether it requires specific browser permissions. This is a significant gap for a tool that executes arbitrary code.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core action ('Execute JavaScript code') and includes essential context and outcome. There is no wasted verbiage, making it highly concise and well-structured for quick understanding.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of executing JavaScript in a browser, the lack of annotations, and no output schema, the description is incomplete. It does not cover critical aspects like error responses, return value formats, or safety considerations, leaving gaps that could hinder effective tool use by an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters ('script' and 'tabId') with clear descriptions. The description does not add any additional meaning beyond what the schema provides, such as examples of valid JavaScript code or details on how 'tabId' interacts with browser tabs.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Execute JavaScript code') and the context ('in the browser context'), distinguishing it from sibling tools that perform different browser automation tasks like clicking, navigating, or taking screenshots. It explicitly mentions the return value ('and return the result'), which is crucial for understanding its function.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for executing JavaScript in a browser, but it does not explicitly state when to use this tool versus alternatives like 'query_selector' for DOM queries or 'get_content' for content extraction. No guidance is provided on prerequisites, such as requiring an active browser session or specific permissions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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