execute_function
Run custom JavaScript functions directly in a browser environment for automation tasks like web scraping, content extraction, or performance audits using Browserless MCP Server.
Instructions
Execute custom JavaScript function in browser context
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| context | No |
Implementation Reference
- src/client.ts:129-144 (handler)The core handler method that executes the tool logic by sending a POST request to Browserless /function endpoint with the provided code and context.async executeFunction(request: FunctionRequest): Promise<BrowserlessResponse<FunctionResponse>> { try { const response: AxiosResponse<FunctionResponse> = await this.httpClient.post('/function', request, { headers: { 'Content-Type': 'application/javascript', }, }); return { success: true, data: response.data, }; } catch (error) { return this.handleError(error); } }
- src/index.ts:370-389 (handler)MCP server handler case for 'execute_function' tool that calls the client executeFunction and formats the MCP response.case 'execute_function': { if (!args) throw new Error('Arguments are required'); const result = await this.client!.executeFunction(args as any); if (result.success && result.data) { return { content: [ { type: 'text', text: `Function executed successfully. Result type: ${result.data.type}`, }, { type: 'text', text: JSON.stringify(result.data.result, null, 2), }, ], }; } else { throw new Error(result.error || 'Failed to execute function'); } }
- src/index.ts:135-146 (registration)Registration of the 'execute_function' tool in the MCP server's tool list, including name, description, and input schema.{ name: 'execute_function', description: 'Execute custom JavaScript function in browser context', inputSchema: { type: 'object', properties: { code: { type: 'string' }, context: { type: 'object' }, }, required: ['code'], }, },
- src/types.ts:169-174 (schema)Zod schema definition for FunctionRequest type used in the executeFunction handler.export const FunctionRequestSchema = z.object({ code: z.string(), context: z.record(z.any()).optional(), }); export type FunctionRequest = z.infer<typeof FunctionRequestSchema>;
- src/types.ts:271-274 (schema)TypeScript interface for FunctionResponse returned by the executeFunction handler.export interface FunctionResponse { result: any; type: string; }