Skip to content

Commit fcea6a1

Browse files
committed
fix: collect multiple --header flags correctly with Commander.js
Commander.js overwrites repeated options by default, keeping only the last value. Use a custom collector function so -H/--header accumulates into an array. This fixes 'mcpc @s tools-list -H "A: 1" -H "B: 2"' where only the second header was being sent. https://claude.ai/code/session_01N2bVWnSTWbVDMmtY7hJ1Cg
1 parent f0f3ff9 commit fcea6a1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/cli/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ interface HandlerOptions {
6060
full?: boolean;
6161
}
6262

63+
/** Commander option collector for repeated flags (e.g. --header) */
64+
function collectOption(value: string, previous: string[]): string[] {
65+
return [...previous, value];
66+
}
67+
6368
/**
6469
* Extract options from Commander's Command object
6570
* Used by command handlers to get parsed options in consistent format
@@ -81,10 +86,8 @@ function getOptionsFromCommand(command: Command): HandlerOptions {
8186
};
8287

8388
// Only include optional properties if they're present
84-
if (opts.header) {
85-
// Commander stores repeated options as arrays, but single values as strings
86-
// Always convert to array for consistent handling
87-
options.headers = Array.isArray(opts.header) ? opts.header : [opts.header];
89+
if (opts.header && (opts.header as string[]).length > 0) {
90+
options.headers = opts.header as string[];
8891
}
8992
if (opts.timeout) {
9093
const timeout = parseInt(opts.timeout as string, 10);
@@ -311,7 +314,7 @@ function createTopLevelProgram(): Command {
311314
)
312315
.usage('[options] [<@session>] [<command>]')
313316
.option('-j, --json', 'Output in JSON format for scripting')
314-
.option('-H, --header <header>', 'HTTP header (can be repeated)')
317+
.option('-H, --header <header>', 'HTTP header (can be repeated)', collectOption, [])
315318
.option('--verbose', 'Enable debug logging')
316319
.option('--profile <name>', 'OAuth profile for the server ("default" if not provided)')
317320
.option('--schema <file>', 'Validate tool/prompt schema against expected schema')
@@ -773,7 +776,7 @@ function createSessionProgram(): Command {
773776
.name('mcpc <@session>')
774777
.helpOption('-h, --help', 'Display help')
775778
.option('-j, --json', 'Output in JSON format for scripting and code mode')
776-
.option('-H, --header <header>', 'Custom HTTP header (can be repeated)')
779+
.option('-H, --header <header>', 'Custom HTTP header (can be repeated)', collectOption, [])
777780
.option('--verbose', 'Enable debug logging')
778781
.option('--schema <file>', 'Validate tool/prompt schema against expected schema')
779782
.option('--schema-mode <mode>', 'Schema validation mode: strict, compatible (default), ignore')

0 commit comments

Comments
 (0)