mcpipe
Version:
Decorate stdio MCP servers with debugging and other capabilities.
102 lines (101 loc) • 4.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const wrapper_1 = require("./wrapper");
const path_1 = __importDefault(require("path"));
function printUsage() {
const scriptName = path_1.default.basename(process.argv[1]);
console.error(`Usage: ${scriptName} [--debug] [--name <prefix>] [--env-file <path>] <command> [args...]`);
console.error("\nOptions:");
console.error(" -d, --debug Enable debug logging of MCP message flows");
console.error(" -n, --name <prefix> Specify a custom name/prefix for the MCP server");
console.error(" -e, --env-file <path> Specify a custom path to the environment file");
console.error("\nArguments:");
console.error(" <command> The command to run the MCP server");
console.error(" [args...] Arguments for the MCP server command");
console.error("\nNote: All options must precede the command.");
}
function main() {
const argv = process.argv.slice(2); // Remove node executable and script path
let name = undefined;
let customEnvFilePath = undefined;
let debug = false;
let commandIndex = 0;
let error = false;
// Check for and parse any arguments provided to mcpipe itself
while (commandIndex < argv.length) {
const arg = argv[commandIndex];
if (arg === '--debug' || arg === '-d') {
if (debug) {
console.error("Error: --debug option specified more than once.");
error = true;
break;
}
debug = true;
commandIndex += 1;
}
else if (arg === '--name' || arg === '-n') {
if (name !== undefined) {
console.error("Error: --name option specified more than once.");
error = true;
break;
}
if (commandIndex + 1 >= argv.length || argv[commandIndex + 1].startsWith('-')) {
console.error(`Error: ${arg} option requires a value.`);
error = true;
break;
}
name = argv[commandIndex + 1];
commandIndex += 2;
}
else if (arg === '--env-file' || arg === '-e') {
if (customEnvFilePath !== undefined) {
console.error("Error: --env-file option specified more than once.");
error = true;
break;
}
if (commandIndex + 1 >= argv.length || argv[commandIndex + 1].startsWith('-')) {
console.error(`Error: ${arg} option requires a path.`);
error = true;
break;
}
customEnvFilePath = argv[commandIndex + 1];
commandIndex += 2;
}
else {
// First non-option argument marks the start of the command
break;
}
}
const commandArgs = argv.slice(commandIndex);
if (error || commandArgs.length === 0) {
if (!error) {
console.error("Error: No command specified.");
}
printUsage();
process.exit(1);
}
const commandExecutable = commandArgs[0];
const actualArgsForCommand = commandArgs.slice(1);
const envFilePath = customEnvFilePath || (0, index_1.findEnvFilePath)();
if (envFilePath) {
const hasLoadedEnvironmentVariables = (0, index_1.loadEnvironmentVariablesFromFile)(envFilePath);
if (!hasLoadedEnvironmentVariables) {
console.error(`Error: Failed to load specified environment file: ${envFilePath}`);
process.exit(1);
}
}
// Execute or wrap the command - use the original arguments
if (name) {
const namespace = name + '_';
(0, wrapper_1.startWrappedServer)(namespace, commandExecutable, actualArgsForCommand, debug);
}
else {
(0, index_1.executeCommand)(commandExecutable, actualArgsForCommand);
}
}
main();