@@ -347,24 +347,27 @@ class MCPConnection {
347347 ) : Promise < Transport > {
348348 switch ( options . transport . type ) {
349349 case "stdio" :
350- const env : Record < string , string > = options . transport . env
351- ? { ...options . transport . env }
352- : { } ;
353-
354- if ( process . env . PATH !== undefined ) {
355- // Set the initial PATH from process.env
356- env . PATH = process . env . PATH ;
357-
358- // For non-Windows platforms, try to get the PATH from user shell
359- if ( process . platform !== "win32" ) {
360- try {
361- const shellEnvPath = await getEnvPathFromUserShell ( ) ;
362- if ( shellEnvPath && shellEnvPath !== process . env . PATH ) {
363- env . PATH = shellEnvPath ;
364- }
365- } catch ( err ) {
366- console . error ( "Error getting PATH:" , err ) ;
350+ const processEnv : Record < string , string > = Object . fromEntries (
351+ Object . entries ( process . env ) . filter ( ( [ _ , v ] ) => v !== undefined ) as [
352+ string ,
353+ string ,
354+ ] [ ] ,
355+ ) ;
356+
357+ const env = {
358+ ...processEnv ,
359+ ...( options . transport . env ?? { } ) ,
360+ } ;
361+
362+ // For non-Windows platforms, try to get the PATH from user shell
363+ if ( process . platform !== "win32" ) {
364+ try {
365+ const shellEnvPath = await getEnvPathFromUserShell ( ) ;
366+ if ( shellEnvPath && shellEnvPath !== process . env . PATH ) {
367+ env . PATH = shellEnvPath ;
367368 }
369+ } catch ( err ) {
370+ console . error ( "Error getting PATH:" , err ) ;
368371 }
369372 }
370373
0 commit comments