{"record":{"id":"357292ad7dd5d271","repo":"Hmbown/CodeWhale","slug":"stored-mcp-oauth-token-for-server-name-is-not","errorCode":null,"errorMessage":"stored MCP OAuth token for '{server_name}' is not valid credential JSON; contents were omitted","messagePattern":"stored MCP OAuth token for '(.+?)' is not valid credential JSON; contents were omitted","errorType":"validation","errorClass":"anyhow","httpStatus":null,"severity":"error","filePath":"crates/tui/src/mcp/oauth.rs","lineNumber":1454,"sourceCode":"    server_name: &str,\n    url: &str,\n) -> Result<Option<StoredMcpOAuthTokens>> {\n    let secrets = codewhale_secrets::Secrets::auto_detect();\n    let key = store_key(server_name, url);\n    let Some(serialized) = secrets\n        .get(&key)\n        .with_context(|| format!(\"reading MCP OAuth token for '{server_name}'\"))?\n    else {\n        return Ok(None);\n    };\n    let mut tokens = parse_stored_oauth_tokens(&serialized, server_name)?;\n    refresh_expires_in_from_timestamp(&mut tokens);\n    Ok(Some(tokens))\n}\n\nfn parse_stored_oauth_tokens(serialized: &str, server_name: &str) -> Result<StoredMcpOAuthTokens> {\n    serde_json::from_str(serialized).map_err(|_| {\n        anyhow!(\n            \"stored MCP OAuth token for '{server_name}' is not valid credential JSON; contents were omitted\"\n        )\n    })\n}\n\npub(crate) fn save_oauth_tokens(tokens: &StoredMcpOAuthTokens) -> Result<()> {\n    let secrets = codewhale_secrets::Secrets::auto_detect();\n    let key = store_key(&tokens.server_name, &tokens.url);\n    let serialized = serde_json::to_string(tokens).context(\"serializing MCP OAuth token\")?;\n    secrets\n        .set(&key, &serialized)\n        .with_context(|| format!(\"saving MCP OAuth token for '{}'\", tokens.server_name))\n}\n\nfn delete_oauth_tokens(server_name: &str, url: &str) -> Result<bool> {\n    let secrets = codewhale_secrets::Secrets::auto_detect();\n    let key = store_key(server_name, url);\n    let existed = secrets","sourceCodeStart":1436,"sourceCodeEnd":1472,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/mcp/oauth.rs#L1436-L1472","documentation":"Stored MCP OAuth tokens are persisted as JSON (StoredMcpOAuthTokens) and deserialized with serde_json::from_str when read back. If the stored string is not valid credential JSON — corrupted file, empty content, truncated write, or an incompatible older format — the parse fails and this error is raised, deliberately omitting the stored contents from the message so secrets are never leaked into logs.","triggerScenarios":"Calling parse_stored_oauth_tokens (via the token-load path) on a persisted token file/string that serde cannot deserialize into StoredMcpOAuthTokens: hand-edited token store, interrupted write, schema change between versions, or non-JSON garbage in the credential file.","commonSituations":"User manually edited or truncated the stored credential file; upgrade/downgrade between app versions changed the StoredMcpOAuthTokens schema; disk-full during save left a partial JSON file; a different tool overwrote the credential store.","solutions":["Delete the corrupted stored credential for the server and re-run the OAuth flow to obtain fresh tokens","Check the file for truncation/corruption and restore from a backup if available","If a version upgrade changed the schema, re-authenticate once to write the new format"],"exampleFix":"// before: hand-edited / corrupted stored credential\n{\"server_name\": \"myserver\", \"url\": \"https://...\", \"client_id\":\n// after: remove it and re-authenticate so a complete record is written\n{\"server_name\": \"myserver\", \"url\": \"https://...\", \"client_id\": \"abc\", \"expires_at\": 1735689600000, \"token_response\": {\"access_token\": \"...\"}}","handlingStrategy":"try-catch","validationCode":"if let Ok(value) = serde_json::from_str::<serde_json::Value>(&stored) {\n    let has_fields = value.get(\"server_name\").is_some() && value.get(\"token_response\").is_some();\n    // only attempt to load when the JSON parses and has the expected fields\n}","typeGuard":null,"tryCatchPattern":"match load_oauth_tokens(server) {\n    Err(e) if e.to_string().contains(\"not valid credential JSON\") => {\n        // delete the corrupt record and start a fresh OAuth flow\n        reauthenticate(server)?;\n    }\n    other => other?,\n}","preventionTips":["Never hand-edit stored credential files","Write token files atomically (temp file + rename) so crashes cannot truncate them","Treat schema changes to StoredMcpOAuthTokens as migrations; re-authenticate rather than loading stale formats"],"tags":["oauth","json","deserialization","mcp"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}