Hmbown/CodeWhale · error · anyhow

stored MCP OAuth token for

Error message

stored MCP OAuth token for '{server_name}' is not valid credential JSON; contents were omitted

What it means

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.

Solutions

  1. Delete the corrupted stored credential for the server and re-run the OAuth flow to obtain fresh tokens
  2. Check the file for truncation/corruption and restore from a backup if available
  3. If a version upgrade changed the schema, re-authenticate once to write the new format

Example fix

// before: hand-edited / corrupted stored credential
{"server_name": "myserver", "url": "https://...", "client_id":
// after: remove it and re-authenticate so a complete record is written
{"server_name": "myserver", "url": "https://...", "client_id": "abc", "expires_at": 1735689600000, "token_response": {"access_token": "..."}}
Defensive patterns

Strategy: try-catch

Validate before calling

if let Ok(value) = serde_json::from_str::<serde_json::Value>(&stored) {
    let has_fields = value.get("server_name").is_some() && value.get("token_response").is_some();
    // only attempt to load when the JSON parses and has the expected fields
}

Try / catch

match load_oauth_tokens(server) {
    Err(e) if e.to_string().contains("not valid credential JSON") => {
        // delete the corrupt record and start a fresh OAuth flow
        reauthenticate(server)?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/357292ad7dd5d271. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/mcp/oauth.rs:1454

    server_name: &str,
    url: &str,
) -> Result<Option<StoredMcpOAuthTokens>> {
    let secrets = codewhale_secrets::Secrets::auto_detect();
    let key = store_key(server_name, url);
    let Some(serialized) = secrets
        .get(&key)
        .with_context(|| format!("reading MCP OAuth token for '{server_name}'"))?
    else {
        return Ok(None);
    };
    let mut tokens = parse_stored_oauth_tokens(&serialized, server_name)?;
    refresh_expires_in_from_timestamp(&mut tokens);
    Ok(Some(tokens))
}

fn parse_stored_oauth_tokens(serialized: &str, server_name: &str) -> Result<StoredMcpOAuthTokens> {
    serde_json::from_str(serialized).map_err(|_| {
        anyhow!(
            "stored MCP OAuth token for '{server_name}' is not valid credential JSON; contents were omitted"
        )
    })
}

pub(crate) fn save_oauth_tokens(tokens: &StoredMcpOAuthTokens) -> Result<()> {
    let secrets = codewhale_secrets::Secrets::auto_detect();
    let key = store_key(&tokens.server_name, &tokens.url);
    let serialized = serde_json::to_string(tokens).context("serializing MCP OAuth token")?;
    secrets
        .set(&key, &serialized)
        .with_context(|| format!("saving MCP OAuth token for '{}'", tokens.server_name))
}

fn delete_oauth_tokens(server_name: &str, url: &str) -> Result<bool> {
    let secrets = codewhale_secrets::Secrets::auto_detect();
    let key = store_key(server_name, url);
    let existed = secrets

View on GitHub (pinned to 73e0f67d83)