UNPKG

memory-engineering-mcp

Version:

🧠 AI Memory System powered by MongoDB Atlas & Voyage AI - Autonomous memory management with zero manual work

67 lines 2.64 kB
import { MongoClient } from 'mongodb'; import { logger } from '../utils/logger.js'; let client = null; let db = null; export async function connectToMongoDB() { const uri = process.env.MONGODB_URI; if (!uri) { throw new Error('MONGODB_URI environment variable is not set'); } try { client = new MongoClient(uri, { // Connection pool settings maxPoolSize: parseInt(process.env.MONGODB_MAX_POOL_SIZE || '100'), minPoolSize: parseInt(process.env.MONGODB_MIN_POOL_SIZE || '5'), // Retry and reliability settings (official MongoDB driver options) retryWrites: true, // Enable retryable writes retryReads: true, // Enable retryable reads serverSelectionTimeoutMS: 30000, // 30s server selection timeout connectTimeoutMS: 30000, // 30s connection timeout socketTimeoutMS: 0, // No socket timeout (recommended for long operations) heartbeatFrequencyMS: 10000, // 10s server monitoring interval // Additional reliability options maxIdleTimeMS: 300000, // 5min max idle time for connections waitQueueTimeoutMS: 10000, // 10s wait queue timeout }); await client.connect(); const dbName = process.env.MEMORY_ENGINEERING_DB || 'memory_engineering'; db = client.db(dbName); // Verify connection await db.admin().ping(); logger.info(`🌐 MONGODB NEURAL LINK ESTABLISHED: ${dbName} database online!`); } catch (error) { logger.error('💀 MONGODB CONNECTION EXPLODED!', error); throw error; } } export async function closeMongoDBConnection() { if (client) { try { await client.close(); logger.info('🚪 MONGODB CONNECTION TERMINATED - Brain offline'); } catch (error) { logger.error('⚠️ MONGODB DISCONNECT FAILED - Connection stuck!', error); } } } export function getDb() { if (!db) { throw new Error('Database not connected. Call connectToMongoDB() first.'); } return db; } export function getMongoClient() { if (!client) { throw new Error('MongoDB client not connected. Call connectToMongoDB() first.'); } return client; } export function getMemoryCollection() { const collectionName = process.env.MEMORY_ENGINEERING_COLLECTION || 'memory_engineering_documents'; return getDb().collection(collectionName); } // Re-export original functions for backward compatibility export { connectToMongoDB as connectDB }; //# sourceMappingURL=connection.js.map