Merge branch 'main' into main
This commit is contained in:
commit
80ab4dcc28
1 changed files with 1022 additions and 118 deletions
|
|
@ -16,6 +16,7 @@ type Args = {
|
|||
cli: boolean;
|
||||
transport?: "stdio" | "sse" | "streamable-http";
|
||||
serverUrl?: string;
|
||||
headers?: Record<string, string>;
|
||||
};
|
||||
|
||||
type CliOptions = {
|
||||
|
|
@ -25,6 +26,7 @@ type CliOptions = {
|
|||
cli?: boolean;
|
||||
transport?: string;
|
||||
serverUrl?: string;
|
||||
header?: Record<string, string>;
|
||||
};
|
||||
|
||||
type ServerConfig =
|
||||
|
|
@ -131,6 +133,9 @@ async function runCli(args: Args): Promise<void> {
|
|||
// Build CLI arguments
|
||||
const cliArgs = [cliPath];
|
||||
|
||||
// Add target URL/command first
|
||||
cliArgs.push(args.command, ...args.args);
|
||||
|
||||
// Add transport flag if specified
|
||||
if (args.transport && args.transport !== "stdio") {
|
||||
// Convert streamable-http back to http for CLI mode
|
||||
|
|
@ -139,8 +144,12 @@ async function runCli(args: Args): Promise<void> {
|
|||
cliArgs.push("--transport", cliTransport);
|
||||
}
|
||||
|
||||
// Add command and remaining args
|
||||
cliArgs.push(args.command, ...args.args);
|
||||
// Add headers if specified
|
||||
if (args.headers) {
|
||||
for (const [key, value] of Object.entries(args.headers)) {
|
||||
cliArgs.push("--header", `${key}: ${value}`);
|
||||
}
|
||||
}
|
||||
|
||||
await spawnPromise("node", cliArgs, {
|
||||
env: { ...process.env, ...args.envArgs },
|
||||
|
|
@ -209,6 +218,30 @@ function parseKeyValuePair(
|
|||
return { ...previous, [key as string]: val };
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
|
@ -235,7 +268,13 @@ function parseArgs(): Args {
|
|||
.option("--server <n>", "server name from config file")
|
||||
.option("--cli", "enable CLI mode")
|
||||
.option("--transport <type>", "transport type (stdio, sse, http)")
|
||||
.option("--server-url <url>", "server URL for SSE/HTTP transport");
|
||||
.option("--server-url <url>", "server URL for SSE/HTTP transport")
|
||||
.option(
|
||||
"--header <headers...>",
|
||||
'HTTP headers as "HeaderName: Value" pairs (for HTTP/SSE transports)',
|
||||
parseHeaderPair,
|
||||
{},
|
||||
);
|
||||
|
||||
// Parse only the arguments before --
|
||||
program.parse(preArgs);
|
||||
|
|
@ -288,6 +327,7 @@ function parseArgs(): Args {
|
|||
envArgs: { ...(config.env || {}), ...(options.e || {}) },
|
||||
cli: options.cli || false,
|
||||
transport: "stdio",
|
||||
headers: options.header,
|
||||
};
|
||||
} else if (config.type === "sse" || config.type === "streamable-http") {
|
||||
return {
|
||||
|
|
@ -297,6 +337,7 @@ function parseArgs(): Args {
|
|||
cli: options.cli || false,
|
||||
transport: config.type,
|
||||
serverUrl: config.url,
|
||||
headers: options.header,
|
||||
};
|
||||
} else {
|
||||
// Backwards compatibility: if no type field, assume stdio
|
||||
|
|
@ -306,6 +347,7 @@ function parseArgs(): Args {
|
|||
envArgs: { ...((config as any).env || {}), ...(options.e || {}) },
|
||||
cli: options.cli || false,
|
||||
transport: "stdio",
|
||||
headers: options.header,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -327,6 +369,7 @@ function parseArgs(): Args {
|
|||
cli: options.cli || false,
|
||||
transport: transport as "stdio" | "sse" | "streamable-http" | undefined,
|
||||
serverUrl: options.serverUrl,
|
||||
headers: options.header,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue