Skip to content

feat: Support environment variable substitution in MCP server headers #5282

@ksprashu

Description

@ksprashu

What would you like to be added?

The Gemini CLI should support environment variable substitution within the headers values of the mcpServers configuration. This would allow users to store sensitive tokens securely in their environment and reference them in the configuration.

Eg:

  {
    "mcpServers": {
      "myApiServer": {
        "httpUrl": "https://api.example.com/",
        "headers": {
          "Authorization": "Bearer $MY_API_TOKEN"
        }
      }
    }
  }

The CLI should replace $MY_API_TOKEN with the value of the MY_API_TOKEN environment variable before sending the request. It should support both $VAR and ${VAR} syntax.

Why is this needed?

Currently, when configuring an MCP server in settings.json, any values in the headers object are treated as literal strings. This means sensitive data, like bearer tokens or API keys, must be hardcoded into the configuration file, which is a security risk.

Additional context

The current alternative is to hardcode the token, which is not secure. Another alternative would be to use a local script as a proxy, but this adds unnecessary complexity for a common use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/toolsBuilt-in tools and the MCP ecosystem.kind/enhancementNew feature or requestpriority/p2Important but can be addressed in a future release.sub-area/mcpIssues related to the Model Context Protocol (MCP).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions