Skip to content

fix(opencode): accept 'env' as alias for 'environment' in MCP local config#31317

Open
sulthonzh wants to merge 2 commits into
anomalyco:devfrom
sulthonzh:fix/mcp-env-field-alias
Open

fix(opencode): accept 'env' as alias for 'environment' in MCP local config#31317
sulthonzh wants to merge 2 commits into
anomalyco:devfrom
sulthonzh:fix/mcp-env-field-alias

Conversation

@sulthonzh
Copy link
Copy Markdown

Issue for this PR

Closes #30892
Closes #26332

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Users configuring MCP servers with "env": { "KEY": "value" } in opencode.json had their variables silently dropped because the schema only accepted "environment". The MCP spec and most documentation examples use env, so this is a common user error that produces no warning.

The schema now accepts both env and environment as optional fields on local MCP config. In connectLocal(), mcp.env is spread after mcp.environment so env takes precedence when both are present.

How did you verify your code works?

  • New test: env field in local MCP config is passed to spawned process — configures a local MCP with env field, verifies the transport receives the env vars in its options
  • All 27 tests pass (26 existing + 1 new)
  • Full typecheck across 23 packages passes
  • Manual verification: write "env": { "MY_VAR": "test" } in opencode.json MCP config, start OpenCode, verify the spawned process has MY_VAR in its environment

Screenshots / recordings

N/A — no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

sulthonzh added 2 commits June 8, 2026 20:54
When an MCP server process dies or the HTTP transport drops, the client
was left in connected state with stale tool definitions. No reconnect
was attempted, so tools permanently vanished for the session.

Register an onclose handler on each MCP client that:
- Detects unexpected transport closure (not intentional disconnect)
- Marks the server as failed and clears cached defs
- Re-creates the client via createAndStore() using the original config
- Publishes ToolsChanged event so the UI updates

Guard all intentional close paths (disconnect, finalizer, closeClient)
by deleting s.clients[name] BEFORE calling client.close(), so the
onclose handler sees s.clients[name] !== client and returns early.

Fixes anomalyco#17099
…onfig

Users writing 'env' in opencode.json MCP config (matching MCP spec
convention) had their variables silently dropped because the schema
only accepted 'environment'. This caused MCP servers to start but fail
at runtime when reading process.env.

Add 'env' as an optional field in ConfigMCPV1.Local schema. In
connectLocal(), spread mcp.env after mcp.environment so env takes
precedence when both are present.

Fixes anomalyco#30892
Closes anomalyco#26332
@sulthonzh sulthonzh force-pushed the fix/mcp-env-field-alias branch from a28f4d0 to f061bf5 Compare June 8, 2026 13:54
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.

MCP local server env block not propagated to child process MCP local server: env config not passed to spawned child process

1 participant