From bc3c8facfa467d92c52edceccc4ec55e898cc7a8 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 22 Jul 2025 23:24:04 +0800 Subject: [PATCH] feat: add replaceEnvVarsInArray function and integrate it into server transport configuration (#241) Co-authored-by: samanhappy@qq.com --- src/config/index.ts | 43 ++++++++++++++++++++++++++++++-------- src/services/mcpService.ts | 2 +- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index affa41f..52c3af9 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -89,17 +89,42 @@ export const getSettingsCacheInfo = (): { hasCache: boolean } => { }; }; -export const replaceEnvVars = (env: Record): Record => { - const res: Record = {}; - for (const [key, value] of Object.entries(env)) { - if (typeof value === 'string') { - res[key] = expandEnvVars(value); - } else { - res[key] = String(value); +export function replaceEnvVars(input: Record): Record; +export function replaceEnvVars(input: string[] | undefined): string[]; +export function replaceEnvVars(input: string): string; +export function replaceEnvVars( + input: Record | string[] | string | undefined, +): Record | string[] | string { + // Handle object input + if (input && typeof input === 'object' && !Array.isArray(input)) { + const res: Record = {}; + for (const [key, value] of Object.entries(input)) { + if (typeof value === 'string') { + res[key] = expandEnvVars(value); + } else { + res[key] = String(value); + } } + return res; } - return res; -}; + + // Handle array input + if (Array.isArray(input)) { + return input.map((item) => expandEnvVars(item)); + } + + // Handle string input + if (typeof input === 'string') { + return expandEnvVars(input); + } + + // Handle undefined/null array input + if (input === undefined || input === null) { + return []; + } + + return input; +} export const expandEnvVars = (value: string): string => { if (typeof value !== 'string') { diff --git a/src/services/mcpService.ts b/src/services/mcpService.ts index 16cc49a..a901b94 100644 --- a/src/services/mcpService.ts +++ b/src/services/mcpService.ts @@ -183,7 +183,7 @@ const createTransportFromConfig = (name: string, conf: ServerConfig): any => { transport = new StdioClientTransport({ command: conf.command, - args: conf.args, + args: replaceEnvVars(conf.args) as string[], env: env, stderr: 'pipe', });