8000 fix(opencode): write MCP secrets as {env:VAR} in the project opencode.json by ahmet-cetinkaya · Pull Request #27 · ahmet-cetinkaya/agent-ctrl · GitHub
[go: up one dir, main page]

Skip to content

fix(opencode): write MCP secrets as {env:VAR} in the project opencode.json - #27

Merged
ahmet-cetinkaya merged 1 commit into
mainfrom
fix/opencode-mcp-env-placeholders
Oct 31, 2026
Merged

ahmet-cetinkaya merged 1 commit into
mainfrom
fix/opencode-mcp-env-placeholders

Conversation

@ahmet-cetinkaya
Copy link
Copy Markdown
Owner

Closes #24

Problem

profile apply opencode (project scope) wrote MCP servers to <project>/opencode.json with ${VAR} placeholders already replaced by the values from .agent-ctrl/.env. The project opencode.json is meant to be committed ("safe to be checked into Git", https://opencode.ai/docs/config/), so secrets ended up in the repository working tree.

Change

  • McpServerAggregator resolves placeholders centrally. It now also keeps the unresolved command, args, env and url of each server (unresolved on McpLoadedServer and ApplyMcpServer, passed through by ApplySourceLoader and ProfileScanner).
  • For project scope, the OpenCode adapter renders those unresolved fields with each ${VAR} written as OpenCode's {env:VAR}. OpenCode substitutes {env:VAR} from its environment at run time (https://opencode.ai/docs/config/; MCP examples at https://opencode.ai/docs/mcp-servers/).
  • The apply output lists the variables to export, also without -v:
    OpenCode reads these MCP values from the environment at run time; export them before starting opencode: TOKEN
  • User scope (~/.config/opencode/opencode.json, private) keeps the resolved values, unchanged.
  • Docs: docs/platforms/OPENCODE.md.

Example output (<project>/opencode.json):

"tok": { "type": "local", "command": ["tool", "--k", "{env:TOKEN}"], "environment": { "API_TOKEN": "{env:TOKEN}" } }

Behaviour change

OpenCode does not read .agent-ctrl/.env. Project-scope MCP servers that use placeholders now need those variables exported in the shell that starts opencode; an unset variable becomes an empty string.

Verification

  • New tests in OpenCodeAdapter.test.ts: project scope has no secret, uses {env:VAR} in environment, command and url, and warns with the variable name (failed before the fix); user scope keeps the resolved value.
  • bun test: 1124 pass, 0 fail. scripts/lint.sh, Prettier (changed files), bun run build pass.
  • Manual check: profile apply opencode in a throwaway project with a secret-using server: no secret anywhere in the project tree, placeholders as above, warning shown without -v.

Not in this PR

  • Kilo also renders MCP through the same function and is unchanged; whether Kilo supports {env:VAR} is not verified.
  • Other platforms' project MCP files were not audited for the same problem.
  • CHANGELOG entry: added at release time.

….json

`profile apply opencode` (project scope) wrote MCP servers to
`<project>/opencode.json` with `${VAR}` placeholders already replaced by
the values from `.agent-ctrl/.env`. That file is meant to be committed,
so secrets ended up in the repository working tree.

MCP placeholders are resolved centrally in McpServerAggregator, so the
loader now also keeps the unresolved command, args, env and url. For
project scope the OpenCode adapter renders those with each `${VAR}`
written as OpenCode's `{env:VAR}`, which OpenCode substitutes from its
environment at run time, and warns which variables to export. The
private user-level ~/.config/opencode/opencode.json keeps resolved values.

Closes #24
@ahmet-cetinkaya ahmet-cetinkaya self-assigned this Oct 31, 2026
@ahmet-cetinkaya
ahmet-cetinkaya merged commit 574723b into main Oct 31, 2026
@ahmet-cetinkaya
ahmet-cetinkaya deleted the fix/opencode-mcp-env-placeholders branch October 31, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

profile apply opencode writes resolved MCP secrets into <project>/opencode.json

1 participant

0