mastra-ai/mastra · error
No organization selected. Run: mastra auth orgs switch
Error message
No organization selected. Run: mastra auth orgs switch
What it means
`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.
Source
Thrown at packages/cli/src/commands/server/env.ts:18
import { chmod, readFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { getToken, getCurrentOrgId } from '../auth/credentials.js';
import { serializeEnvFile } from '../env/env-file.js';
import { loadProjectConfig } from '../studio/project-config.js';
import { parseEnvFile } from './deploy.js';
import { fetchServerProjects, getServerProjectEnv, updateServerProjectEnv } from './platform-api.js';
/* ------------------------------------------------------------------ */
/* Shared helpers */
/* ------------------------------------------------------------------ */
export async function resolveAuth(cliOrg?: string): Promise<{ token: string; orgId: string }> {
const token = await getToken();
const orgId = process.env.MASTRA_ORG_ID ?? cliOrg ?? (await getCurrentOrgId());
if (!orgId) {
throw new Error('No organization selected. Run: mastra auth orgs switch');
}
return { token, orgId };
}
export async function resolveProjectId(
opts: { config?: string; project?: string },
auth?: { token: string; orgId: string },
): Promise<string> {
const envProjectId = process.env.MASTRA_PROJECT_ID;
if (envProjectId) return envProjectId;
if (opts.project) {
if (auth) {
const projects = await fetchServerProjects(auth.token, auth.orgId);
const match = projects.find(p => p.slug === opts.project || p.id === opts.project);
if (match) return match.id;
}
return opts.project;View on GitHub (pinned to 75dd419e61)
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
Example fix
// before mastra server env list // after mastra server env list --org org_123 # or export MASTRA_ORG_ID=org_123 && mastra server env list
Defensive patterns
Strategy: validation
Validate before calling
// Preflight for env commands
export async function ensureOrgSelected(cliOrg?: string) {
if (process.env.MASTRA_ORG_ID || cliOrg) return;
// getCurrentOrgId() mirrors the CLI's persisted selection
const { getCurrentOrgId } = await import('../auth/credentials.js');
if (!(await getCurrentOrgId())) {
throw new Error('Run `mastra auth orgs switch` or set MASTRA_ORG_ID first.');
}
} Type guard
function hasOrgContext(env: NodeJS.ProcessEnv, cliOrg?: string): boolean {
return Boolean(env.MASTRA_ORG_ID || cliOrg);
} Try / catch
try {
await exec('mastra server env list');
} catch (err) {
if (err instanceof Error && err.message.includes('No organization selected')) {
await exec('mastra auth orgs switch');
await exec('mastra server env list');
} else throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- No access to organization ${orgId}. Run: mastra auth orgs
- No organizations found. Create one at ${MASTRA_STUDIO_URL}
- You have no organizations. Please create one at ${MASTRA_STU
- No organization matched --org "${value}". Available: ${avail
- Session expired. Run: mastra auth login
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/49762e104e3d2584.
Report an issue: GitHub.