mastra-ai/mastra · error · ProviderAuthRequiredError
Kimi For Coding credentials are invalid. Please reconnect th
Error message
Kimi For Coding credentials are invalid. Please reconnect the account.
What it means
After finding an oauth credential for 'kimi-for-coding', buildKimiCodingOAuthFetch validates the stored deviceId with isKimiCodingDeviceId. The deviceId is required to build the device headers the Kimi API expects. If the deviceId is missing, malformed, or fails validation, it throws ProviderAuthRequiredError telling the user to reconnect the account.
Source
Thrown at mastracode/sdk/src/providers/kimi-coding.ts:35
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;
}
export function buildKimiCodingApiKeyFetch(apiKey: string): typeof fetch {
return (async (input: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {
const headers = new Headers(input instanceof Request ? input.headers : undefined);View on GitHub (pinned to 75dd419e61)
Solutions
- Re-run the Kimi For Coding OAuth login to rewrite the credential with a valid deviceId.
- Delete the corrupt 'kimi-for-coding' credential entry and log in again from scratch.
- Inspect the stored credential JSON to confirm deviceId is present and correctly formatted.
- If migrating SDK versions, clear old credentials and reconnect rather than carrying them over.
Example fix
// before (corrupt stored credential)
{ "id": "kimi-for-coding", "type": "oauth" } // missing deviceId
// after: reconnect
await kimiForCodingLogin();
// stores { "id": "kimi-for-coding", "type": "oauth", "deviceId": "<valid-id>", ... } Defensive patterns
Strategy: validation
Validate before calling
import { isKimiCodingDeviceId } from '@mastra/code-sdk';
const cred = store.get('kimi-for-coding');
if (cred?.type === 'oauth' && !isKimiCodingDeviceId(cred.deviceId)) {
await kimiForCodingLogin(); // credential is corrupt; reconnect
} Type guard
function hasValidKimiCred(c: unknown): c is { type: 'oauth'; deviceId: string } {
return !!c && typeof c === 'object' && (c as any).type === 'oauth' &&
typeof (c as any).deviceId === 'string' && (c as any).deviceId.length > 0;
} Try / catch
try {
return await fetchWithAuth(input, init);
} catch (e) {
if (e instanceof ProviderAuthRequiredError && /reconnect/.test(e.message)) {
await kimiForCodingLogin(); // full reconnect rewrites the credential
return await fetchWithAuth(input, init);
}
throw e;
} Prevention
- Never hand-edit the credentials file; use the SDK login flow.
- Validate stored credentials after version upgrades or migrations.
- Delete and re-create the credential if its shape looks unexpected.
When it happens
Trigger: A request through the Kimi For Coding OAuth fetch where store.get('kimi-for-coding') returns an oauth credential whose deviceId is undefined/null or does not match the expected device-id format (isKimiCodingDeviceId returns false).
Common situations: Partially written or hand-edited credentials file; a schema change in an SDK version that altered the stored credential shape; credentials synced from another tool that omit the deviceId; corrupted storage.
Related errors
- Not logged in to Kimi For Coding.
- Failed to refresh the Kimi For Coding token.
- State token has expired
- Redirect URI is required for SSO login
- Token exchange failed: ${error}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/da58c9253523599d.
Report an issue: GitHub.