Skip to main content
Glama

browser_evaluate

Destructive

Execute JavaScript code on web pages or specific elements to extract data, modify content, or automate interactions within browser environments.

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

  • The main handler function for the browser_evaluate tool. Evaluates the provided JavaScript function on the current page or a specific element locator using Playwright's evaluate and _evaluateFunction methods.
    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 input schema and tool metadata definition for browser_evaluate, including name, title, description, and parameters (function, element, ref).
    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'),
    });
    
    const evaluate = defineTabTool({
      capability: 'core',
      schema: {
        name: 'browser_evaluate',
        title: 'Evaluate JavaScript',
        description: 'Evaluate JavaScript expression on page or element',
        inputSchema: evaluateSchema,
        type: 'destructive',
      },
  • src/tools.ts:36-52 (registration)
    The browser_evaluate tool is registered in the central allTools array by spreading the exports from the evaluate module.
    export const allTools: Tool<any>[] = [
      ...common,
      ...console,
      ...dialogs,
      ...evaluate,
      ...files,
      ...install,
      ...keyboard,
      ...navigate,
      ...network,
      ...mouse,
      ...pdf,
      ...screenshot,
      ...snapshot,
      ...tabs,
      ...wait,
    ];
  • ServerBackend.tools() method returns the schemas of all tools (including browser_evaluate) for MCP tool listing.
    tools(): mcpServer.ToolSchema<any>[] {
      return this._tools.map(tool => tool.schema);
  • ServerBackend.callTool dispatches to the specific tool's handle function (browser_evaluate handler) based on tool name.
    async callTool(schema: mcpServer.ToolSchema<any>, parsedArguments: any) {
      const context = this._context!;
      const response = new Response(context, schema.name, parsedArguments);
      const tool = this._tools.find(tool => tool.schema.name === schema.name)!;
      context.setRunningTool(true);
      try {
        await tool.handle(context, parsedArguments, response);
        await response.finish();
        this._sessionLog?.logResponse(response);
      } catch (error: any) {
        response.addError(String(error));
      } finally {
        context.setRunningTool(false);
      }
      return response.serialize();
Behavior3/5

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

Annotations already declare destructiveHint=true and readOnlyHint=false, indicating this is a potentially unsafe write operation. The description adds minimal context about evaluating JavaScript but doesn't elaborate on risks (e.g., side effects, permissions needed for element interaction) or behavioral traits like execution scope. It doesn't contradict annotations, but adds limited value beyond them.

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 conveys the core functionality without unnecessary words. It's front-loaded and wastes no space, making it easy for an agent to parse quickly.

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?

For a destructive tool (per annotations) with no output schema, the description is insufficient. It doesn't explain what 'Evaluate' entails (e.g., returns a value, executes code), potential errors, or safety considerations. Given the complexity and lack of output details, more context is needed for effective use.

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 mentions 'page or element' which loosely relates to the element/ref parameters but adds no specific semantics beyond what the schema provides. With high schema coverage, the baseline 3 is appropriate.

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 siblings like browser_click or browser_type that perform different browser interactions. However, it doesn't explicitly differentiate from potential JavaScript-related siblings (none exist in the provided list), so it's not a perfect 5.

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 prerequisites (e.g., needing a page loaded), exclusions, or comparisons to other tools like browser_console_messages for JavaScript output. Usage is implied but not articulated.

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/maywzh/playwright-mcp'

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