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

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

OpenCode is an open-source terminal coding agent. It supports local (stdio) and remote MCP servers under the `mcp` key of `opencode.json`, with `{env:VAR}` and `{file:path}` substitution and automatic OAuth. It reads `AGENTS.md` (falling back to `CLAUDE.md`), and its `permission` settings default most tools to allow but ask before reading `.env` files.

## MCP support

Yes. Transports: stdio, http.

- project: `opencode.json`
- global: `~/.config/opencode/opencode.json`

## Environment variables and .env files

Local servers take an `environment` object; `{env:VAR_NAME}` (empty string if unset) and `{file:path}` substitute values anywhere in the config, and the docs suggest `{file:...}` for keeping API keys out of the main config. OAuth tokens for remote servers are stored in `~/.local/share/opencode/mcp-auth.json`. The pages reviewed do not describe `.env` loading by OpenCode itself.

## File access

Under the `permission` key each tool is `allow`, `ask` or `deny`, with wildcard patterns where the last matching rule wins. Most permissions default to allow, while `doom_loop` and `external_directory` default to ask. `read` defaults to allow, but `.env` files require approval by default: the docs' example sets `*.env` and `*.env.*` to ask and `*.env.example` to allow.

Deny or ignore files with: permission rules in opencode.json (e.g. "*.env": "deny").

## Credential risks

- **CVE-2026-22812 (before 1.0.216).** OpenCode automatically started an unauthenticated HTTP server that let any local process, or any website through permissive CORS, run shell commands and read files with the user's privileges. Fixed in 1.0.216 (advisory GHSA-vxw4-wv6m-9hhh).
- **Permissive defaults.** The permissions page states most permissions default to allow, so shell and file tools run without a prompt unless you tighten them; only `.env` reads, `doom_loop` and `external_directory` ask by default.
- **Remote instruction URLs.** The `instructions` key accepts remote URLs that OpenCode fetches (5-second timeout) and merges with `AGENTS.md`, so remote content becomes agent instructions.

## FAQ

### How do I add an MCP server to OpenCode?

Add it under `mcp` in `opencode.json` with `type` set to `local` (with a `command` array) or `remote` (with a `url`). The docs document `opencode mcp auth`, `list`, `logout` and `debug`, but no `add` command.

### How do I use an environment variable in opencode.json?

Write `{env:VAR_NAME}`, for example `"Authorization": "Bearer {env:MY_API_KEY}"`. Use `{file:path}` to read a value from a file.

### Does OpenCode read .env files?

Reads of `.env` files require approval by default. You can deny them with a rule such as `"*.env": "deny"` under `permission.read`.

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

Don't put the key in the prompt, in a file OpenCode 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://opencode.ai/docs/mcp-servers/
- https://opencode.ai/docs/config/
- https://opencode.ai/docs/permissions/
- https://opencode.ai/docs/rules/
- https://advisories.gitlab.com/pkg/npm/opencode-ai/CVE-2026-22812/
