Merge branch 'main' into cmgriffing/fix-cli-truncated-output

This commit is contained in:
Ola Hungerford 2025-08-28 20:40:58 -07:00 • committed by GitHub
commit d3fcedc81a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -41,11 +41,13 @@ type Args = {
toolName?: string;
toolArg?: Record<string, JsonValue>;
transport?: "sse" | "stdio" | "http";
headers?: Record<string, string>;
};
function createTransportOptions(
target: string[],
transport?: "sse" | "stdio" | "http",
headers?: Record<string, string>,
): TransportOptions {
if (target.length === 0) {
throw new Error(
@ -92,11 +94,16 @@ function createTransportOptions(
command: isUrl ? undefined : command,
args: isUrl ? undefined : commandArgs,
url: isUrl ? command : undefined,
headers,
};
}
async function callMethod(args: Args): Promise<void> {
const transportOptions = createTransportOptions(args.target, args.transport);
const transportOptions = createTransportOptions(
args.target,
args.transport,
args.headers,
);
const transport = createTransport(transportOptions);
const client = new Client({
name: "inspector-cli",
@ -197,6 +204,30 @@ function parseKeyValuePair(
return { ...previous, [key as string]: parsedValue };
}
function parseHeaderPair(
value: string,
previous: Record<string, string> = {},
): Record<string, string> {
const colonIndex = value.indexOf(":");
if (colonIndex === -1) {
throw new Error(
`Invalid header format: ${value}. Use "HeaderName: Value" format.`,
);
}
const key = value.slice(0, colonIndex).trim();
const val = value.slice(colonIndex + 1).trim();
if (key === "" || val === "") {
throw new Error(
`Invalid header format: ${value}. Use "HeaderName: Value" format.`,
);
}
return { ...previous, [key]: val };
}
function parseArgs(): Args {
const program = new Command();
@ -276,12 +307,24 @@ function parseArgs(): Args {
}
return value as "sse" | "http" | "stdio";
},
)
//
// HTTP headers
//
.option(
"--header <headers...>",
'HTTP headers as "HeaderName: Value" pairs (for HTTP/SSE transports)',
parseHeaderPair,
{},
);
// Parse only the arguments before --
program.parse(preArgs);
const options = program.opts() as Omit<Args, "target">;
const options = program.opts() as Omit<Args, "target"> & {
header?: Record<string, string>;
};
let remainingArgs = program.args;
// Add back any arguments that came after --
@ -296,6 +339,7 @@ function parseArgs(): Args {
return {
target: finalArgs,
...options,
headers: options.header, // commander.js uses 'header' field, map to 'headers'
};
}
@ -307,8 +351,9 @@ async function main(): Promise<void> {
try {
const args = parseArgs();
await callMethod(args);
// Explicitly exit to ensure process terminates in CI
process.exit(0);
// Let Node.js naturally exit instead of force-exiting
// process.exit(0) was causing stdout truncation
} catch (error) {
handleError(error);
}