UNPKG

@shirokuma-library/mcp-knowledge-base

Version:

MCP server for AI-powered knowledge management with semantic search, graph analysis, and automatic enrichment

116 lines (115 loc) 3.92 kB
import fs from 'fs/promises'; import path from 'path'; export class ConfigManager { schema; constructor() { this.schema = { SHIROKUMA_DATA_DIR: { type: 'string', required: false, description: 'Data directory path (defaults to .shirokuma/data-prod)', default: '.shirokuma/data-prod' }, SHIROKUMA_EXPORT_DIR: { type: 'string', required: false, description: 'Export directory path', default: 'docs/export' } }; } getConfig() { const config = {}; for (const [key, field] of Object.entries(this.schema)) { const value = process.env[key] || field.default; if (value !== undefined) { config[key] = value; } } return config; } exportConfig(format) { const config = this.getConfig(); if (format === 'json') { const exportData = {}; for (const [key, value] of Object.entries(config)) { const field = this.schema[key]; if (field?.sensitive && value) { exportData[key] = '***REDACTED***'; } else { exportData[key] = value; } } exportData._metadata = { exported_at: new Date().toISOString(), version: '0.9.0' }; return JSON.stringify(exportData, null, 2); } let envContent = '# Environment configuration exported\n'; envContent += `# Exported at: ${new Date().toISOString()}\n\n`; for (const [key, value] of Object.entries(config)) { const field = this.schema[key]; if (field?.description) { envContent += `# ${field.description}\n`; } if (field?.sensitive && value) { envContent += `${key}=***REDACTED***\n`; } else if (value !== undefined) { envContent += `${key}=${value}\n`; } envContent += '\n'; } return envContent; } validateConfig() { const errors = []; for (const [key, field] of Object.entries(this.schema)) { const value = process.env[key]; if (field.required && !value) { errors.push(`${key} is required`); } if (field.type === 'enum' && value && field.values) { if (!field.values.includes(value)) { errors.push(`${key} must be one of: ${field.values.join(', ')}`); } } } return { valid: errors.length === 0, errors }; } getSchema() { return this.schema; } async createEnvExample() { const examplePath = path.join(process.cwd(), '.env.example'); let content = '# Example environment configuration\n'; content += '# Copy this file to .env and update with your values\n\n'; for (const [key, field] of Object.entries(this.schema)) { if (field.description) { content += `# ${field.description}\n`; } if (field.type === 'enum' && field.values) { content += `# Values: ${field.values.join(', ')}\n`; } if (field.sensitive) { content += `# ${key}=your-secret-key-here\n`; } else if (field.default) { content += `${key}=${field.default}\n`; } else { content += `# ${key}=\n`; } content += '\n'; } await fs.writeFile(examplePath, content, { encoding: 'utf-8', mode: 0o644 }); } }