Skip to main content
Glama
pvinis
by pvinis

playwright_evaluate

Execute JavaScript in a browser console to interact with web pages or automate tasks directly from the Playwright MCP Server environment.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The EvaluateTool class provides the core handler logic for 'playwright_evaluate', executing the provided JavaScript script using page.evaluate() and formatting the result for response.
    export class EvaluateTool extends BrowserToolBase {
      /**
       * Execute the evaluate tool
       */
      async execute(args: any, context: ToolContext): Promise<ToolResponse> {
        return this.safeExecute(context, async (page) => {
          const result = await page.evaluate(args.script);
          
          // Convert result to string for display
          let resultStr: string;
          try {
            resultStr = JSON.stringify(result, null, 2);
          } catch (error) {
            resultStr = String(result);
          }
          
          return createSuccessResponse([
            `Executed JavaScript:`,
            `${args.script}`,
            `Result:`,
            `${resultStr}`
          ]);
        });
      }
  • The input schema definition for the 'playwright_evaluate' tool, specifying the 'script' 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"],
      },
    },
  • Registration and dispatching of 'playwright_evaluate' tool call to the EvaluateTool instance in the main tool handler switch statement.
    case "playwright_evaluate":
      return await evaluateTool.execute(args, context);
  • Instantiation of the EvaluateTool instance used for handling 'playwright_evaluate' tool calls.
    if (!evaluateTool) evaluateTool = new EvaluateTool(server);
  • src/tools.ts:4-402 (registration)
    The 'playwright_evaluate' tool is registered in the createToolDefinitions() function which returns the array of all MCP tool definitions.
    export function createToolDefinitions() {
      return [
        // Codegen tools
        {
          name: "start_codegen_session",
          description: "Start a new code generation session to record Playwright actions",
          inputSchema: {
            type: "object",
            properties: {
              options: {
                type: "object",
                description: "Code generation options",
                properties: {
                  outputPath: { 
                    type: "string", 
                    description: "Directory path where generated tests will be saved (use absolute path)" 
                  },
                  testNamePrefix: { 
                    type: "string", 
                    description: "Prefix to use for generated test names (default: 'GeneratedTest')" 
                  },
                  includeComments: { 
                    type: "boolean", 
                    description: "Whether to include descriptive comments in generated tests" 
                  }
                },
                required: ["outputPath"]
              }
            },
            required: ["options"]
          }
        },
        {
          name: "end_codegen_session",
          description: "End a code generation session and generate the test file",
          inputSchema: {
            type: "object",
            properties: {
              sessionId: { 
                type: "string", 
                description: "ID of the session to end" 
              }
            },
            required: ["sessionId"]
          }
        },
        {
          name: "get_codegen_session",
          description: "Get information about a code generation session",
          inputSchema: {
            type: "object",
            properties: {
              sessionId: { 
                type: "string", 
                description: "ID of the session to retrieve" 
              }
            },
            required: ["sessionId"]
          }
        },
        {
          name: "clear_codegen_session",
          description: "Clear a code generation session without generating a test",
          inputSchema: {
            type: "object",
            properties: {
              sessionId: { 
                type: "string", 
                description: "ID of the session to clear" 
              }
            },
            required: ["sessionId"]
          }
        },
        {
          name: "playwright_navigate",
          description: "Navigate to a URL",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to navigate to the website specified" },
              browserType: { type: "string", description: "Browser type to use (chromium, firefox, webkit). Defaults to chromium", enum: ["chromium", "firefox", "webkit"] },
              width: { type: "number", description: "Viewport width in pixels (default: 1280)" },
              height: { type: "number", description: "Viewport height in pixels (default: 720)" },
              timeout: { type: "number", description: "Navigation timeout in milliseconds" },
              waitUntil: { type: "string", description: "Navigation wait condition" },
              headless: { type: "boolean", description: "Run browser in headless mode (default: false)" }
            },
            required: ["url"],
          },
        },
        {
          name: "playwright_screenshot",
          description: "Take a screenshot of the current page or a specific element",
          inputSchema: {
            type: "object",
            properties: {
              name: { type: "string", description: "Name for the screenshot" },
              selector: { type: "string", description: "CSS selector for element to screenshot" },
              width: { type: "number", description: "Width in pixels (default: 800)" },
              height: { type: "number", description: "Height in pixels (default: 600)" },
              storeBase64: { type: "boolean", description: "Store screenshot in base64 format (default: true)" },
              fullPage: { type: "boolean", description: "Store screenshot of the entire page (default: false)" },
              savePng: { type: "boolean", description: "Save screenshot as PNG file (default: false)" },
              downloadsDir: { type: "string", description: "Custom downloads directory path (default: user's Downloads folder)" },
            },
            required: ["name"],
          },
        },
        {
          name: "playwright_click",
          description: "Click an element on the page",
          inputSchema: {
            type: "object",
            properties: {
              selector: { type: "string", description: "CSS selector for the element to click" },
            },
            required: ["selector"],
          },
        },
        {
          name: "playwright_iframe_click",
          description: "Click an element in an iframe on the page",
          inputSchema: {
            type: "object",
            properties: {
              iframeSelector: { type: "string", description: "CSS selector for the iframe containing the element to click" },
              selector: { type: "string", description: "CSS selector for the element to click" },
            },
            required: ["iframeSelector", "selector"],
          },
        },
        {
          name: "playwright_fill",
          description: "fill out an input field",
          inputSchema: {
            type: "object",
            properties: {
              selector: { type: "string", description: "CSS selector for input field" },
              value: { type: "string", description: "Value to fill" },
            },
            required: ["selector", "value"],
          },
        },
        {
          name: "playwright_select",
          description: "Select an element on the page with Select tag",
          inputSchema: {
            type: "object",
            properties: {
              selector: { type: "string", description: "CSS selector for element to select" },
              value: { type: "string", description: "Value to select" },
            },
            required: ["selector", "value"],
          },
        },
        {
          name: "playwright_hover",
          description: "Hover an element on the page",
          inputSchema: {
            type: "object",
            properties: {
              selector: { type: "string", description: "CSS selector for element to hover" },
            },
            required: ["selector"],
          },
        },
        {
          name: "playwright_evaluate",
          description: "Execute JavaScript in the browser console",
          inputSchema: {
            type: "object",
            properties: {
              script: { type: "string", description: "JavaScript code to execute" },
            },
            required: ["script"],
          },
        },
        {
          name: "playwright_console_logs",
          description: "Retrieve console logs from the browser with filtering options",
          inputSchema: {
            type: "object",
            properties: {
              type: {
                type: "string",
                description: "Type of logs to retrieve (all, error, warning, log, info, debug)",
                enum: ["all", "error", "warning", "log", "info", "debug"]
              },
              search: {
                type: "string",
                description: "Text to search for in logs (handles text with square brackets)"
              },
              limit: {
                type: "number",
                description: "Maximum number of logs to return"
              },
              clear: {
                type: "boolean",
                description: "Whether to clear logs after retrieval (default: false)"
              }
            },
            required: [],
          },
        },
        {
          name: "playwright_close",
          description: "Close the browser and release all resources",
          inputSchema: {
            type: "object",
            properties: {},
            required: [],
          },
        },
        {
          name: "playwright_get",
          description: "Perform an HTTP GET request",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to perform GET operation" }
            },
            required: ["url"],
          },
        },
        {
          name: "playwright_post",
          description: "Perform an HTTP POST request",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to perform POST operation" },
              value: { type: "string", description: "Data to post in the body" },
              token: { type: "string", description: "Bearer token for authorization" },
              headers: { 
                type: "object", 
                description: "Additional headers to include in the request",
                additionalProperties: { type: "string" }
              }
            },
            required: ["url", "value"],
          },
        },
        {
          name: "playwright_put",
          description: "Perform an HTTP PUT request",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to perform PUT operation" },
              value: { type: "string", description: "Data to PUT in the body" },
            },
            required: ["url", "value"],
          },
        },
        {
          name: "playwright_patch",
          description: "Perform an HTTP PATCH request",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to perform PUT operation" },
              value: { type: "string", description: "Data to PATCH in the body" },
            },
            required: ["url", "value"],
          },
        },
        {
          name: "playwright_delete",
          description: "Perform an HTTP DELETE request",
          inputSchema: {
            type: "object",
            properties: {
              url: { type: "string", description: "URL to perform DELETE operation" }
            },
            required: ["url"],
          },
        },
        {
          name: "playwright_expect_response",
          description: "Ask Playwright to start waiting for a HTTP response. This tool initiates the wait operation but does not wait for its completion.",
          inputSchema: {
            type: "object",
            properties: {
              id: { type: "string", description: "Unique & arbitrary identifier to be used for retrieving this response later with `Playwright_assert_response`." },
              url: { type: "string", description: "URL pattern to match in the response." }
            },
            required: ["id", "url"],
          },
        },
        {
          name: "playwright_assert_response",
          description: "Wait for and validate a previously initiated HTTP response wait operation.",
          inputSchema: {
            type: "object",
            properties: {
              id: { type: "string", description: "Identifier of the HTTP response initially expected using `Playwright_expect_response`." },
              value: { type: "string", description: "Data to expect in the body of the HTTP response. If provided, the assertion will fail if this value is not found in the response body." }
            },
            required: ["id"],
          },
        },
        {
          name: "playwright_custom_user_agent",
          description: "Set a custom User Agent for the browser",
          inputSchema: {
            type: "object",
            properties: {
              userAgent: { type: "string", description: "Custom User Agent for the Playwright browser instance" }
            },
            required: ["userAgent"],
          },
        },
        {
          name: "playwright_get_visible_text",
          description: "Get the visible text content of the current page",
          inputSchema: {
            type: "object",
            properties: {},
            required: [],
          },
        },
        {
          name: "playwright_get_visible_html",
          description: "Get the HTML content of the current page",
          inputSchema: {
            type: "object",
            properties: {},
            required: [],
          },
        },
        {
          name: "playwright_go_back",
          description: "Navigate back in browser history",
          inputSchema: {
            type: "object",
            properties: {},
            required: [],
          },
        },
        {
          name: "playwright_go_forward",
          description: "Navigate forward in browser history",
          inputSchema: {
            type: "object",
            properties: {},
            required: [],
          },
        },
        {
          name: "playwright_drag",
          description: "Drag an element to a target location",
          inputSchema: {
            type: "object",
            properties: {
              sourceSelector: { type: "string", description: "CSS selector for the element to drag" },
              targetSelector: { type: "string", description: "CSS selector for the target location" }
            },
            required: ["sourceSelector", "targetSelector"],
          },
        },
        {
          name: "playwright_press_key",
          description: "Press a keyboard key",
          inputSchema: {
            type: "object",
            properties: {
              key: { type: "string", description: "Key to press (e.g. 'Enter', 'ArrowDown', 'a')" },
              selector: { type: "string", description: "Optional CSS selector to focus before pressing key" }
            },
            required: ["key"],
          },
        },
        {
          name: "playwright_save_as_pdf",
          description: "Save the current page as a PDF file",
          inputSchema: {
            type: "object",
            properties: {
              outputPath: { type: "string", description: "Directory path where PDF will be saved" },
              filename: { type: "string", description: "Name of the PDF file (default: page.pdf)" },
              format: { type: "string", description: "Page format (e.g. 'A4', 'Letter')" },
              printBackground: { type: "boolean", description: "Whether to print background graphics" },
              margin: {
                type: "object",
                description: "Page margins",
                properties: {
                  top: { type: "string" },
                  right: { type: "string" },
                  bottom: { type: "string" },
                  left: { type: "string" }
                }
              }
            },
            required: ["outputPath"],
          },
        },
      ] as const satisfies Tool[];
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action but fails to mention critical traits such as execution context (e.g., page scope), error handling, security implications, or whether it returns a value. This is inadequate for a tool that executes arbitrary JavaScript.

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 zero wasted words. It is front-loaded and efficiently conveys the core purpose without unnecessary elaboration, 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?

Given the complexity of executing JavaScript in a browser (with no annotations or output schema), the description is insufficient. It lacks details on return values, error behavior, or execution scope, which are crucial for safe and effective use. This leaves significant gaps in understanding the tool's full context.

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 'script' parameter fully documented in the schema. The description adds no additional meaning beyond what the schema provides, such as examples or constraints on the JavaScript code. 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 ('Execute JavaScript') and the context ('in the browser console'), which is specific and unambiguous. However, it doesn't explicitly differentiate from sibling tools like 'playwright_console_logs' (which retrieves logs) or other JavaScript execution contexts, keeping it from a perfect score.

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. For example, it doesn't mention when to prefer this over other Playwright tools for interaction or how it relates to siblings like 'playwright_get_visible_html'. This lack of context leaves the agent without usage direction.

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

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