@@ -23,6 +23,14 @@ async function main() {
23
23
let configPath = null ;
24
24
let serverName = null ;
25
25
26
+ // Inspector can either be ran with a config file or a command to start an MCP server
27
+ // Order of precedence is:
28
+ // 1. Load configuration from MCP server config file
29
+ // 2. Use direct command (and args) provided on the command line (if no config file is provided)
30
+
31
+ // Early check if an MCP server config file is provided to make logic simpler below
32
+ const configProvided = args . includes ( "--config" ) ;
33
+
26
34
for ( let i = 0 ; i < args . length ; i ++ ) {
27
35
const arg = args [ i ] ;
28
36
@@ -31,32 +39,51 @@ async function main() {
31
39
continue ;
32
40
}
33
41
34
- if ( parsingFlags && arg === "--config" && i + 1 < args . length ) {
35
- configPath = args [ ++ i ] ;
36
- continue ;
37
- }
42
+ if ( parsingFlags ) {
43
+ // Parse a file path to an MCP servers' config file where each server has:
44
+ // - Server type (sse, streamable-http, or stdio)
45
+ // - Server URL (for sse/streamable-http)
46
+ // - Command and args (for stdio)
47
+ // - Environment variables
48
+ if ( arg === "--config" && i + 1 < args . length ) {
49
+ configPath = args [ ++ i ] ;
50
+ continue ;
51
+ }
38
52
39
- if ( parsingFlags && arg === "--server" && i + 1 < args . length ) {
40
- serverName = args [ ++ i ] ;
41
- continue ;
42
- }
53
+ // Parse a server name to use from the relevant config file
54
+ if ( arg === "--server" && i + 1 < args . length ) {
55
+ serverName = args [ ++ i ] ;
56
+ continue ;
57
+ }
43
58
44
- if ( parsingFlags && arg === "-e" && i + 1 < args . length ) {
45
- const envVar = args [ ++ i ] ;
46
- const equalsIndex = envVar . indexOf ( "=" ) ;
59
+ // Process any environment variables (in addition to those provided in the config file)
60
+ // CLI env vars will override those in the config file - handled below
61
+ // Format: -e KEY=VALUE or -e KEY (empty value)
62
+ if ( arg === "-e" && i + 1 < args . length ) {
63
+ const envVar = args [ ++ i ] ;
64
+ const equalsIndex = envVar . indexOf ( "=" ) ;
65
+
66
+ if ( equalsIndex !== - 1 ) {
67
+ const key = envVar . substring ( 0 , equalsIndex ) ;
68
+ const value = envVar . substring ( equalsIndex + 1 ) ;
69
+ envVars [ key ] = value ;
70
+ } else {
71
+ envVars [ envVar ] = "" ;
72
+ }
73
+ continue ;
74
+ }
75
+ }
47
76
48
- if ( equalsIndex !== - 1 ) {
49
- const key = envVar . substring ( 0 , equalsIndex ) ;
50
- const value = envVar . substring ( equalsIndex + 1 ) ;
51
- envVars [ key ] = value ;
77
+ // If a config file isn't provided, then an explicit command (and args) can be provided instead
78
+ // eg. node //some/path/to/a/build/index.js
79
+ if ( ! configProvided ) {
80
+ // Set the first argument as the command to run
81
+ if ( ! command ) {
82
+ command = arg ;
52
83
} else {
53
- envVars [ envVar ] = "" ;
84
+ // If a command has already been provided, then the remaining args as passed to the command
85
+ mcpServerArgs . push ( arg ) ;
54
86
}
55
- // If loading a config file, we don't need to pass the command or args
56
- } else if ( ! command && ! configPath ) {
57
- command = arg ;
58
- } else {
59
- mcpServerArgs . push ( arg ) ;
60
87
}
61
88
}
62
89
@@ -110,6 +137,11 @@ async function main() {
110
137
111
138
// Build server arguments based on config or command line
112
139
let serverArgs = [ ] ;
140
+
141
+ // Environment variables precedence:
142
+ // 1. Command line env vars (-e flag) take highest precedence
143
+ // 2. Config file env vars are next
144
+ // 3. System environment variables are lowest precedence
113
145
let envVarsToPass = { ...envVars } ;
114
146
115
147
let serverEnv = {
0 commit comments