55import java .util .HashMap ;
66import java .util .List ;
77import java .util .Map ;
8- import java .util .Objects ;
98import java .util .stream .Collectors ;
109
1110import com .fasterxml .jackson .annotation .JsonProperty ;
12-
13- import org .springframework .util .CollectionUtils ;
11+ import spring .ai .mcp .client .util .Assert ;
1412
1513/**
1614 * Server parameters for stdio client.
@@ -19,11 +17,11 @@ public class StdioServerParameters {
1917
2018 // Environment variables to inherit by default
2119 private static final List <String > DEFAULT_INHERITED_ENV_VARS = System .getProperty ("os.name" )
22- .toLowerCase ()
23- .contains ("win" )
24- ? Arrays .asList ("APPDATA" , "HOMEDRIVE" , "HOMEPATH" , "LOCALAPPDATA" , "PATH" ,
25- "PROCESSOR_ARCHITECTURE" , "SYSTEMDRIVE" , "SYSTEMROOT" , "TEMP" , "USERNAME" , "USERPROFILE" )
26- : Arrays .asList ("HOME" , "LOGNAME" , "PATH" , "SHELL" , "TERM" , "USER" );
20+ .toLowerCase ()
21+ .contains ("win" )
22+ ? Arrays .asList ("APPDATA" , "HOMEDRIVE" , "HOMEPATH" , "LOCALAPPDATA" , "PATH" ,
23+ "PROCESSOR_ARCHITECTURE" , "SYSTEMDRIVE" , "SYSTEMROOT" , "TEMP" , "USERNAME" , "USERPROFILE" )
24+ : Arrays .asList ("HOME" , "LOGNAME" , "PATH" , "SHELL" , "TERM" , "USER" );
2725
2826 @ JsonProperty ("command" )
2927 private String command ;
@@ -35,13 +33,13 @@ public class StdioServerParameters {
3533 private Map <String , String > env ;
3634
3735 private StdioServerParameters (String command , List <String > args , Map <String , String > env ) {
38- Objects . nonNull (command );
39- Objects . nonNull (args );
36+ Assert . notNull (command , "The command can not be null" );
37+ Assert . notNull (args , "The args can not be null" );
4038
4139 this .command = command ;
4240 this .args = args ;
4341 this .env = new HashMap <>(getDefaultEnvironment ());
44- if (! CollectionUtils .isEmpty (env )) {
42+ if (env != null && ! env .isEmpty ()) {
4543 this .env .putAll (env );
4644 }
4745 }
@@ -71,38 +69,38 @@ public static class Builder {
7169 private Map <String , String > env = new HashMap <>();
7270
7371 public Builder (String command ) {
74- Objects . no . hasText (command , "Command must not be empty " );
72+ Assert . notNull (command , "The command can not be null " );
7573 this .command = command ;
7674 }
7775
7876 public Builder args (String ... args ) {
79- Assert .notNull (args , "Arguments must not be null" );
77+ Assert .notNull (args , "The args can not be null" );
8078 this .args = Arrays .asList (args );
8179 return this ;
8280 }
8381
8482 public Builder args (List <String > args ) {
85- Assert .notNull (args , "Arguments must not be null" );
83+ Assert .notNull (args , "The args can not be null" );
8684 this .args = new ArrayList <>(args );
8785 return this ;
8886 }
8987
9088 public Builder arg (String arg ) {
91- Assert .hasText (arg , "Argument must not be empty " );
89+ Assert .notNull (arg , "The arg can not be null " );
9290 this .args .add (arg );
9391 return this ;
9492 }
9593
9694 public Builder env (Map <String , String > env ) {
97- if (! CollectionUtils .isEmpty (env )) {
95+ if (env != null && ! env .isEmpty ()) {
9896 this .env .putAll (env );
9997 }
10098 return this ;
10199 }
102100
103101 public Builder addEnvVar (String key , String value ) {
104- Assert .hasText (key , "Environment variable key must not be empty " );
105- Assert .notNull (value , "Environment variable value must not be null" );
102+ Assert .notNull (key , "The key can not be null " );
103+ Assert .notNull (value , "The value can not be null" );
106104 this .env .put (key , value );
107105 return this ;
108106 }
@@ -119,12 +117,12 @@ public StdioServerParameters build() {
119117 */
120118 private static Map <String , String > getDefaultEnvironment () {
121119 return System .getenv ()
122- .entrySet ()
123- .stream ()
124- .filter (entry -> DEFAULT_INHERITED_ENV_VARS .contains (entry .getKey ()))
125- .filter (entry -> entry .getValue () != null )
126- .filter (entry -> !entry .getValue ().startsWith ("()" ))
127- .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
120+ .entrySet ()
121+ .stream ()
122+ .filter (entry -> DEFAULT_INHERITED_ENV_VARS .contains (entry .getKey ()))
123+ .filter (entry -> entry .getValue () != null )
124+ .filter (entry -> !entry .getValue ().startsWith ("()" ))
125+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
128126 }
129127
130128}
0 commit comments