UNPKG

whodis-mcp-server

Version:

Whodis MCP Server for checking the availability of domain names using WHOIS lookups.

125 lines (124 loc) 6.24 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PACKAGE_NAME = exports.VERSION = exports.Logger = exports.config = void 0; exports.startServer = startServer; const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js"); const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js"); const logger_util_js_1 = require("./utils/logger.util.js"); Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_util_js_1.Logger; } }); const config_util_js_1 = require("./utils/config.util.js"); Object.defineProperty(exports, "config", { enumerable: true, get: function () { return config_util_js_1.config; } }); const error_util_js_1 = require("./utils/error.util.js"); // Import handleCliError const constants_util_js_1 = require("./utils/constants.util.js"); const index_js_1 = require("./cli/index.js"); // Import tools const domain_availability_tool_js_1 = __importDefault(require("./tools/domain-availability.tool.js")); // Create file-level logger const indexLogger = logger_util_js_1.Logger.forContext('index.ts'); // Log initialization at debug level indexLogger.debug('Whodis MCP server module loaded'); let serverInstance = null; let transportInstance = null; /** * Start the MCP server with the specified transport mode * * @param mode The transport mode to use (stdio or sse) * @returns Promise that resolves to the server instance when started successfully */ async function startServer(mode = 'stdio') { const serverLogger = logger_util_js_1.Logger.forContext('index.ts', 'startServer'); // Load configuration serverLogger.info('Starting Whodis MCP server initialization...'); config_util_js_1.config.load(); serverLogger.info('Configuration loaded successfully'); // Enable debug logging if DEBUG is set if (config_util_js_1.config.getBoolean('DEBUG')) { serverLogger.debug('Debug mode enabled via config'); } else { serverLogger.debug('Debug mode is disabled'); } serverLogger.debug(`Resolved DEBUG config value: ${config_util_js_1.config.get('DEBUG')}`); serverLogger.info(`Initializing Whodis MCP Server v${constants_util_js_1.VERSION}`); serverInstance = new mcp_js_1.McpServer({ name: constants_util_js_1.PACKAGE_NAME, version: constants_util_js_1.VERSION, }); if (mode === 'stdio') { serverLogger.info('Using STDIO transport for MCP communication'); transportInstance = new stdio_js_1.StdioServerTransport(); } else { // Keep this check in case SSE is supported later serverLogger.error('SSE mode is not currently supported'); throw (0, error_util_js_1.createUnexpectedError)('SSE mode is not supported yet'); } // Register tools serverLogger.info('Registering MCP tools...'); domain_availability_tool_js_1.default.registerTools(serverInstance); serverLogger.debug('Registered Domain Availability tool'); serverLogger.info('All tools registered successfully'); try { serverLogger.info(`Connecting to ${mode.toUpperCase()} transport...`); await serverInstance.connect(transportInstance); serverLogger.info('Whodis MCP server started successfully and ready to process requests'); return serverInstance; } catch (err) { serverLogger.error(`Failed to start MCP server`, err); // Ensure graceful exit process.exit(1); } } /** * Main entry point - determines whether to run in CLI or server mode. */ async function main() { const mainLogger = logger_util_js_1.Logger.forContext('index.ts', 'main'); try { // Load configuration early config_util_js_1.config.load(); mainLogger.debug(`Initial DEBUG environment variable: ${process.env.DEBUG}`); mainLogger.debug(`Initial Config DEBUG value: ${config_util_js_1.config.get('DEBUG')}`); mainLogger.debug(`Resolved DEBUG boolean: ${config_util_js_1.config.getBoolean('DEBUG')}`); // Check if arguments indicate CLI mode (more than just 'node' and 'script.js') if (process.argv.length > 2) { mainLogger.info('Starting in CLI mode'); // Pass all arguments after the script name to the CLI runner await (0, index_js_1.runCli)(process.argv); // Pass the full argv array mainLogger.info('CLI execution completed'); } else { // MCP Server mode: Start server with default STDIO mainLogger.info('No CLI arguments provided. Starting in MCP server mode (stdio)'); await startServer('stdio'); // Explicitly specify stdio mainLogger.info('Server is now running and listening for MCP requests'); // Keep the process alive in server mode // This typically happens implicitly if the transport keeps the event loop busy. // For stdio, it waits for input. Add a mechanism if needed. // Example: new Promise(() => {}); // Keep alive indefinitely } } catch (error) { // Use the CLI error handler for consistency, even if it's a server startup error (0, error_util_js_1.handleCliError)(error); } } // Execute main only if the script is run directly // Using check `require.main === module` is commonjs style. // For ESM compatibility, a different check might be needed if this was pure ESM. // Since package.json specifies "type": "commonjs", this check is correct. if (require.main === module) { main().catch((err) => { // This catch is a fallback, main() should handle errors internally indexLogger.error('Unhandled error in main execution:', err); process.exit(1); // Ensure exit on unhandled error }); } var constants_util_js_2 = require("./utils/constants.util.js"); Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return constants_util_js_2.VERSION; } }); Object.defineProperty(exports, "PACKAGE_NAME", { enumerable: true, get: function () { return constants_util_js_2.PACKAGE_NAME; } });