# Secrets and API keys in Gemini CLI: keep credentials out of the agent's reach

Source: https://botpassword.com/agents/gemini-cli
Last checked: 2026-10-25

Gemini CLI is Google's open-source terminal agent. It supports MCP over stdio, SSE and streamable HTTP, configured under `mcpServers` in `~/.gemini/settings.json` or `.gemini/settings.json`, or with `gemini mcp add`. It loads `.env` files, reads `GEMINI.md` for instructions, and redacts secret-looking host variables before starting MCP servers.

## MCP support

Yes. Transports: stdio, sse, http.

- user: `~/.gemini/settings.json`
- project: `.gemini/settings.json`

Add a server with `gemini mcp add [options] <name> <commandOrUrl> [args...]`.

## Environment variables and .env files

Gemini CLI loads a `.env` file by searching from the current directory upward until it finds one, reaches the project root (`.git`) or the home directory, then falls back to `~/.env`. In an MCP server's `env` block, `$VAR`, `${VAR}` (and `%VAR%` on Windows) expand, and undefined variables become empty strings. By default, host variables such as `GEMINI_API_KEY`, `GOOGLE_API_KEY` and names matching `*TOKEN*`, `*SECRET*`, `*PASSWORD*`, `*KEY*`, `*AUTH*` and `*CREDENTIAL*` are redacted before an MCP server is spawned; variables listed explicitly in the server's `env` are trusted and kept.

## File access

The agent reads and edits files in the workspace and asks before running shell commands. A `.geminiignore` file in the project root (gitignore-style patterns) excludes matching files from tools that support it, such as the `@` command; Google states only tools that respect the file honour it, and it takes effect after a restart. MCP servers set `trust: false` by default, and `includeTools` and `excludeTools` limit which tools of a server the model sees.

Deny or ignore files with: .geminiignore.

## Credential risks

- **Allowlist bypass with prompt injection (fixed in v0.1.14).** Tracebit reported in 2025 that a malicious `README.md` or `GEMINI.md` could make Gemini CLI run a command disguised as an allowlisted one such as `grep`, and exfiltrate environment variables to an external server. Google fixed it on 25 July 2025.
- **`.env` files are loaded from parent directories and the home directory.** The documented search order means a `.env` outside the project can supply variables to the CLI and the commands it runs.
- **`trust: true` skips confirmations.** Setting `trust` on an MCP server (or `--trust` in `gemini mcp add`) bypasses all tool-call confirmation prompts for that server.

## FAQ

### Where does Gemini CLI store MCP servers?

Under `mcpServers` in `~/.gemini/settings.json` (user) or `.gemini/settings.json` (project). `gemini mcp add` writes to project scope by default.

### Does Gemini CLI load .env files?

Yes. It looks for `.env` in the current directory, then parent directories up to the project root or home directory, then `~/.env`.

### Does Gemini CLI pass my API keys to MCP servers?

Not by default. Host variables with names like `*KEY*`, `*TOKEN*` or `*SECRET*` are redacted unless you list them in the server's `env` block, for example `"MY_KEY": "$MY_KEY"`.

### How do I give Gemini CLI an API key without it seeing the key?

Don't put the key in the prompt, in a file Gemini CLI can read, or in an environment variable it can print. Give it a tool that performs the authenticated step instead: a credential broker exposed over MCP or a CLI that injects the key into the command or HTTP request and returns only the result. BotPassword is that broker, through `bpw mcp` and the `bpw` CLI.

## Sources

- https://github.com/google-gemini/gemini-cli/blob/main/docs/tools/mcp-server.md
- https://geminicli.com/docs/reference/configuration/
- https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/gemini-ignore.md
- https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/gemini-md.md
- https://tracebit.com/blog/code-exec-deception-gemini-ai-cli-hijack
