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

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

Input Schema (JSON Schema)

{ "properties": { "code": { "description": "Código JavaScript para executar", "type": "string" }, "returnValue": { "default": true, "description": "Se true, retorna o valor da execução", "type": "boolean" } }, "required": [ "code" ], "type": "object" }

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); }

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