Skip to main content
Glama

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
NameRequiredDescriptionDefault
codeYes
contextNo

Implementation Reference

  • 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); } }
  • 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'], }, },
  • 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>;
  • TypeScript interface for FunctionResponse returned by the executeFunction handler.
    export interface FunctionResponse { result: any; type: string; }

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/Lizzard-Solutions/browserless-mcp'

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