Hmbown/CodeWhale · error

invalid environment placeholder in MCP config value

Error message

invalid environment placeholder in MCP config value

What it means

`${NAME}` placeholders in MCP config values allow only non-empty names of ASCII alphanumerics and underscores. expand_env_placeholders_with bails when the name between `${` and `}` is empty or contains any other character (hyphens, dots, spaces, non-ASCII).

Solutions

  1. Rename the placeholder to ASCII letters, digits, and underscores only (e.g. ${OPENAI_API_KEY})
  2. If the environment variable itself has hyphens (unusual), expose an underscore-aliased variable
  3. Remove empty `${}` occurrences left by templates

Example fix

// before
"token": "${MY-API-KEY}"
// after
"token": "${MY_API_KEY}"
Defensive patterns

Strategy: validation

Validate before calling

const valid = /^\$\{[A-Za-z0-9_]+\}$/.test(placeholder) ||
  [...value.matchAll(/\$\{([^}]*)\}/g)].every(([, n]) => /^[A-Za-z0-9_]+$/.test(n));
if (!valid) throw new Error(`placeholder name in "${value}" must match [A-Za-z0-9_]+`);

Try / catch

try {
  loadMcpConfig(cfg);
} catch (e) {
  if (String(e).includes("invalid environment placeholder")) {
    // rename offending placeholder to snake_case and retry
  }
}

Prevention

When it happens

Trigger: Placeholders like ${}, ${MY-VAR} (hyphen), ${my.var} (dot), ${VAR NAME} (space), or ${K8S_NAMESPACE/KEY} in any MCP config value string.

Common situations: Using kebab-case environment variable conventions (e.g. ${OPENAI-API-KEY}) that the grammar rejects; nested or composite references like ${A.B}; accidentally leaving ${} from an unfilled template.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at crates/tui/src/mcp.rs:91

///
/// On a missing or malformed placeholder the error names only the offending
/// variable, never the surrounding value, so a secret-bearing string is never
/// echoed into logs or error output.
fn expand_env_placeholders_with(
    value: &str,
    environment: Option<&crate::plugins::HostEnvironment>,
) -> Result<String> {
    let mut out = String::new();
    let mut rest = value;
    while let Some(start) = rest.find("${") {
        out.push_str(&rest[..start]);
        let after = &rest[start + 2..];
        let Some(end) = after.find('}') else {
            anyhow::bail!("unterminated environment placeholder in MCP config value");
        };
        let name = &after[..end];
        if name.is_empty() || !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
            anyhow::bail!("invalid environment placeholder in MCP config value");
        }
        let env_value = environment
            .map_or_else(|| std::env::var(name), |env| env.var(name))
            .with_context(|| {
                format!("environment variable {name} required by MCP config is not set")
            })?;
        out.push_str(&env_value);
        rest = &after[end + 1..];
    }
    out.push_str(rest);
    Ok(out)
}

#[cfg(test)]
fn expand_env_placeholders(value: &str) -> Result<String> {
    expand_env_placeholders_with(value, None)
}

View on GitHub (pinned to 73e0f67d83)