anomalyco / anomalyco/opencode
opencode v2: background service does not inherit workspace .envrc variables from the TUI
@rekram1-node is already working on this.
Since Aug 25, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Summary
OpenCode V2 resolves {env:...} values for local MCP servers from the background service environment, not from the environment that launched the TUI session.
When a workspace uses .envrc, variables available in the terminal that started the TUI are unavailable to a background service launched elsewhere or before direnv activated the workspace. This prevents local MCP servers from receiving their configured credentials.
Environment
- opencode version: 0.0.0-beta-18155
- OS: Linux 7.0.0-30-generic (linux x64)
- Terminal: Ghostty (
TERM=xterm-ghostty,COLORTERM=truecolor) - Shell: /usr/bin/zsh
- Install/channel: beta
- Active plugins: configured; omitted for privacy.
Reproduction
- Create a workspace
.envrcthat exports a variable required by a local MCP:export EXAMPLE_TOKEN="..." - Allow the
.envrcand start OpenCode V2 from a terminal whereEXAMPLE_TOKENis available throughdirenv. - Configure a local MCP server using the variable:
{ "mcp": { "servers": { "example": { "type": "local", "command": ["<local-mcp-command>"], "environment": { "REQUIRED_TOKEN": "{env:EXAMPLE_TOKEN}" } } } } } - Ensure the OpenCode background service was started outside that workspace, or before the workspace
.envrcwas activated. - Run
opencode2 mcp list.
Expected Behavior
A TUI session started from a workspace with an active .envrc should make that workspace environment available when resolving {env:...} substitutions for its local MCP servers.
If this is incompatible with the shared-service model, OpenCode V2 should clearly document the limitation and report that the variable is unavailable in the background-service environment.
Actual Behavior
The local MCP receives an empty or missing required environment variable and exits during startup. OpenCode reports only:
example failed: MCP error -32000: Connection closed
The child process stderr identifies the missing variable when run directly, but that detail is not surfaced by OpenCode. Its log contains only:
message="mcp connect failed" server=example status.error="MCP error -32000: Connection closed"
No corresponding child stderr is recorded, even when the MCP is configured with LOG_LEVEL=debug.
Additional Context
The same MCP configuration connects successfully when the background service has the required environment variable, and fails after a service restart that does not inherit the workspace .envrc.
This makes MCP behavior depend on where and when the shared background service was started rather than on the environment of the workspace TUI session.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.