{"record":{"id":"85576034bc938b6b","repo":"danny-avila/LibreChat","slug":"code-execution-is-not-enabled-for-agents","errorCode":null,"errorMessage":"Code execution is not enabled for Agents","messagePattern":"Code execution is not enabled for Agents","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/server/services/Files/process.js","lineNumber":696,"sourceCode":"    throw new Error('No tool resource provided for agent file upload');\n  }\n\n  if (tool_resource === EToolResources.file_search && file.mimetype.startsWith('image')) {\n    throw new Error('Image uploads are not supported for file search tool resources');\n  }\n\n  if (!messageAttachment && !agent_id) {\n    throw new Error('No agent ID provided for agent file upload');\n  }\n\n  const isImage = file.mimetype.startsWith('image');\n  let fileInfoMetadata;\n  const entity_id = messageAttachment === true ? undefined : agent_id;\n  const basePath = mime.getType(file.originalname)?.startsWith('image') ? 'images' : 'uploads';\n  if (tool_resource === EToolResources.execute_code) {\n    const isCodeEnabled = await checkCapability(req, AgentCapabilities.execute_code);\n    if (!isCodeEnabled) {\n      throw new Error('Code execution is not enabled for Agents');\n    }\n    const { handleFileUpload: uploadCodeEnvFile } = getStrategyFunctions(FileSources.execute_code);\n    const stream = fs.createReadStream(file.path);\n    /* Resource identity for codeapi's sessionKey:\n     * - chat attachments (messageAttachment=true): `kind: 'user'`, codeapi\n     *   buckets under `<tenant>:user:<authContext.userId>` regardless of `id`.\n     * - agent setup files (messageAttachment=false): `kind: 'agent'`, shared\n     *   per agent identity. `id` carries the agent id. */\n    const codeKind = messageAttachment === true ? 'user' : 'agent';\n    const codeId = messageAttachment === true ? req.user.id : agent_id;\n    /* Upload under the same sanitized filename LC stores in its DB\n     * (`fileInfo.filename` below uses `sanitizeFilename(originalname)`).\n     * Codeapi/file_server use this as the on-disk name in the sandbox\n     * — `/mnt/data/<filename>` — and `primeFiles`'s `toolContext` text\n     * + `_injected_files.name` both reference `file.filename`. Sending\n     * the unsanitized `file.originalname` here makes the sandbox path\n     * (with spaces / special chars) drift from what LC tells the model\n     * is available, causing FileNotFoundError on the first reference. */","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Files/process.js#L678-L714","documentation":"Thrown when `tool_resource === EToolResources.execute_code` but `checkCapability(req, AgentCapabilities.execute_code)` returns false. The capability flag is set per-agent (or per-tenant) admin config; without it the code execution tool is not authorized for this agent, so its dedicated upload path (codeapi session file) is blocked before any I/O.","triggerScenarios":"An upload to an agent with `tool_resource: execute_code` where the agent (or its model/tenant) does not have the execute_code capability enabled. Also when a user tries to use a tool resource the admin has globally disabled.","commonSituations":"Admin disabled code execution globally but a stale agent still references the tool resource; user copying an agent config across environments where the capability is off; a new install where execute_code requires explicit opt-in.","solutions":["Enable the execute_code capability for the agent (admin settings / agent config).","If code execution should stay off, remove `execute_code` from the agent's tool_resources and stop uploading to it.","Frontend: hide/disable the execute_code upload UI when the capability is not enabled.","Check the tenant/role capability matrix if multi-tenant — the user's tenant may not have the feature."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"async function assertExecuteCodeEnabled(req) {\n  const ok = await checkCapability(req, AgentCapabilities.execute_code);\n  if (!ok) throw new Error('Enable execute_code capability for this agent');\n}","typeGuard":null,"tryCatchPattern":"try { await processAgentFileUpload(params); }\ncatch (e) {\n  if (/Code execution is not enabled/.test(e.message)) return res.status(403).json({ error: e.message });\n  throw e;\n}","preventionTips":["Gate the execute_code upload UI on the capability flag in the frontend.","Keep agent capability flags in sync across environments when copying configs.","Disable stale tool_resources on agents after turning a capability off globally."],"tags":["agent","capability","execute-code","authorization"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}