@@ -99,18 +99,21 @@ export class ConfigurationManager {
9999
100100 settings [ 'mcp.servers' ] . forEach ( ( server ) => {
101101 if ( server . name && server . command ) {
102- // Build server configuration
102+ // Build server configuration with variable expansion
103103 const serverConfig : any = {
104- command : server . command
104+ command : this . _expandVariables ( server . command )
105105 } ;
106106
107- // Add args if provided
107+ // Add args if provided with variable expansion
108108 if ( server . args ) {
109109 if ( typeof server . args === 'string' ) {
110- // If args is a string, split it into array
111- serverConfig . args = server . args . trim ( ) . split ( / \s + / ) ;
110+ // If args is a string, split it into array and expand variables
111+ serverConfig . args = server . args . trim ( ) . split ( / \s + / ) . map ( ( arg : string ) => this . _expandVariables ( arg ) ) ;
112112 } else if ( Array . isArray ( server . args ) ) {
113- serverConfig . args = server . args ;
113+ // Expand variables in each argument
114+ serverConfig . args = server . args . map ( ( arg : any ) =>
115+ typeof arg === 'string' ? this . _expandVariables ( arg ) : arg
116+ ) ;
114117 }
115118 }
116119
@@ -126,13 +129,18 @@ export class ConfigurationManager {
126129 server . env . split ( / \s + / ) . forEach ( ( pair : string ) => {
127130 const [ key , value ] = pair . split ( '=' ) ;
128131 if ( key && value ) {
129- envObj [ key ] = value ;
132+ envObj [ key ] = this . _expandVariables ( value ) ;
130133 }
131134 } ) ;
132135 serverConfig . env = envObj ;
133136 }
134137 } else if ( typeof server . env === 'object' ) {
135- serverConfig . env = server . env ;
138+ // Expand variables in object values
139+ const expandedEnv : any = { } ;
140+ for ( const [ key , value ] of Object . entries ( server . env ) ) {
141+ expandedEnv [ key ] = typeof value === 'string' ? this . _expandVariables ( value ) : value ;
142+ }
143+ serverConfig . env = expandedEnv ;
136144 }
137145 }
138146
@@ -305,4 +313,35 @@ export class ConfigurationManager {
305313 return config . get < string > ( 'api.key' , '' ) ;
306314 }
307315
316+ /**
317+ * Expands environment variables in strings (e.g., $HOME, ${VAR})
318+ * Supports both $VAR and ${VAR} syntax
319+ * @param value String potentially containing environment variables
320+ * @returns String with expanded variables
321+ */
322+ private _expandVariables ( value : string ) : string {
323+ if ( ! value || typeof value !== 'string' ) {
324+ return value ;
325+ }
326+
327+ // Replace ${VAR} and $VAR with environment variable values
328+ return value . replace ( / \$ \{ ( [ ^ } ] + ) \} | \$ ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) / g, ( match , p1 , p2 ) => {
329+ const varName = p1 || p2 ;
330+ const envValue = process . env [ varName ] ;
331+
332+ // If variable exists, return its value; otherwise, return original
333+ if ( envValue !== undefined ) {
334+ return envValue ;
335+ }
336+
337+ // Special handling for common variables
338+ if ( varName === 'HOME' || varName === 'USERPROFILE' ) {
339+ return os . homedir ( ) ;
340+ }
341+
342+ // Return original if variable not found
343+ return match ;
344+ } ) ;
345+ }
346+
308347}
0 commit comments