mastra-ai/mastra · error · Error

Refresh failed

Error message

Refresh failed

What it means

In the same authenticatedFetch refresh path, tryRefreshToken(creds) returned a falsy value, so the CLI throws 'Refresh failed'. The refresh token itself was rejected or unusable — the stored session cannot be extended and the caller must re-authenticate interactively.

Source

Thrown at packages/cli/src/commands/auth/client.ts:140

  }

  const response = await fetch(input, init);

  if (response.status !== 401 || !_currentToken) {
    return response;
  }

  // Avoid multiple concurrent refreshes
  if (!_refreshInFlight) {
    _refreshInFlight = (async () => {
      try {
        // Dynamic import to avoid circular dependency
        const { tryRefreshToken, loadCredentials } = await import('./credentials.js');
        const creds = await loadCredentials();
        if (!creds) throw new Error('No credentials');

        const newToken = await tryRefreshToken(creds);
        if (!newToken) throw new Error('Refresh failed');

        _currentToken = newToken;
        return newToken;
      } finally {
        _refreshInFlight = null;
      }
    })();
  }

  let newToken: string;
  try {
    newToken = await _refreshInFlight;
  } catch {
    // Refresh failed — return the original 401 response
    return response;
  }

  // Retry with the refreshed token.

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Run `mastra auth login` to start a fresh session (the refresh token is no longer usable).
  2. Confirm network connectivity to the Mastra auth endpoint and retry.
  3. If it recurs immediately, delete stored credentials and log in again.
  4. Check for version updates to the CLI in case the auth flow changed.

Example fix

// before
mastra auth tokens list  # -> Error: Refresh failed
// after
mastra auth login
mastra auth tokens list
Defensive patterns

Strategy: try-catch

Validate before calling

// verify session is refreshable before dependent work
const { loadCredentials } = await import('./credentials.js');
const { tryRefreshToken } = await import('./credentials.js');
const creds = await loadCredentials();
if (creds && !(await tryRefreshToken(creds))) {
  throw new Error('Refresh token rejected — run `mastra auth login`');
}

Type guard

function isRefreshFailure(err: unknown): boolean {
  return err instanceof Error && err.message === 'Refresh failed';
}

Try / catch

try {
  return await platformFetch(url, init);
} catch (err) {
  if (isRefreshFailure(err)) {
    console.error('Cannot refresh session. Run `mastra auth login` and retry.');
    process.exitCode = 1;
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: authenticatedFetch's single-flight refresh invokes tryRefreshToken and it resolves null/undefined (refresh endpoint rejected the refresh token, returned an error, or network call failed silently), triggering `throw new Error('Refresh failed')`.

Common situations: Refresh token expired or revoked server-side; user logged out elsewhere invalidating the session; offline/flaky network during refresh; platform auth service rejecting an old refresh-token format after an API change.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/2dd81ad71e59fe76. Report an issue: GitHub.