Skip to main content
Glama

browser_execute_script

Execute JavaScript code directly in a web browser to automate interactions, manipulate page elements, or retrieve dynamic content during web automation tasks.

Instructions

Execute JavaScript in the context of the current page

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • Handler function for the browser_execute_script tool. It uses StateManager to get the WebDriver, instantiates ActionService, and calls its executeScript method with the provided script.
    async ({ script }) => {
      try {
        const driver = stateManager.getDriver();
        const actionService = new ActionService(driver);
        const result = await actionService.executeScript(script);
        return {
          content: [{ type: 'text', text: `Script executed successfully: ${result}` }],
        };
      } catch (e) {
        return {
          content: [
            {
              type: 'text',
              text: `Error executing script: ${(e as Error).message}`,
            },
          ],
        };
      }
    }
  • Input schema for browser_execute_script tool defining the 'script' parameter as a string.
    {
      script: z.string().describe('JavaScript code to execute'),
    },
  • Registration of the browser_execute_script tool via server.tool() call within registerActionTools function.
    server.tool(
      'browser_execute_script',
      'Execute JavaScript in the context of the current page',
      {
        script: z.string().describe('JavaScript code to execute'),
      },
      async ({ script }) => {
        try {
          const driver = stateManager.getDriver();
          const actionService = new ActionService(driver);
          const result = await actionService.executeScript(script);
          return {
            content: [{ type: 'text', text: `Script executed successfully: ${result}` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error executing script: ${(e as Error).message}`,
              },
            ],
          };
        }
      }
    );
  • Helper method in ActionService class that executes the JavaScript script using the Selenium WebDriver's executeScript method.
    async executeScript(script: string, args = []): Promise<any> {
      return this.driver.executeScript(script, ...args);
    }
Behavior2/5

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

No annotations are provided, so the description carries full burden. It states the action but lacks critical behavioral details: it doesn't specify if this is a read-only or destructive operation, what permissions are needed, how errors are handled, or what the return value might be. For a JavaScript execution tool with zero annotation coverage, this is a significant gap in transparency.

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's front-loaded with the core action and context, making it highly efficient and easy to parse. Every word earns its place without redundancy.

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 context, no annotations, and no output schema, the description is incomplete. It doesn't cover behavioral aspects like safety, return values, or error handling. For a tool that could have significant side effects, this leaves too many gaps for an agent to use it effectively.

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% for the single parameter 'script', which is well-documented in the schema as 'JavaScript code to execute'. The description adds no additional semantic context beyond what the schema provides, such as examples or constraints on the script content. With high schema coverage, a baseline score of 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 ('Execute JavaScript') and the context ('in the context of the current page'), making the purpose immediately understandable. It doesn't explicitly differentiate from sibling tools like browser_navigate or browser_click, but the specificity of executing JavaScript distinguishes it well enough from most other browser actions.

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 an open browser page), exclusions, or related tools for other JavaScript-related tasks. The agent must infer usage from the purpose alone.

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/pshivapr/selenium-mcp'

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