Skip to main content
Glama
code-executor.ts1.41 kB
const TIMEOUT_MS = 120000; const MAX_RESULT_SIZE = 100000; export interface ExecutionResult { success: boolean; data?: any; error?: string; } export class CodeExecutor { async execute(code: string, payload: any): Promise<ExecutionResult> { const wrappedCode = ` (async function() { const payload = ${JSON.stringify(payload)}; ${code} })() `; try { // biome-ignore lint/security/noGlobalEval: intentionally executing LLM-generated code for evaluation const evalResult = eval(wrappedCode); const timeoutPromise = new Promise<never>((_, reject) => { setTimeout(() => reject(new Error(`Code execution timed out after ${TIMEOUT_MS / 1000} seconds`)), TIMEOUT_MS); }); const result = await Promise.race([ evalResult, timeoutPromise ]); return { success: true, data: this.truncateResult(result) }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } private truncateResult(result: any): any { const resultStr = JSON.stringify(result); if (resultStr.length > MAX_RESULT_SIZE) { return { truncated: true, preview: resultStr.substring(0, 1000), message: 'Result truncated due to size' }; } return result; } }

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/superglue-ai/superglue'

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