Skip to main content
Glama

execute_js

Execute JavaScript code directly within web pages to test scripts, manipulate DOM elements, extract data, and debug frontend functionality during browser development workflows.

Instructions

Executa código JavaScript arbitrário no contexto da página

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesCódigo JavaScript para executar
returnValueNoSe true, retorna o valor da execução

Implementation Reference

  • The core handler function for the 'execute_js' tool. It receives arguments, casts them to ExecuteJsArgs, evaluates the provided JavaScript code in the browser page context using Puppeteer, handles errors, and returns the result as a ToolResponse.
    export async function handleExecuteJs(args: unknown, currentPage: Page): Promise<ToolResponse> {
      const typedArgs = args as unknown as ExecuteJsArgs;
      const { code } = typedArgs;
    
      const result = await currentPage.evaluate((jsCode: string) => {
        try {
          const func = new Function(jsCode);
          return { success: true, result: func() };
        } catch (error) {
          const err = error as Error;
          return { success: false, error: err.message, stack: err.stack };
        }
      }, code);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2),
          },
        ],
      };
    }
  • src/tools.ts:65-83 (registration)
    Registration of the 'execute_js' tool in the tools array exported for MCP server, including name, description, and JSON input schema.
    {
      name: 'execute_js',
      description: 'Executa código JavaScript arbitrário no contexto da página',
      inputSchema: {
        type: 'object',
        properties: {
          code: {
            type: 'string',
            description: 'Código JavaScript para executar',
          },
          returnValue: {
            type: 'boolean',
            description: 'Se true, retorna o valor da execução',
            default: true,
          },
        },
        required: ['code'],
      },
    },
  • TypeScript interface defining the input parameters for the execute_js tool handler.
    export interface ExecuteJsArgs {
      code: string;
      returnValue?: boolean;
    }
  • Dispatch case in the main MCP server request handler that initializes the browser page and delegates to the specific handleExecuteJs function.
    case 'execute_js': {
      const currentPage = await initBrowser();
      return await handleExecuteJs(args, currentPage);
    }
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 mentions executing arbitrary JavaScript in the page context, which implies potential side effects like DOM manipulation or network requests, but fails to detail critical aspects such as security implications, error handling, execution timeouts, or impact on page state.

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 in Portuguese that directly states the tool's function without unnecessary words. It's front-loaded with the core action and context, making it highly concise and well-structured.

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 arbitrary JavaScript (a powerful and potentially risky operation), the description is insufficient. With no annotations and no output schema, it lacks details on return values, error formats, execution constraints, or safety considerations, leaving significant gaps for an AI 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%, so the schema already documents both parameters ('code' and 'returnValue') adequately. The description doesn't add any meaningful parameter details beyond what the schema provides, such as examples of JavaScript code or use cases for toggling returnValue, meeting the baseline for high 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 ('executa código JavaScript') and the context ('no contexto da página'), which is specific and unambiguous. However, it doesn't differentiate from sibling tools like 'evaluate_xpath' or 'query_selector_all' that also execute code-like operations, preventing 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 like 'evaluate_xpath' for XPath expressions or 'query_selector_all' for DOM queries. It lacks context about scenarios where arbitrary JavaScript execution is preferred over more specific tools.

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/EmmanuelBarbosaMonteiro/mcp-server-browser'

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