Hmbown/CodeWhale · error · anyhow::Error

Failed to render MCP template JSON

Error message

Failed to render MCP template JSON: {e}

What it means

Raised by mcp_template_json when serde_json::to_string_pretty fails to serialize the generated MCP configuration template. Serialization of plain config structs should never fail in practice, so this usually indicates an internal problem (e.g. a non-string map key or NaN-like value introduced into the template) rather than user input.

Solutions

  1. Report/inspect the newly added template field that breaks serde_json serialization
  2. Ensure all template map keys are strings and no non-finite floats are present
  3. Update or rebuild the binary so template code and serde derives match

Example fix

// before
let servers: HashMap<ServerId, ServerConfig> = ...;
// after
let servers: BTreeMap<String, ServerConfig> = ...;
Defensive patterns

Strategy: try-catch

Validate before calling

// template is internal; nothing the caller can validate beforehand,
// but a sanity render test protects against regressions
let json = serde_json::to_string_pretty(&cfg)
    .expect("MCP template must remain JSON-serializable");

Try / catch

match mcp_template_json() {
    Err(e) if e.to_string().contains("Failed to render MCP template JSON") => {
        eprintln!("internal template serialization bug: {e}");
    }
    Err(e) => return Err(e),
    Ok(t) => write_template_file(path, &t, force)?,
}

Prevention

When it happens

Trigger: Calling the MCP config initialization path (init_mcp_config / template rendering) where the in-memory McpConfig template contains values serde_json cannot represent (e.g. a map with non-string keys, a float that is not a finite number).

Common situations: A code change added a new field to the MCP template struct that is not JSON-serializable; custom serialization of server entries with exotic key types; effectively only reachable after a source-level regression.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/b2402d2204604dc9. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/lib.rs:3562

            read_timeout: None,
            disabled: true,
            enabled: true,
            required: false,
            enabled_tools: Vec::new(),
            disabled_tools: Vec::new(),
            headers: std::collections::HashMap::new(),
            env_headers: std::collections::HashMap::new(),
            bearer_token_env_var: None,
            scopes: Vec::new(),
            oauth: None,
            oauth_resource: None,
            reviewed_plugin: None,
            runtime_added: false,
            allow_private_network: false,
        },
    );
    serde_json::to_string_pretty(&cfg)
        .map_err(|e| anyhow!("Failed to render MCP template JSON: {e}"))
}

fn init_mcp_config(path: &Path, force: bool) -> Result<WriteStatus> {
    let template = mcp_template_json()?;
    write_template_file(path, &template, force)
}

fn skills_template(name: &str) -> String {
    format!(
        "\
---\n\
name: {name}\n\
description: Quick repo diagnostics and setup guidance\n\
allowed-tools: diagnostics, list_dir, read_file, grep_files, git_status, git_diff\n\
---\n\n\
When this skill is active:\n\
1. Run the diagnostics tool to report workspace and sandbox status.\n\
2. Skim key project files (README.md, Cargo.toml, AGENTS.md) before editing.\n\

View on GitHub (pinned to 433685b202)