{"record":{"id":"ff020ba1e0190d28","repo":"mastra-ai/mastra","slug":"agent-with-id-storedagentid-not-found","errorCode":null,"errorMessage":"Agent with id ${storedAgentId} not found","messagePattern":"Agent with id (.+?) not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"packages/server/src/server/handlers/stored-agents.ts","lineNumber":394,"sourceCode":"  body: Record<string, unknown>;\n}) {\n  const storage = mastra.getStorage();\n  const agentsStore = storage ? await storage.getStore('agents') : undefined;\n  const storedAgent = await agentsStore?.getByIdResolved(storedAgentId, { status: 'draft' });\n  if (storedAgent) {\n    assertStoredResourceScope(storedAgent, await getStoredResourceScope(mastra, requestContext));\n    assertReadAccess({ requestContext, resource: 'stored-agents', resourceId: storedAgentId, record: storedAgent });\n  }\n\n  let codeAgent: { __getEditorConfig?: () => unknown; source?: string } | undefined;\n  try {\n    codeAgent = mastra.getAgentById?.(storedAgentId) as typeof codeAgent;\n  } catch {\n    codeAgent = undefined;\n  }\n\n  if (!storedAgent && !codeAgent) {\n    throw new HTTPException(404, { message: `Agent with id ${storedAgentId} not found` });\n  }\n\n  const config = buildExportConfig(body, codeAgent);\n  const content = `${JSON.stringify(config, null, 2)}\\n`;\n\n  return {\n    agentId: storedAgentId,\n    fileName: agentExportFilename(storedAgentId),\n    content,\n    config,\n  };\n}\n\nexport const EXPORT_STORED_AGENT_ROUTE = createRoute({\n  method: 'POST',\n  path: '/stored/agents/:storedAgentId/export',\n  responseType: 'json',\n  pathParamSchema: storedAgentIdPathParams,","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/stored-agents.ts#L376-L412","documentation":"buildStoredAgentExport (used by POST /stored/agents/:storedAgentId/export and the change-request route) looks up the agent both in the agents store and via mastra.getAgentById (code-defined agents). If neither yields a result, it throws HTTP 404 — export needs either a stored override or a code-defined agent to serialize.","triggerScenarios":"POST /stored/agents/:id/export or POST /stored/agents/:id/change-request with an id that exists in neither storage nor the registered code agents (typo, agent deleted, server running older code without the agent, wrong environment).","commonSituations":"Exporting from a server whose code was updated and the agent was removed; ID from a different deployment; agent defined only in a branch not running on the server; case-sensitivity mismatch in the agent id.","solutions":["Confirm the id with GET /stored/agents/:id or by listing agents before exporting","Ensure the code-defined agent is registered in the Mastra instance running on the server","Point the client at the correct environment/deployment that contains the agent","Fix the id typo/casing in the request URL"],"exampleFix":"// before\npost(`/stored/agents/${'My-Agent'}/export`, body); // wrong casing\n// after\npost(`/stored/agents/${'my-agent'}/export`, body);","handlingStrategy":"validation","validationCode":"const stored = await fetch(`${base}/stored/agents/${id}`).then(r => r.ok);\nif (!stored) throw new Error(`Cannot export: agent ${id} not found in storage or code`);","typeGuard":"function isExportNotFound(e: unknown): e is { status: number; message: string } {\n  return typeof e === 'object' && e !== null && (e as any).status === 404 && /not found/.test((e as any).message ?? '');\n}","tryCatchPattern":"try {\n  return await post(`/stored/agents/${id}/export`, body);\n} catch (e) {\n  if (isExportNotFound(e)) {\n    console.error(`Agent ${id} not found in storage or registered code agents`);\n    return null;\n  }\n  throw e;\n}","preventionTips":["Resolve ids from the list endpoint instead of hardcoding","Keep code-defined agent registrations in sync across environments","Verify ids after refactors that rename/remove agents","Check agent existence in the target deployment before export"],"tags":["http-404","stored-agents","export"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}