anomalyco / anomalyco/opencode

opencode v2: background service does not inherit workspace .envrc variables from the TUI

Open
#44,914 0 comments 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Aug 25, 2026.

2.0
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

  1. Create a workspace .envrc that exports a variable required by a local MCP:
    export EXAMPLE_TOKEN="..."
    
  2. Allow the .envrc and start OpenCode V2 from a terminal where EXAMPLE_TOKEN is available through direnv.
  3. Configure a local MCP server using the variable:
    {
      "mcp": {
        "servers": {
          "example": {
            "type": "local",
            "command": ["<local-mcp-command>"],
            "environment": {
              "REQUIRED_TOKEN": "{env:EXAMPLE_TOKEN}"
            }
          }
        }
      }
    }
    
  4. Ensure the OpenCode background service was started outside that workspace, or before the workspace .envrc was activated.
  5. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.