Skip to main content
Glama

browser_evaluate

Execute JavaScript code directly in the browser to manipulate DOM, retrieve data, or test functionality. Provides dynamic interaction with web pages through script execution.

Instructions

Execute JavaScript in the browser context

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The handler function `handleBrowserEvaluate` that executes JavaScript in the browser context via Playwright's `page.evaluate()`. It intercepts console.log/info/warn/error, runs `eval(script)` in the page, and returns both the script result (JSON-stringified) and any console output.
    async function handleBrowserEvaluate(page: Page, args: any): Promise<{ toolResult: CallToolResult }> {
      try {
        const result = await page.evaluate((script) => {
    
          const logs: string[] = [];
          const originalConsole = { ...console };
    
    
          ['log', 'info', 'warn', 'error'].forEach(method => {
            (console as any)[method] = (...args: any[]) => {
              logs.push(`[${method}] ${args.join(' ')}`);
              (originalConsole as any)[method](...args);
            };
          });
    
          try {
    
            const result = eval(script);
    
            Object.assign(console, originalConsole);
            return { result, logs };
          } catch (error) {
    
            Object.assign(console, originalConsole);
            throw error;
          }
        }, args.script);
    
        return {
          toolResult: {
            content: [
              {
                type: "text",
                text: `Script result: ${JSON.stringify(result.result, null, 2)}`,
              },
              {
                type: "text",
                text: `Console output:\n${result.logs.join('\n')}`,
              }
            ],
            isError: false,
          },
        };
      } catch (error) {
        return {
          toolResult: {
            content: [{
              type: "text",
              text: `Script execution failed: ${(error as Error).message}`,
            }],
            isError: true,
          },
        };
      }
    }
  • The input schema definition for 'browser_evaluate' tool, requiring a 'script' string property (JavaScript code to execute).
    {
      name: "browser_evaluate",
      description: "Execute JavaScript in the browser context",
      inputSchema: {
        type: "object",
        properties: {
          script: { type: "string", description: "JavaScript code to execute" }
        },
        required: ["script"]
      }
    },
  • src/tools.ts:3-12 (registration)
    The tool string 'browser_evaluate' is registered in the BROWSER_TOOLS array which is used by the executor to route tool calls to the browser.
    export const BROWSER_TOOLS = [
      "browser_navigate",
      "browser_screenshot",
      "browser_click",
      "browser_fill",
      "browser_select",
      "browser_hover",
      "browser_evaluate",
      "browser_set_viewport"
    ];
  • The switch-case dispatcher in `executeToolCall` that routes the 'browser_evaluate' tool name to the `handleBrowserEvaluate` handler function.
    case "browser_evaluate":
      return await handleBrowserEvaluate(activePage!, args);
Behavior2/5

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

With no annotations, the description must fully disclose behavior. It mentions script execution but omits critical details: return value, side effects, permissions, sandboxing, or error handling. This is insufficient for a potentially powerful tool.

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, clear sentence with no unnecessary words. It is optimally concise.

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?

The tool has no output schema, so the description should explain return values or behavior. It does not. Additionally, it lacks details on execution context (e.g., async support, timeout). This leaves the agent underinformed.

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?

The single required parameter 'script' has a description in the schema. The tool description adds no additional meaning beyond what the schema already provides, making it adequate but not additive.

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 'Execute JavaScript in the browser context' clearly states the action and resource. It is specific and distinct from sibling tools like browser_click or api_get.

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 (e.g., browser_navigate or API calls). An agent receives no context for decision-making.

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/imprvhub/mcp-browser-agent'

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