From dfe3fcbb0b692c77a98de4c40b9be9641343f548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=86=A0=E8=BE=B0?= Date: Tue, 11 Aug 2026 15:54:28 +0800 Subject: [PATCH] fix(config): accept env alias for native MCP servers --- packages/opencode/src/config/config.ts | 15 +++++++++ packages/opencode/src/config/mcp.ts | 3 ++ packages/opencode/test/config/config.test.ts | 35 ++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index e90542d54..53a4d5a08 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -60,6 +60,21 @@ function mergeConfigConcatArrays(target: Info, source: Info): Info { function normalizeLoadedConfig(data: unknown, source: string) { if (!isRecord(data)) return data const copy = { ...data } + if (isRecord(copy.mcp)) { + copy.mcp = Object.fromEntries( + Object.entries(copy.mcp).map(([name, server]) => { + if (!isRecord(server) || !isRecord(server.env)) return [name, server] + const { env, ...rest } = server + return [ + name, + { + ...rest, + ...(!("environment" in server) && { environment: env }), + }, + ] + }), + ) + } const hadLegacy = "theme" in copy || "keybinds" in copy || "tui" in copy if (!hadLegacy) return copy delete copy.theme diff --git a/packages/opencode/src/config/mcp.ts b/packages/opencode/src/config/mcp.ts index 67d95ad2c..4b8716b80 100644 --- a/packages/opencode/src/config/mcp.ts +++ b/packages/opencode/src/config/mcp.ts @@ -21,6 +21,9 @@ export class Local extends Schema.Class("McpLocalConfig")({ environment: Schema.optional(Schema.Record(Schema.String, Schema.String)).annotate({ description: "Environment variables to set when running the MCP server", }), + env: Schema.optional(Schema.Record(Schema.String, Schema.String)).annotate({ + description: "@deprecated Use environment. Alias for environment variables to set when running the MCP server.", + }), enabled: Schema.optional(Schema.Boolean).annotate({ description: "Enable or disable the MCP server on startup", }), diff --git a/packages/opencode/test/config/config.test.ts b/packages/opencode/test/config/config.test.ts index 551ca60c6..3e8943158 100644 --- a/packages/opencode/test/config/config.test.ts +++ b/packages/opencode/test/config/config.test.ts @@ -147,6 +147,41 @@ test("loads JSON config file", async () => { }) }) +test("loads native MCP env alias as environment", async () => { + await using tmp = await tmpdir({ + init: async (dir) => { + await writeConfig(dir, { + $schema: "https://opencode.ai/config.json", + mcp: { + postgres: { + type: "local", + command: ["cmd.exe", "/c", "npx", "-y", "@yawlabs/postgres-mcp@latest"], + env: { + DATABASE_URL: "postgres://postgres:secret@localhost:5432/demo1", + ALLOW_WRITES: "1", + }, + }, + }, + }) + }, + }) + + await Instance.provide({ + directory: tmp.path, + fn: async () => { + const config = await load() + expect(config.mcp?.postgres).toEqual({ + type: "local", + command: ["cmd.exe", "/c", "npx", "-y", "@yawlabs/postgres-mcp@latest"], + environment: { + DATABASE_URL: "postgres://postgres:secret@localhost:5432/demo1", + ALLOW_WRITES: "1", + }, + }) + }, + }) +}) + test("loads Claude Code MCP servers from home and project config", async () => { await writeClaudeConfig(path.join(Global.Path.home, ".claude.json"), { mcpServers: {