UNPKG

whodis-mcp-server

Version:

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

148 lines (147 loc) 6.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.config = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const logger_util_js_1 = require("./logger.util.js"); const dotenv_1 = __importDefault(require("dotenv")); const os_1 = __importDefault(require("os")); const constants_util_js_1 = require("./constants.util.js"); // Import the constant /** * Configuration loader that handles multiple sources with priority: * 1. Direct ENV pass (process.env) * 2. .env file in project root * 3. Global config file at $HOME/.mcp/configs.json */ class ConfigLoader { /** * Create a new ConfigLoader instance * @param packageName The package name to use for global config lookup */ constructor(packageName) { this.configLoaded = false; this.packageName = packageName; } /** * Load configuration from all sources with proper priority */ load() { const methodLogger = logger_util_js_1.Logger.forContext('utils/config.util.ts', 'load'); if (this.configLoaded) { methodLogger.debug('Configuration already loaded, skipping'); return; } methodLogger.debug('Loading configuration...'); // Priority 3: Load from global config file this.loadFromGlobalConfig(); // Priority 2: Load from .env file this.loadFromEnvFile(); // Priority 1: Direct ENV pass is already in process.env // No need to do anything as it already has highest priority this.configLoaded = true; methodLogger.debug('Configuration loaded successfully'); } /** * Load configuration from .env file in project root */ loadFromEnvFile() { const methodLogger = logger_util_js_1.Logger.forContext('utils/config.util.ts', 'loadFromEnvFile'); try { // Use dotenv.config({ path: path.resolve(process.cwd(), '.env') }) for robustness const result = dotenv_1.default.config({ path: path_1.default.resolve(process.cwd(), '.env'), }); if (result.error) { // It's okay if .env doesn't exist if (result.error.code !== 'ENOENT') { methodLogger.warn(`Error reading .env file: ${result.error.message}`); } else { methodLogger.debug('No .env file found.'); } return; } if (result.parsed && Object.keys(result.parsed).length > 0) { methodLogger.debug('Loaded configuration from .env file'); } else { methodLogger.debug('.env file found but is empty or only contains comments.'); } } catch (error) { methodLogger.error('Error processing .env file', error); } } /** * Load configuration from global config file at $HOME/.mcp/configs.json */ loadFromGlobalConfig() { const methodLogger = logger_util_js_1.Logger.forContext('utils/config.util.ts', 'loadFromGlobalConfig'); try { const homedir = os_1.default.homedir(); const globalConfigPath = path_1.default.join(homedir, '.mcp', 'configs.json'); if (!fs_1.default.existsSync(globalConfigPath)) { methodLogger.debug('Global config file not found'); return; } const configContent = fs_1.default.readFileSync(globalConfigPath, 'utf8'); const config = JSON.parse(configContent); if (!config[this.packageName] || !config[this.packageName].environments) { methodLogger.debug(`No configuration found for ${this.packageName} in global config`); return; } const environments = config[this.packageName].environments; let loadedCount = 0; for (const [key, value] of Object.entries(environments)) { // Only set if not already defined in process.env (respecting higher priority) if (process.env[key] === undefined) { process.env[key] = String(value); loadedCount++; } } if (loadedCount > 0) { methodLogger.debug(`Loaded ${loadedCount} variable(s) from global config file for ${this.packageName}`); } else { methodLogger.debug(`Global config found for ${this.packageName}, but all variables were already set by higher priority sources.`); } } catch (error) { methodLogger.error('Error loading global config file', error); } } /** * Get a configuration value * @param key The configuration key * @param defaultValue The default value if the key is not found * @returns The configuration value or the default value */ get(key, defaultValue) { return process.env[key] ?? defaultValue; } /** * Get a boolean configuration value * @param key The configuration key * @param defaultValue The default value if the key is not found * @returns The boolean configuration value or the default value */ getBoolean(key, defaultValue = false) { const value = this.get(key); if (value === undefined || value === null) { return defaultValue; } // Explicitly check for 'false', '0', empty string as false, otherwise treat as true const lowerValue = value.trim().toLowerCase(); if (lowerValue === 'false' || lowerValue === '0' || lowerValue === '') { return false; } // Any other non-empty value is considered true return true; } } // Create and export a singleton instance using the package name constant exports.config = new ConfigLoader(constants_util_js_1.PACKAGE_NAME);