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
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Código JavaScript para executar | |
| returnValue | No | Se 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
- src/browserTools.ts:188-210 (handler)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'], }, },
- src/types.ts:44-47 (schema)TypeScript interface defining the input parameters for the execute_js tool handler.export interface ExecuteJsArgs { code: string; returnValue?: boolean; }
- src/index.ts:79-82 (handler)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); }