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,
    ];
Behavior4/5

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

Annotations provide readOnlyHint=false, destructiveHint=true, and openWorldHint=true, indicating this is a potentially dangerous write operation with unpredictable outcomes. The description adds context by specifying it evaluates JavaScript 'on page or element,' which clarifies the scope beyond what annotations provide. However, it doesn't elaborate on specific risks, side effects, or execution environment details that would be helpful given the destructive nature.

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 immediately communicates the core functionality. Every word earns its place with no redundancy or unnecessary elaboration. It's perfectly front-loaded and appropriately sized for a tool with good schema documentation.

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 with destructive potential), the description is minimally adequate. Annotations cover safety aspects, and the schema fully documents parameters, but there's no output schema to explain return values. The description doesn't address execution context, error handling, or security implications that would be valuable for such a powerful tool.

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%, with clear documentation for all three parameters. The description adds minimal value beyond the schema, only implying that 'element' and 'ref' parameters relate to targeting specific elements. It doesn't explain parameter interactions or provide examples of valid JavaScript expressions, so it meets but doesn't exceed the baseline expectation.

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 verb ('Evaluate') and resource ('JavaScript expression on page or element'), making the purpose immediately understandable. It distinguishes this from siblings like browser_click or browser_type by focusing on JavaScript evaluation rather than UI interaction. However, it doesn't explicitly differentiate from browser_run_code, which might be a similar sibling.

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 when to choose browser_evaluate over browser_run_code (a likely similar sibling) or other JavaScript execution tools. There's no context about appropriate use cases, prerequisites, or limitations beyond what's implied by the parameters.

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/nzjami/mcpPlaywright'

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