Fixes › Claude Desktop, Cursor, Claude Code, VS Code

MCP server can't see my API key (env vars not passed)

Claude DesktopCursorClaude CodeVS Code

The error

401 Unauthorized • "API key is required" • "Missing environment variable"

Why it happens

MCP clients start servers with a minimal environment. Variables exported in your shell profile are usually not inherited, so the key has to be put in the server's env block (or passed with the client's CLI flag).

How to fix it

  1. Add an env block next to command/args
    Keys go inside the server entry, not at the top level.
    {
      "mcpServers": {
        "brave-search": {
          "command": "npx",
          "args": ["-y", "@brave/brave-search-mcp-server"],
          "env": { "BRAVE_API_KEY": "your-key" }
        }
      }
    }
  2. Claude Code: use -e
    Pass variables with --env/-e when adding the server.
    claude mcp add brave -e BRAVE_API_KEY=your-key -- npx -y @brave/brave-search-mcp-server
  3. VS Code: use inputs for secrets
    VS Code's mcp.json supports ${input:…} prompts so you don't hardcode keys.
    {
      "inputs": [{ "type": "promptString", "id": "brave-key", "description": "Brave API key", "password": true }],
      "servers": {
        "brave": { "command": "npx", "args": ["-y", "@brave/brave-search-mcp-server"], "env": { "BRAVE_API_KEY": "${input:brave-key}" } }
      }
    }
  4. Restart and avoid committing keys
    Restart the client. Add project-level config files that contain keys to .gitignore.

Related servers

Sources