diff --git a/src/config/index.ts b/src/config/index.ts index 8bf1adc..7ec1537 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -41,6 +41,14 @@ export const saveSettings = (settings: McpSettings): boolean => { } }; +export const replaceEnvVars = (env: Record): Record => { + const res: Record = {}; + for (const [key, value] of Object.entries(env)) { + res[key] = expandEnvVars(value); + } + return res; +}; + export const expandEnvVars = (value: string): string => { return value.replace(/\$\{([^}]+)\}/g, (_, key) => process.env[key] || ''); }; diff --git a/src/services/mcpService.ts b/src/services/mcpService.ts index 3f83688..ee16662 100644 --- a/src/services/mcpService.ts +++ b/src/services/mcpService.ts @@ -5,7 +5,7 @@ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; import { ServerInfo, ServerConfig } from '../types/index.js'; -import { loadSettings, saveSettings, expandEnvVars } from '../config/index.js'; +import { loadSettings, saveSettings, expandEnvVars, replaceEnvVars } from '../config/index.js'; import config from '../config/index.js'; import { getGroup } from './sseService.js'; import { getServersInGroup } from './groupService.js'; @@ -95,7 +95,7 @@ export const initializeClientsFromSettings = (isInit: boolean): ServerInfo[] => transport = new SSEClientTransport(new URL(conf.url)); } else if (conf.command && conf.args) { // If type is stdio or if command and args are provided without type - const env: Record = conf.env || {}; + const env: Record = replaceEnvVars(conf.env || {}); env['PATH'] = expandEnvVars(process.env.PATH as string) || ''; // Add UV_DEFAULT_INDEX from settings if available (for Python packages)