Skip to main content

expand_env_value

Function expand_env_value 

Source
pub fn expand_env_value(
    value: &str,
    lookup: impl Fn(&str) -> Option<String>,
) -> String
Expand description

Expand ${VAR} / ${VAR:-default} references in an mcp.json env value against lookup (the parent process env at spawn time).

MCP children are spawned with a CLEARED environment (H3: never leak host secrets such as ANTHROPIC_API_KEY/AWS_* to third-party servers). This is the explicit, per-variable opt-in for forwarding a parent value — the same ${VAR} syntax Claude Code / Gemini CLI accept in their mcpServers env maps, so configs stay portable. Semantics:

  • ${VAR} → parent value, or “” when unset
  • ${VAR:-fallback} → parent value, or fallback when unset/empty
  • anything else (incl. a lone $, $VAR, unterminated ${) is literal

Only the FIRST expansion level is applied: a parent value containing ${…} is copied verbatim (no recursion, so no expansion loops).