Hmbown/CodeWhale · error

unterminated environment placeholder in MCP config value

Error message

unterminated environment placeholder in MCP config value

What it means

MCP config values support `${NAME}` environment-variable placeholders. expand_env_placeholders_with scans each value and bails when it finds `${` with no closing `}` — an unterminated placeholder would otherwise be silently passed through as literal text.

Solutions

  1. Add the missing `}` to close the placeholder: ${VAR}
  2. If a literal `${` is intended, escape or restructure the value so it does not parse as a placeholder
  3. Validate the JSON config values with a quick check for balanced `${`...`}` pairs before loading

Example fix

// before
"header": "Bearer ${API_KEY"
// after
"header": "Bearer ${API_KEY}"
Defensive patterns

Strategy: validation

Validate before calling

function checkPlaceholders(v) {
  let i = 0;
  while ((i = v.indexOf("${", i)) !== -1) {
    const end = v.indexOf("}", i);
    if (end === -1) throw new Error(`unterminated placeholder in: ${v}`);
    i = end + 1;
  }
}

Try / catch

try {
  loadMcpConfig(cfg);
} catch (e) {
  if (String(e).includes("unterminated environment placeholder")) {
    // point user at the offending value; check balanced ${...} pairs
  }
}

Prevention

When it happens

Trigger: An MCP config value containing `${VAR` without a closing brace, e.g. "Bearer ${API_KEY" or a value with a stray `${` in a URL; JSON editing that accidentally deleted the closing brace; shell escaping leaving a literal `${` behind.

Common situations: Hand-editing mcp.json and dropping the brace; copy-pasting shell snippets where `${VAR}` was meant for the shell, not the config; config generators that truncate values.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

/// Expand `${NAME}` placeholders in an MCP config value from the process
/// environment. This lets secrets (API keys, bearer tokens, …) be supplied
/// through environment variables instead of being written in cleartext into
/// the MCP config file on disk.
///
/// 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)]

View on GitHub (pinned to 73e0f67d83)