Skip to main content
Glama
execute.ts1.88 kB
import { spawn } from 'child_process'; import { CONFIG } from '../config.js'; import { CodeExecuteInput, ToolResponse } from '../validation.js'; export async function executeCode(input: CodeExecuteInput): Promise<ToolResponse> { const { cmd, args = [], cwd = process.cwd(), timeoutMs = 15000 } = input; // Security check: only allow whitelisted executables if (!CONFIG.ALLOWED_EXECUTABLES.includes(cmd)) { return { content: [{ type: 'text' as const, text: `Error: Command '${cmd}' is not in the allowed executables list. Allowed: ${CONFIG.ALLOWED_EXECUTABLES.join(', ')}` }] }; } return new Promise((resolve) => { const startTime = Date.now(); let stdout = ''; let stderr = ''; let timedOut = false; const child = spawn(cmd, args, { cwd, shell: false, stdio: ['pipe', 'pipe', 'pipe'] }); const timeout = setTimeout(() => { timedOut = true; child.kill('SIGTERM'); setTimeout(() => child.kill('SIGKILL'), 1000); }, timeoutMs); child.stdout?.on('data', (data) => { stdout += data.toString(); }); child.stderr?.on('data', (data) => { stderr += data.toString(); }); child.on('close', (exitCode) => { clearTimeout(timeout); const durationMs = Date.now() - startTime; const result = { exitCode: timedOut ? null : exitCode, stdout: stdout.trim(), stderr: stderr.trim(), durationMs, timedOut }; resolve({ content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }] }); }); child.on('error', (error) => { clearTimeout(timeout); resolve({ content: [{ type: 'text' as const, text: `Error executing command: ${error.message}` }] }); }); }); }

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/bermingham85/mcp-puppet-pipeline'

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