Parse the flags shared by add (scope, transport, env, headers, oauth).
(args: string[])
| 126 | |
| 127 | /** Parse the flags shared by add (scope, transport, env, headers, oauth). */ |
| 128 | function parseAddFlags(args: string[]): { |
| 129 | scope: McpScope |
| 130 | transport: (typeof VALID_TRANSPORTS)[number] |
| 131 | env: Record<string, string> |
| 132 | headers: Record<string, string> |
| 133 | oauth: boolean | { scope: string } |
| 134 | positional: string[] |
| 135 | } { |
| 136 | let scope: McpScope = "project" |
| 137 | let transport: (typeof VALID_TRANSPORTS)[number] = "stdio" |
| 138 | const env: Record<string, string> = {} |
| 139 | const headers: Record<string, string> = {} |
| 140 | let oauth: boolean | { scope: string } = false |
| 141 | const positional: string[] = [] |
| 142 | |
| 143 | // Flags must come before the server name. Once we hit the first positional |
| 144 | // (the server name), everything after it — including args that look like |
| 145 | // flags (-y, -f, --foo) — becomes the server's command/args. This matches |
| 146 | // Claude Code and lets stdio servers take their own flags. |
| 147 | let sawPositional = false |
| 148 | for (let i = 0; i < args.length; i++) { |
| 149 | const arg = args[i]! |
| 150 | if (sawPositional) { |
| 151 | // A "--" immediately after the server name is the separator between |
| 152 | // orbcode's flags and the server's command (e.g. `add name -- npx`), |
| 153 | // matching Claude Code's `claude mcp add <name> -- <command>`. Consume |
| 154 | // it so it isn't mistaken for the command. A later "--" is kept as a |
| 155 | // literal arg for the server command. |
| 156 | if (arg === "--" && positional.length === 1) continue |
| 157 | positional.push(arg) |
| 158 | continue |
| 159 | } |
| 160 | if (arg === "-s" || arg === "--scope") { |
| 161 | const value = args[++i] |
| 162 | if (!value || !VALID_SCOPES.includes(value as McpScope)) { |
| 163 | throw new Error(`Invalid scope "${value ?? ""}". Valid: ${VALID_SCOPES.join(", ")}`) |
| 164 | } |
| 165 | scope = value as McpScope |
| 166 | } else if (arg === "-t" || arg === "--transport") { |
| 167 | const value = args[++i] |
| 168 | if (!value || !VALID_TRANSPORTS.includes(value as (typeof VALID_TRANSPORTS)[number])) { |
| 169 | throw new Error(`Invalid transport "${value ?? ""}". Valid: ${VALID_TRANSPORTS.join(", ")}`) |
| 170 | } |
| 171 | transport = value as (typeof VALID_TRANSPORTS)[number] |
| 172 | } else if (arg === "-e" || arg === "--env") { |
| 173 | const value = args[++i] |
| 174 | if (!value) throw new Error("Missing value for --env") |
| 175 | const [k, v] = parseKeyValue(value) |
| 176 | env[k] = v |
| 177 | } else if (arg === "--header") { |
| 178 | const value = args[++i] |
| 179 | if (!value) throw new Error("Missing value for --header") |
| 180 | const [k, v] = parseKeyValue(value) |
| 181 | headers[k] = v |
| 182 | } else if (arg === "--oauth") { |
| 183 | oauth = true |
| 184 | } else if (arg === "--oauth-scope") { |
| 185 | const value = args[++i] |
no test coverage detected