xai-org/grok-build · error

Invalid command '{command}': it looks like an environment va

Error message

Invalid command '{command}': it looks like an environment variable. Pass each variable as its own flag: {pairs}

What it means

When the trailing command after `--` looks like a KEY=value environment variable pair, the CLI assumes an env assignment accidentally escaped the greedy/old `-e` parsing and refuses to treat it as the command. It echoes the offending token and suggests re-issuing each pair as its own `-e` flag. This guards against silently launching a bogus executable name.

Source

Thrown at crates/codegen/xai-grok-pager/src/mcp_cmd.rs:340

        McpTransport::Stdio => {
            let Some(command) = source else {
                bail!(
                    "A command is required for stdio servers. Usage: grok mcp add <name> -- <command> [args...]"
                );
            };
            if !args.header.is_empty() {
                bail!("--header can only be used with HTTP or SSE servers.");
            }
            // A KEY=value command means an env pair leaked out of -e, which takes one pair per flag (the old --env was greedy)
            if looks_like_env_pair(command) {
                let pairs: Vec<String> = args
                    .env
                    .iter()
                    .map(String::as_str)
                    .chain([command])
                    .map(|pair| format!("-e {pair}"))
                    .collect();
                bail!(
                    "Invalid command '{command}': it looks like an environment variable. Pass each variable as its own flag: {}",
                    pairs.join(" ")
                );
            }
            let env = parse_env_vars(&args.env)?;

            let mut warnings = Vec::new();
            if !explicit_transport && looks_like_url(command) {
                // Suggest a command that passes URL validation even when the original lacks a scheme (e.g. localhost:3000).
                let suggested_url =
                    if command.starts_with("http://") || command.starts_with("https://") {
                        command.to_string()
                    } else {
                        format!("http://{command}")
                    };
                warnings.push(format!(
                    "Warning: '{command}' looks like a URL, but it is being added as a stdio command because --transport was not specified.\nFor a remote server, use: grok mcp add --transport http {} {suggested_url}",
                    args.name

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Pass each environment variable as its own -e flag: -e KEY1=value1 -e KEY2=value2.
  2. Ensure the real executable appears first after `--`, e.g. `-- npx -y pkg` not `-- KEY=value npx -y pkg`.
  3. Inline env vars in the shell instead (`KEY=value grok mcp add ...`) if you meant them for the CLI itself.

Example fix

// before
grok mcp add api -- API_KEY=abc node server.js
// after
grok mcp add api -e API_KEY=abc -- node server.js
Defensive patterns

Strategy: validation

Validate before calling

fn looks_like_env_pair(s: &str) -> bool {
    match s.split_once('=') { Some((k, _)) => !k.is_empty(), None => false }
}
if let Some(cmd) = command { assert!(!looks_like_env_pair(cmd), "'{cmd}' looks like an env pair; use -e instead"); }

Try / catch

match result {
    Err(e) if e.to_string().contains("looks like an environment variable") => {
        // re-run with -e flags per the suggested pairs in the message
    }
    other => other?,
}

Prevention

When it happens

Trigger: `grok mcp add <name> -- KEY=value` where the first (or only) token after `--` matches KEY=value shape and the transport is stdio; e.g. the user forgot the actual command or an env var was consumed as the command.

Common situations: Older command lines written when `--env` accepted multiple pairs, shell scripts where quoting dropped the real command, pasting `FOO=bar npx ...` style commands where the env prefix was split incorrectly.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/e5135b506808ed95. Report an issue: GitHub.