#!/usr/bin/env node
import { program } from 'commander';
import { loadConfig, validateConfig, getConfigDir, ConfigError } from './config.js';
import { buildTools } from './tools.js';
import { startStdioServer } from './index.js';
program
.name('instruction-mcp')
.description('MCP server that serves markdown instructions as tools')
.version('0.1.0');
program
.command('start', { isDefault: true })
.description('Start the MCP server (stdio mode)')
.option('-c, --config <path>', 'Path to instruction.yaml', 'instruction.yaml')
.action(async (options) => {
await startStdioServer({ configPath: options.config });
});
program
.command('validate')
.description('Validate config without starting server')
.option('-c, --config <path>', 'Path to instruction.yaml', 'instruction.yaml')
.action((options) => {
try {
const config = loadConfig(options.config);
const configDir = getConfigDir(options.config);
const errors = validateConfig(config, configDir);
if (errors.length > 0) {
console.error('Validation failed:');
for (const error of errors) {
console.error(` [${error.tool}] ${error.message}`);
}
process.exit(1);
}
const toolCount = Object.keys(config.tools).length;
const instructionCount = Object.values(config.tools).reduce(
(sum, tool) => sum + Object.keys(tool.instructions).length,
0
);
console.log('Config is valid!');
console.log(` Tools: ${toolCount}`);
console.log(` Instructions: ${instructionCount}`);
} catch (err) {
if (err instanceof ConfigError) {
console.error(`Error: ${err.message}`);
process.exit(1);
}
throw err;
}
});
program
.command('list')
.description('List all tools and their instructions')
.option('-c, --config <path>', 'Path to instruction.yaml', 'instruction.yaml')
.option('-j, --json', 'Output as JSON')
.action((options) => {
try {
const config = loadConfig(options.config);
const tools = buildTools(config);
if (options.json) {
console.log(JSON.stringify(tools, null, 2));
return;
}
for (const [name, tool] of Object.entries(config.tools)) {
console.log(`\n${name}`);
console.log(` Title: ${tool.title}`);
console.log(` Description: ${tool.description}`);
if (tool.default) {
console.log(` Default: ${tool.default}`);
}
console.log(' Instructions:');
for (const [key, filepath] of Object.entries(tool.instructions)) {
const marker = tool.default === key ? ' (default)' : '';
console.log(` - ${key}: ${filepath}${marker}`);
}
}
} catch (err) {
if (err instanceof ConfigError) {
console.error(`Error: ${err.message}`);
process.exit(1);
}
throw err;
}
});
program.parse();