Skip to main content
Glama

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
NameRequiredDescriptionDefault
codeYesBabashka code to execute
timeoutNoTimeout in milliseconds (default: 30000)

Implementation Reference

  • 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"] } }] }) );
  • 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"] }
  • 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");
  • 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;

Other Tools

Related Tools

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/bmorphism/babashka-mcp-server'

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