mastra-ai/mastra · error · ProviderAuthRequiredError

Not logged in to Kimi For Coding.

Error message

Not logged in to Kimi For Coding.

What it means

buildKimiCodingOAuthFetch wraps fetch so every request to the Kimi For Coding API carries OAuth credentials plus device headers. Before each request it reloads the credential store and requires an entry with provider id 'kimi-for-coding' of type 'oauth'. If none exists (or it is an api-key credential), it throws ProviderAuthRequiredError('Not logged in to Kimi For Coding.').

Source

Thrown at mastracode/sdk/src/providers/kimi-coding.ts:32

let authStorageInstance: AuthStorage | null = null;

export function setAuthStorage(storage: AuthStorage | undefined): void {
  authStorageInstance = storage ?? null;
}

function getAuthStorage(): AuthStorage {
  if (!authStorageInstance) authStorageInstance = new AuthStorage();
  return authStorageInstance;
}

export function buildKimiCodingOAuthFetch(options: { credentialStore?: CredentialStore } = {}): typeof fetch {
  return (async (input: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {
    const store = options.credentialStore ?? getAuthStorage();
    store.reload();
    const credential = store.get(PROVIDER_ID);
    if (!credential || credential.type !== 'oauth') {
      throw new ProviderAuthRequiredError('Not logged in to Kimi For Coding.');
    }
    if (!isKimiCodingDeviceId(credential.deviceId)) {
      throw new ProviderAuthRequiredError('Kimi For Coding credentials are invalid. Please reconnect the account.');
    }
    const deviceHeaders = getKimiCodingDeviceHeaders(credential.deviceId);
    const token = await store.getApiKey(PROVIDER_ID);
    if (!token) throw new ProviderAuthRequiredError('Failed to refresh the Kimi For Coding token.');

    const headers = new Headers(input instanceof Request ? input.headers : undefined);
    if (init?.headers) new Headers(init.headers).forEach((value, key) => headers.set(key, value));
    for (const [key, value] of Object.entries(deviceHeaders)) headers.set(key, value);
    headers.delete('authorization');
    headers.delete('x-api-key');
    headers.set('Authorization', `Bearer ${token}`);
    return fetch(input, { ...init, headers });
  }) as typeof fetch;
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Complete the Kimi For Coding OAuth login flow to store the 'kimi-for-coding' oauth credential.
  2. Check the credentials store for a 'kimi-for-coding' entry and confirm its type is 'oauth'.
  3. If you intend API-key auth instead, use buildKimiCodingApiKeyFetch / pass an apiKey rather than the OAuth fetch wrapper.
  4. Verify the credentialStore passed in options is the one the login flow wrote to.

Example fix

// before
const fetchWithAuth = buildKimiCodingOAuthFetch(); // throws if never logged in
// after
if (!credentialStore.get('kimi-for-coding')) {
  await kimiForCodingLogin(); // device OAuth flow
}
const fetchWithAuth = buildKimiCodingOAuthFetch({ credentialStore });
Defensive patterns

Strategy: validation

Validate before calling

const cred = store.get('kimi-for-coding');
if (!cred || cred.type !== 'oauth') {
  throw new Error('Kimi For Coding login required before making requests');
}

Type guard

function isKimiOAuthCred(c: unknown): c is { type: 'oauth'; deviceId: string } {
  return !!c && typeof c === 'object' && (c as any).type === 'oauth' && typeof (c as any).deviceId === 'string';
}

Try / catch

try {
  return await fetchWithAuth(input, init);
} catch (e) {
  if (e instanceof ProviderAuthRequiredError && /Not logged in/.test(e.message)) {
    await kimiForCodingLogin(); // interactive re-auth
    return await fetchWithAuth(input, init);
  }
  throw e;
}

Prevention

When it happens

Trigger: Any API call routed through the Kimi For Coding OAuth fetch wrapper when the credential store has no 'kimi-for-coding' entry, or the entry exists but its type is not 'oauth' (e.g. it is an api-key credential or null after reload).

Common situations: User never completed the Kimi For Coding device-OAuth login; credentials were logged out or cleared; a user switched from API-key auth to OAuth (or vice versa) so the credential type mismatches; pointing the SDK at a fresh/empty credential store in tests or CI.

Related errors


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