Skip to main content
Glama
hrmeetsingh

MCP Browser Automation Server

by hrmeetsingh

playwright_evaluate

Execute JavaScript in the browser console for automation tasks with Playwright. Simplify web interactions by running user-defined scripts directly within the browser environment.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • Handler function for 'playwright_evaluate' tool: executes JavaScript using page.evaluate, overrides console methods to capture logs, returns execution result and console output.
    case "playwright_evaluate":
      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: `Execution result:\n${JSON.stringify(result.result, null, 2)}\n\nConsole 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,
          },
        };
      }
  • Tool schema definition including name, description, and input schema requiring a 'script' string parameter.
    {
      name: "playwright_evaluate",
      description: "Execute JavaScript in the browser console",
      inputSchema: {
        type: "object",
        properties: {
          script: { type: "string", description: "JavaScript code to execute" },
        },
        required: ["script"],
      },
    },
  • Helper constant listing browser tools including 'playwright_evaluate' to conditionally launch browser in handleToolCall.
    export const BROWSER_TOOLS = [
      "playwright_navigate",
      "playwright_screenshot",
      "playwright_click",
      "playwright_fill",
      "playwright_select",
      "playwright_hover",
      "playwright_evaluate"
    ];
  • src/index.ts:22-26 (registration)
    Registration of all tools (including 'playwright_evaluate') via createToolDefinitions and setupRequestHandlers on the MCP server.
    // Create tool definitions
    const TOOLS = createToolDefinitions();
    
    // Setup request handlers
    setupRequestHandlers(server, TOOLS);
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. While 'Execute JavaScript' implies a mutation operation, it doesn't specify whether this runs in a specific context (e.g., current page vs. isolated sandbox), what permissions are needed, whether it can modify page state, or what happens with errors. The description lacks crucial behavioral context for a JavaScript execution 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 extremely concise at just 5 words, front-loading the essential information ('Execute JavaScript') immediately. Every word earns its place, with no wasted text or unnecessary elaboration.

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 JavaScript execution tool with no annotations and no output schema, the description is insufficiently complete. It doesn't explain what the tool returns (e.g., evaluation result, success status), error handling, execution context, or security implications. Given the complexity of browser JavaScript execution, more context is needed.

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 schema description coverage is 100%, with the single parameter 'script' clearly documented as 'JavaScript code to execute'. The description adds no additional parameter semantics beyond what the schema already provides, so it meets the baseline for high schema coverage without adding extra value.

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 ('Execute JavaScript') and location ('in the browser console'), making the purpose immediately understandable. However, it doesn't distinguish this tool from potential alternatives like other JavaScript execution methods or differentiate it from sibling tools that also interact with browser elements.

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. With sibling tools like playwright_click, playwright_fill, and playwright_navigate available, there's no indication whether this is for general JavaScript execution versus specific browser automation tasks, or any prerequisites for its use.

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

Related 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/hrmeetsingh/mcp-browser-automation'

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