Skip to content

Commit f061bf5

Browse files
committed
fix(opencode): accept 'env' as alias for 'environment' in MCP local config
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
1 parent 083abf0 commit f061bf5

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎packages/core/src/v1/config/mcp.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ export const Local = Schema.Struct({
88
command: Schema.mutable(Schema.Array(Schema.String)).annotate({
99
description: "Command and arguments to run the MCP server",
1010
}),
11-
environment: Schema.optional(Schema.Record(Schema.String, Schema.String)).annotate({
11+
env: Schema.optional(Schema.Record(Schema.String, Schema.String)).annotate({
1212
description: "Environment variables to set when running the MCP server",
1313
}),
14+
environment: Schema.optional(Schema.Record(Schema.String, Schema.String)).annotate({
15+
description: "Alias for 'env'. Prefer 'env' for consistency with MCP spec.",
16+
}),
1417
enabled: Schema.optional(Schema.Boolean).annotate({
1518
description: "Enable or disable the MCP server on startup",
1619
}),

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ export const layer = Layer.effect(
435435
...process.env,
436436
...(cmd === "opencode" ? { BUN_BE_BUN: "1" } : {}),
437437
...mcp.environment,
438+
...mcp.env,
438439
},
439440
})
440441
transport.stderr?.on("data", (chunk: Buffer) => {

‎packages/opencode/test/mcp/lifecycle.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ function getOrCreateClientState(name?: string): MockClientState {
5959
}
6060

6161
// Mock transport that succeeds or fails based on connectShouldFail / connectShouldHang
62+
let lastTransportOpts: any = undefined
63+
6264
class MockStdioTransport {
6365
stderr: null = null
6466
pid = 12345
6567
// oxlint-disable-next-line no-useless-constructor
66-
constructor(_opts: any) {}
68+
constructor(opts: any) {
69+
lastTransportOpts = opts
70+
}
6771
async start() {
6872
if (connectShouldHang) return new Promise<void>(() => {}) // never resolves
6973
if (connectShouldFail) throw new Error(connectError)
@@ -188,6 +192,7 @@ beforeEach(() => {
188192
connectError = "Mock transport cannot connect"
189193
clientCreateCount = 0
190194
transportCloseCount = 0
195+
lastTransportOpts = undefined
191196
})
192197

193198
// Import after mocks
@@ -1079,3 +1084,36 @@ it.instance(
10791084
},
10801085
},
10811086
)
1087+
1088+
// ========================================================================
1089+
// Test: 'env' field propagates to child process (#30892)
1090+
// ========================================================================
1091+
1092+
it.instance(
1093+
"env field in local MCP config is passed to spawned process",
1094+
() =>
1095+
MCP.Service.use((mcp: MCPNS.Interface) =>
1096+
Effect.gen(function* () {
1097+
lastCreatedClientName = "env-server"
1098+
const serverState = getOrCreateClientState("env-server")
1099+
serverState.tools = [
1100+
{ name: "test_tool", description: "A test tool", inputSchema: { type: "object", properties: {} } },
1101+
]
1102+
1103+
yield* mcp.add("env-server", {
1104+
type: "local",
1105+
command: ["npx", "-y", "mcp-searxng"],
1106+
env: {
1107+
SEARXNG_URL: "http://localhost:47820",
1108+
API_KEY: "secret123",
1109+
},
1110+
} as any)
1111+
1112+
expect((yield* mcp.status())["env-server"]?.status).toBe("connected")
1113+
expect(lastTransportOpts).toBeDefined()
1114+
expect(lastTransportOpts.env.SEARXNG_URL).toBe("http://localhost:47820")
1115+
expect(lastTransportOpts.env.API_KEY).toBe("secret123")
1116+
}),
1117+
),
1118+
{ config: { mcp: {} } },
1119+
)

0 commit comments

Comments
 (0)