Skip to main content
Glama
nzjami

Playwright MCP

by nzjami

browser_evaluate

Destructive

Execute JavaScript code on web pages or specific elements to extract data, manipulate content, or automate interactions during browser automation sessions.

Instructions

Evaluate JavaScript expression on page or element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
functionYes() => { /* code */ } or (element) => { /* code */ } when element is provided
elementNoHuman-readable element description used to obtain permission to interact with the element
refNoExact target element reference from the page snapshot

Implementation Reference

  • Handler function for the browser_evaluate tool that evaluates the provided JavaScript function on the page or a specific element locator.
    handle: async (tab, params, response) => {
      response.setIncludeSnapshot();
    
      let locator: playwright.Locator | undefined;
      if (params.ref && params.element) {
        locator = await tab.refLocator({ ref: params.ref, element: params.element });
        response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
      } else {
        response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
      }
    
      await tab.waitForCompletion(async () => {
        const receiver = locator ?? tab.page as any;
        const result = await receiver._evaluateFunction(params.function);
        response.addResult(JSON.stringify(result, null, 2) || 'undefined');
      });
    },
  • Zod schema defining the input parameters for browser_evaluate: function (JS code), element (optional description), ref (optional element reference).
    const evaluateSchema = z.object({
      function: z.string().describe('() => { /* code */ } or (element) => { /* code */ } when element is provided'),
      element: z.string().optional().describe('Human-readable element description used to obtain permission to interact with the element'),
      ref: z.string().optional().describe('Exact target element reference from the page snapshot'),
    });
  • Tool definition and export using defineTabTool, specifying name 'browser_evaluate', schema, capability 'core', and the handle function.
    const evaluate = defineTabTool({
      capability: 'core',
      schema: {
        name: 'browser_evaluate',
        title: 'Evaluate JavaScript',
        description: 'Evaluate JavaScript expression on page or element',
        inputSchema: evaluateSchema,
        type: 'destructive',
      },
    
      handle: async (tab, params, response) => {
        response.setIncludeSnapshot();
    
        let locator: playwright.Locator | undefined;
        if (params.ref && params.element) {
          locator = await tab.refLocator({ ref: params.ref, element: params.element });
          response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
        } else {
          response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
        }
    
        await tab.waitForCompletion(async () => {
          const receiver = locator ?? tab.page as any;
          const result = await receiver._evaluateFunction(params.function);
          response.addResult(JSON.stringify(result, null, 2) || 'undefined');
        });
      },
    });
  • src/tools.ts:36-52 (registration)
    Imports evaluate.ts (line 20) and spreads its tools (line 40) into the central allTools array used for tool registration.
    export const allTools: Tool<any>[] = [
      ...common,
      ...console,
      ...dialogs,
      ...evaluate,
      ...files,
      ...install,
      ...keyboard,
      ...navigate,
      ...network,
      ...mouse,
      ...pdf,
      ...screenshot,
      ...snapshot,
      ...tabs,
      ...wait,
    ];

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

A3.5/5.0
Behavior4/5

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

The description doesn't contradict annotations. Annotations indicate this is a destructive, non-read-only operation with open-world characteristics. The description adds context by specifying it evaluates JavaScript on 'page or element,' which helps clarify the scope beyond what annotations provide. However, it doesn't detail potential side effects like page modifications or security implications.

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 extremely concise—a single, clear phrase with no wasted words. It's front-loaded with the core functionality, making it easy to understand at a glance without unnecessary elaboration.

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

Completeness3/5

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

Given the tool's complexity (JavaScript evaluation in a browser with destructive potential) and lack of output schema, the description is minimal. It covers the basic purpose but doesn't address return values, error handling, or security considerations, leaving gaps for an AI agent to infer behavior in a potentially risky operation.

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?

With 100% schema description coverage, the schema already documents all three parameters thoroughly. The description adds minimal value beyond the schema by implying the 'element' parameter is optional (evaluating on 'page or element'), but doesn't provide additional syntax or usage details. This meets the baseline for high schema coverage.

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

Purpose4/5

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

The description clearly states the action ('evaluate') and target ('JavaScript expression on page or element'), distinguishing it from sibling tools that perform physical interactions like click, type, or navigate. However, it doesn't specify that this is for browser automation contexts, which is implied but could be more explicit.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention that this is for executing custom JavaScript code in a browser context, nor does it differentiate from other browser tools like browser_console_messages (for reading console output) or browser_snapshot (for capturing page state).

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