"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const tools_1 = require("./tools");
const logger_1 = require("../../../utils/logger");
function main(serverName, toolGroupsToLoad) {
    return __awaiter(this, void 0, void 0, function* () {
        const server = new mcp_js_1.McpServer({
            name: serverName,
            version: "1.0.0"
        }, {
            capabilities: {
                tools: {}
            }
        });
        let toolNames = "";
        for (let toolGroup of toolGroupsToLoad) {
            if ((0, tools_1.narrowToolGroup)(toolGroup)) {
                for (let toolSpec of tools_1.tools[toolGroup]) {
                    server.tool(toolSpec.name, toolSpec.description, toolSpec.inputSchema, toolSpec.cb);
                    toolNames += toolSpec.name + '\n';
                }
            }
            else {
                logger_1.logger.error("Unrecognized toolgroup: ", toolGroup);
            }
        }
        try {
            const transport = new stdio_js_1.StdioServerTransport();
            yield server.connect(transport);
            logger_1.logger.info(`MCP server connected: ${serverName} with tools: \n${toolNames}`);
        }
        catch (err) {
            logger_1.logger.error("Could not connect to mcp server");
            process.exit(1);
        }
    });
}
const name = process.argv[2];
if (!name) {
    console.error("Name not passed");
    process.exit(1);
}
const toolGroupsToLoad = process.argv.slice(3);
main(name, toolGroupsToLoad);
