{"record":{"id":"49762e104e3d2584","repo":"mastra-ai/mastra","slug":"no-organization-selected-run-mastra-auth-orgs-sw-49762e","errorCode":null,"errorMessage":"No organization selected. Run: mastra auth orgs switch","messagePattern":"No organization selected\\. Run: mastra auth orgs switch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/env.ts","lineNumber":18,"sourceCode":"import { chmod, readFile, writeFile } from 'node:fs/promises';\nimport { resolve } from 'node:path';\n\nimport { getToken, getCurrentOrgId } from '../auth/credentials.js';\nimport { serializeEnvFile } from '../env/env-file.js';\nimport { loadProjectConfig } from '../studio/project-config.js';\nimport { parseEnvFile } from './deploy.js';\nimport { fetchServerProjects, getServerProjectEnv, updateServerProjectEnv } from './platform-api.js';\n\n/* ------------------------------------------------------------------ */\n/*  Shared helpers                                                     */\n/* ------------------------------------------------------------------ */\n\nexport async function resolveAuth(cliOrg?: string): Promise<{ token: string; orgId: string }> {\n  const token = await getToken();\n  const orgId = process.env.MASTRA_ORG_ID ?? cliOrg ?? (await getCurrentOrgId());\n  if (!orgId) {\n    throw new Error('No organization selected. Run: mastra auth orgs switch');\n  }\n  return { token, orgId };\n}\n\nexport async function resolveProjectId(\n  opts: { config?: string; project?: string },\n  auth?: { token: string; orgId: string },\n): Promise<string> {\n  const envProjectId = process.env.MASTRA_PROJECT_ID;\n  if (envProjectId) return envProjectId;\n\n  if (opts.project) {\n    if (auth) {\n      const projects = await fetchServerProjects(auth.token, auth.orgId);\n      const match = projects.find(p => p.slug === opts.project || p.id === opts.project);\n      if (match) return match.id;\n    }\n    return opts.project;","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/env.ts#L1-L36","documentation":"`mastra server env` commands require an organization id. resolveAuth builds it from MASTRA_ORG_ID, the CLI --org option, or the locally persisted current org (set by `mastra auth orgs switch`). If all three are absent it throws, because env APIs are org-scoped and there is no safe default.","triggerScenarios":"Running `mastra server env list|set|unset|import` after authenticating (token exists) but never switching to an org, with no MASTRA_ORG_ID env var and no --org flag; common in fresh installs or new machines where credentials were copied but org selection state wasn't.","commonSituations":"CI containers that only receive the auth token secret; developers who logged in on a new machine; scripts that pass --project but forget org context.","solutions":["Run `mastra auth orgs switch` and pick the organization","Set MASTRA_ORG_ID in the environment (CI secret or shell profile)","Pass --org <organization-id> to the env command"],"exampleFix":"// before\nmastra server env list\n// after\nmastra server env list --org org_123\n# or\nexport MASTRA_ORG_ID=org_123 && mastra server env list","handlingStrategy":"validation","validationCode":"// Preflight for env commands\nexport async function ensureOrgSelected(cliOrg?: string) {\n  if (process.env.MASTRA_ORG_ID || cliOrg) return;\n  // getCurrentOrgId() mirrors the CLI's persisted selection\n  const { getCurrentOrgId } = await import('../auth/credentials.js');\n  if (!(await getCurrentOrgId())) {\n    throw new Error('Run `mastra auth orgs switch` or set MASTRA_ORG_ID first.');\n  }\n}","typeGuard":"function hasOrgContext(env: NodeJS.ProcessEnv, cliOrg?: string): boolean {\n  return Boolean(env.MASTRA_ORG_ID || cliOrg);\n}","tryCatchPattern":"try {\n  await exec('mastra server env list');\n} catch (err) {\n  if (err instanceof Error && err.message.includes('No organization selected')) {\n    await exec('mastra auth orgs switch');\n    await exec('mastra server env list');\n  } else throw err;\n}","preventionTips":["Run `mastra auth orgs switch` once on every new machine/container","Set MASTRA_ORG_ID in shell profiles and CI secrets","Pass --org explicitly in scripts for reproducibility","Bake org selection into the CI image setup step"],"tags":["cli","auth","organization","env-vars"],"backgroundTag":"missing-org-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}