"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const setup_1 = require("./utils/setup");
const server_1 = require("./server");
const defaultContext_1 = require("./templates/defaultContext");
const config_service_1 = require("./services/config.service");
const port_1 = require("./utils/port");
const mcp_adapter_service_1 = require("./features/mcp/services/mcp-adapter.service");
const contextFileUtil_1 = require("./features/mcp/services/contextFileUtil");
const path_1 = __importDefault(require("path"));
/**
* CLI tool for managing the Context Injection MCP server
*/
commander_1.program
.name('context-injection-mcp')
.description('MCP server for injecting development context into Claude conversations')
.version('1.0.0');
commander_1.program
.command('setup')
.description('Set up the project with default configuration and context')
.action(() => {
(0, setup_1.setupProject)();
});
commander_1.program
.command('start')
.description('Start the MCP server')
.option('-c, --config <path>', 'Path to configuration file')
.option('-f, --force', 'Force kill any process using the configured port', false)
.option('-p, --port <port>', 'Override the port to use')
.action(async (options) => {
console.log('Starting Context Injection MCP server...');
// If a specific port is provided via CLI, update the config
if (options.port) {
const port = parseInt(options.port, 10);
if (!isNaN(port) && port > 0 && port < 65536) {
const configService = new config_service_1.ConfigService(options.config);
configService.updateConfig({ port });
console.log(`Port set to ${port} from command line argument`);
}
else {
console.error('Invalid port number provided. Using configuration value instead.');
}
}
const server = new server_1.ContextInjectionServer(options.config);
await server.start({ forceKill: options.force });
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\nShutting down server...');
server.stop();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\nShutting down server...');
server.stop();
process.exit(0);
});
});
commander_1.program
.command('mcp-start')
.description('Start as an MCP adapter for Claude Desktop')
.option('-c, --config <path>', 'Path to configuration file')
.option('-p, --port <port>', 'Override the port to use')
.option('--context-path <path>', 'Override the context file path')
.action((options) => {
console.error('Starting Context Injection MCP adapter for Claude Desktop...');
try {
let configPath = options.config;
// Update config if context path is provided
if (options.contextPath) {
const configService = new config_service_1.ConfigService(configPath);
configService.updateConfig({ contextPath: options.contextPath });
console.error(`Context path set to ${options.contextPath}`);
}
const configService = new config_service_1.ConfigService(configPath);
const config = configService.getConfig();
// Ensure context file exists
if (!(0, contextFileUtil_1.ensureContextFile)(config.contextPath)) {
console.error('ERROR: Could not ensure context file. MCP adapter will start, but context injection may not work.');
}
const mcpAdapter = new mcp_adapter_service_1.McpAdapterService(configPath);
mcpAdapter.start();
}
catch (error) {
console.error('Failed to start MCP adapter:', error instanceof Error ? error.message : String(error));
process.exit(1);
}
});
commander_1.program
.command('create-context')
.description('Create a new default context file')
.option('-o, --output <path>', 'Output path for the context file', path_1.default.join(process.cwd(), 'context.json'))
.action((options) => {
(0, defaultContext_1.createDefaultContextFile)(options.output);
});
commander_1.program
.command('config')
.description('Configure the MCP server')
.option('--kg-reference', 'Enable Knowledge Graph reference in context')
.option('--no-kg-reference', 'Disable Knowledge Graph reference in context')
.option('--kg-text <text>', 'Set Knowledge Graph reference text')
.option('--port <port>', 'Set server port')
.option('--auto-inject', 'Enable automatic context injection')
.option('--manual-inject', 'Disable automatic context injection')
.option('--show', 'Show current configuration')
.action((options) => {
const configService = new config_service_1.ConfigService();
let config = configService.getConfig();
let updated = false;
if (options.show) {
console.log('Current configuration:');
console.log(JSON.stringify(config, null, 2));
console.log('\nKnowledge Graph Reference:', config.knowledgeGraphReference ? 'ENABLED' : 'DISABLED');
console.log('Knowledge Graph Reference Text:', config.knowledgeGraphReferenceText);
console.log('Port:', config.port);
console.log('Auto Inject:', config.autoInject ? 'ENABLED' : 'DISABLED');
console.log('Trigger Keywords:', config.triggerKeywords.join(', '));
return;
}
if (options.kgReference !== undefined) {
config.knowledgeGraphReference = options.kgReference;
updated = true;
console.log(`Knowledge Graph reference ${options.kgReference ? 'ENABLED' : 'DISABLED'}`);
}
if (options.kgText) {
config.knowledgeGraphReferenceText = options.kgText;
updated = true;
console.log(`Knowledge Graph reference text set to: "${options.kgText}"`);
}
if (options.port) {
const port = parseInt(options.port, 10);
if (!isNaN(port) && port > 0 && port < 65536) {
config.port = port;
updated = true;
console.log(`Server port set to: ${port}`);
}
else {
console.error('Invalid port number. Port must be between 1 and 65535');
}
}
if (options.autoInject) {
config.autoInject = true;
updated = true;
console.log('Automatic context injection ENABLED');
}
if (options.manualInject) {
config.autoInject = false;
updated = true;
console.log('Automatic context injection DISABLED (will use trigger keywords)');
}
if (updated) {
configService.updateConfig(config);
console.log('Configuration updated successfully');
}
else {
console.log('No configuration changes made. Use --show to see current config or provide options to change.');
}
});
commander_1.program
.command('check-port')
.description('Check if a port is in use')
.option('-p, --port <port>', 'Port to check', '3000')
.action(async (options) => {
const port = parseInt(options.port, 10);
if (isNaN(port)) {
console.error('Invalid port number');
return;
}
try {
const pid = await (0, port_1.findProcessUsingPort)(port);
if (pid) {
console.log(`Port ${port} is in use by process ID: ${pid}`);
console.log('You can start the server with a different port using:');
console.log(` npm run start -- --port <new-port>`);
console.log('Or force kill the process using:');
console.log(` npm run start -- --force`);
}
else {
console.log(`Port ${port} is available and ready to use`);
}
}
catch (error) {
console.error('Error checking port:', error instanceof Error ? error.message : String(error));
}
});
// Handle unknown commands
commander_1.program.on('command:*', () => {
console.error(`\nInvalid command: ${commander_1.program.args.join(' ')}`);
console.error('See --help for a list of available commands.');
process.exit(1);
});
// Parse command-line arguments
commander_1.program.parse(process.argv);
// Show help if no arguments provided
if (process.argv.length === 2) {
commander_1.program.outputHelp();
}
//# sourceMappingURL=cli.js.map