When configuring a local MCP server in opencode.jsonc with an env block (the standard key used by Claude Desktop, Cursor, and OpenCode's own LSP config), the environment variables were not being passed to the spawned child process.
Root causes:
Local schema in packages/core/src/v1/config/mcp.ts and packages/core/src/config/mcp.ts only accepted environment, causing Effect Schema's decode with onExcessProperty: "ignore" to strip env.
ConfigV2Compat.lower in packages/opencode/src/config/v2-compat.ts similarly omitted env from the intermediate Server schema.
connectLocal in packages/opencode/src/mcp/index.ts only spread ...mcp.environment when constructing StdioClientTransport.
Fix:
Added env as an optional string record in V1 and V2 Local MCP schemas and Server in v2-compat.ts.
Forwarded env in migrateMcp.
Merged both ...mcp.environment and ...mcp.env into the child process environment in connectLocal.
How did you verify your code works?
Added unit tests in packages/opencode/test/config/v2-compat.test.ts verifying env preservation in both flat and enveloped MCP configs.
Added an end-to-end unit test in packages/opencode/test/mcp/lifecycle.test.ts verifying that TEST_MCP_ENV_VAR configured via env is received by the spawned child process MCP server.
Ran test suites:
bun test test/config/v2-compat.test.ts (37/37 pass)
bun test test/mcp/lifecycle.test.ts (22/22 pass)
bun test test/mcp (62/62 pass across 10 files)
bun test packages/core/test/config/config.test.ts (15/15 pass)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #49983
Type of change
What does this PR do?
When configuring a local MCP server in opencode.jsonc with an env block (the standard key used by Claude Desktop, Cursor, and OpenCode's own LSP config), the environment variables were not being passed to the spawned child process.
Root causes:
Fix:
How did you verify your code works?
Checklist