execute
Run Babashka (bb) code with configurable timeouts and integrated script management. Designed for efficient execution and control of scripting workflows using the Model Context Protocol.
Instructions
Execute Babashka (bb) code
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Babashka code to execute | |
| timeout | No | Timeout in milliseconds (default: 30000) |
Implementation Reference
- src/index.ts:135-199 (handler)Handler for CallToolRequestSchema that implements the 'execute' tool: validates tool name and arguments, executes Babashka code via child_process.execAsync, handles errors, caches recent results.this.server.setRequestHandler( CallToolRequestSchema, async (request) => { if (request.params.name !== "execute") { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } if (!isValidBabashkaCommandArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, "Invalid Babashka command arguments" ); } try { const timeout = request.params.arguments.timeout || CONFIG.DEFAULT_TIMEOUT; const { stdout, stderr } = await execAsync( `${CONFIG.BABASHKA_PATH} -e "${request.params.arguments.code.replace(/"/g, '\\"')}"`, { timeout } ); const result: BabashkaCommandResult = { stdout: stdout.trim(), stderr: stderr.trim(), exitCode: 0 }; // Cache the command result this.recentCommands.unshift({ code: request.params.arguments.code, result, timestamp: new Date().toISOString() }); // Keep only recent commands if (this.recentCommands.length > CONFIG.MAX_CACHED_COMMANDS) { this.recentCommands.pop(); } return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } catch (error: any) { const result: BabashkaCommandResult = { stdout: "", stderr: error.message || "Unknown error", exitCode: error.code || 1 }; return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], isError: true }; } } );
- src/index.ts:108-132 (registration)Registers the 'execute' tool metadata and input schema in the ListToolsRequestSchema handler.this.server.setRequestHandler( ListToolsRequestSchema, async () => ({ tools: [{ name: "execute", description: "Execute Babashka (bb) code", inputSchema: { type: "object", properties: { code: { type: "string", description: "Babashka code to execute" }, timeout: { type: "number", description: "Timeout in milliseconds (default: 30000)", minimum: 1000, maximum: 300000 } }, required: ["code"] } }] }) );
- src/index.ts:114-129 (schema)JSON Schema defining the input for the 'execute' tool: required 'code' string and optional 'timeout' number.inputSchema: { type: "object", properties: { code: { type: "string", description: "Babashka code to execute" }, timeout: { type: "number", description: "Timeout in milliseconds (default: 30000)", minimum: 1000, maximum: 300000 } }, required: ["code"] }
- src/types.ts:18-24 (helper)Helper function to validate Babashka command arguments used in the 'execute' handler.export const isValidBabashkaCommandArgs = ( args: any ): args is BabashkaCommandArgs => typeof args === "object" && args !== null && typeof args.code === "string" && (args.timeout === undefined || typeof args.timeout === "number");
- src/types.ts:27-31 (helper)Configuration constants used in the 'execute' handler: default timeout, max cached commands, Babashka executable path.export const CONFIG = { DEFAULT_TIMEOUT: 30000, // 30 seconds MAX_CACHED_COMMANDS: 10, BABASHKA_PATH: process.env.BABASHKA_PATH || "bb" // Allow custom bb path } as const;