@subtotal-inc/mcp
Version:
A command line tool for setting up a Subtotal MCP server
78 lines (77 loc) • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseArgs = parseArgs;
exports.main = main;
const modelcontextprotocol_1 = require("@subtotal-inc/ai-toolkit/modelcontextprotocol");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const colors_1 = require("colors");
const ACCEPTED_ARGS = ['subtotal-secret-key', 'tools'];
function parseArgs(args) {
const options = {};
args.forEach((arg) => {
if (arg.startsWith('--')) {
const [key, value] = arg.slice(2).split('=');
if (key == 'tools') {
options.tools = value.split(',');
}
else if (key == 'subtotal-secret-key') {
options.subtotalSecretKey = value;
}
else {
throw new Error(`Invalid argument: ${key}. Accepted arguments are: ${ACCEPTED_ARGS.join(', ')}`);
}
}
});
// Check if required tools arguments is present
if (!options.tools) {
throw new Error('The --tools arguments must be provided.');
}
// Validate tools against accepted enum values
const acceptedTools = Object.values(modelcontextprotocol_1.Tools);
options.tools.forEach((tool) => {
if (tool == 'all') {
return;
}
const trimmedTool = tool.trim();
if (!acceptedTools.includes(trimmedTool)) {
throw new Error(`Invalid tool: ${tool}. Accepted tools are: ${Object.values(modelcontextprotocol_1.Tools).join(', ')}`);
}
});
// Get Subtotal API Credentials
const subtotalSecretKey = options.subtotalSecretKey || process.env.SUBTOTAL_SECRET_KEY;
if (!subtotalSecretKey) {
throw new Error('Subtotal API key not provided. Please either pass it as an argument --subtotal-secret-key=$SECRET-KEY or set the SUBTOTAL_SECRET_KEY environment variable.');
}
options.subtotalSecretKey = subtotalSecretKey;
return options;
}
function handleError(error) {
console.error((0, colors_1.red)('\n🚨 Error initializing Subtotal MCP server:\n'));
console.error((0, colors_1.yellow)(` ${error.message}\n`));
}
async function main() {
const options = parseArgs(process.argv.slice(2));
// Create the SubtotalAIToolkit instance
const selectedTools = options.tools;
const configuration = { tools: [] };
if (selectedTools.includes('all')) {
configuration.tools = [...Object.values(modelcontextprotocol_1.Tools)];
}
else {
configuration.tools = [...selectedTools];
}
const server = new modelcontextprotocol_1.SubtotalAIToolkit({
secretKey: options.subtotalSecretKey,
configuration: configuration,
});
const transport = new stdio_js_1.StdioServerTransport();
await server.connect(transport);
// We use console.error instead of console.log since console.log will output to stdio, which will confuse the MCP server
console.error((0, colors_1.green)('✅ Subtotal MCP Server running on stdio'));
}
if (require.main === module) {
main().catch((error) => {
handleError(error);
});
}