UNPKG

mcp-context-engineering

Version:

The intelligent context optimization system for AI coding assistants. Built with Cole's PRP methodology, Context Portal knowledge graphs, and production-ready MongoDB architecture.

274 lines (273 loc) 12.5 kB
import { MongoClient } from 'mongodb'; import { z } from 'zod'; // MongoDB connection configuration schema export const MongoConfigSchema = z.object({ uri: z.string().url(), database: z.string(), options: z.object({ maxPoolSize: z.number().default(10), serverSelectionTimeoutMS: z.number().default(5000), socketTimeoutMS: z.number().default(45000), maxIdleTimeMS: z.number().default(30000), retryWrites: z.boolean().default(true), retryReads: z.boolean().default(true) }).optional() }); /** * MongoDB Connection Manager following official MongoDB MCP patterns */ export class MongoConnectionManager { client = null; database = null; config; isConnected = false; constructor(config) { this.config = MongoConfigSchema.parse(config); } /** * Establish connection to MongoDB * Following MongoDB MCP server connection patterns */ async connect() { if (this.isConnected && this.client) { return; } try { const options = { ...this.config.options, // Vector search specific options maxPoolSize: this.config.options?.maxPoolSize || 10, serverSelectionTimeoutMS: this.config.options?.serverSelectionTimeoutMS || 5000, socketTimeoutMS: this.config.options?.socketTimeoutMS || 45000, }; this.client = new MongoClient(this.config.uri, options); await this.client.connect(); // Test the connection await this.client.db(this.config.database).admin().ping(); this.database = this.client.db(this.config.database); this.isConnected = true; console.log(`Connected to MongoDB database: ${this.config.database}`); // Ensure indexes for vector search await this.ensureIndexes(); } catch (error) { console.error('MongoDB connection failed:', error); throw new Error(`Failed to connect to MongoDB: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Ensure database connection is active * Following MongoDB MCP server patterns */ async ensureConnected() { if (!this.isConnected || !this.client || !this.database) { await this.connect(); } try { // Ping to verify connection is still active await this.database.admin().ping(); } catch (error) { console.log('Connection lost, reconnecting...'); this.isConnected = false; await this.connect(); } } /** * Get database instance */ getDatabase() { if (!this.database) { throw new Error('Database not connected. Call connect() first.'); } return this.database; } /** * Get client instance */ getClient() { if (!this.client) { throw new Error('Client not connected. Call connect() first.'); } return this.client; } /** * Check if connected */ isConnectedToDatabase() { return this.isConnected && this.client !== null && this.database !== null; } /** * Close connection */ async disconnect() { if (this.client) { await this.client.close(); this.client = null; this.database = null; this.isConnected = false; console.log('Disconnected from MongoDB'); } } /** * Ensure required indexes for vector search and performance * Enhanced with Context Portal patterns and MongoDB best practices */ async ensureIndexes() { if (!this.database) return; try { console.log('Creating database indexes...'); // 1. Context patterns collection indexes const contextCollection = this.database.collection('context_patterns'); // Text search index for PRP methodology and knowledge graph await contextCollection.createIndex({ "prp_methodology.implementation.goal": "text", "prp_methodology.implementation.technical_requirements": "text", "knowledge_graph.decisions.title": "text", "knowledge_graph.decisions.description": "text", "metadata.pattern_type": "text", "metadata.tags": "text" }, { name: "context_text_search" }); // Metadata indexes for filtering and performance await contextCollection.createIndex({ "metadata.pattern_type": 1 }); await contextCollection.createIndex({ "metadata.complexity": 1 }); await contextCollection.createIndex({ "metadata.project_types": 1 }); await contextCollection.createIndex({ "metadata.tech_stacks": 1 }); await contextCollection.createIndex({ "metadata.created_at": -1 }); await contextCollection.createIndex({ "effectiveness_metrics.overall_success_rate": -1 }); // Agent optimization indexes await contextCollection.createIndex({ "agent_optimizations.cursor.effectiveness_score": -1 }); await contextCollection.createIndex({ "agent_optimizations.windsurf.effectiveness_score": -1 }); await contextCollection.createIndex({ "agent_optimizations.claude_code.effectiveness_score": -1 }); // 2. Knowledge Graph Collections (Context Portal patterns) // Decisions collection const decisionsCollection = this.database.collection('decisions'); await decisionsCollection.createIndex({ "workspace_id": 1, "project_id": 1 }); await decisionsCollection.createIndex({ "status": 1, "date": -1 }); await decisionsCollection.createIndex({ "tags": 1 }); await decisionsCollection.createIndex({ "title": "text", "description": "text", "rationale": "text" }); // Progress entries collection const progressCollection = this.database.collection('progress_entries'); await progressCollection.createIndex({ "workspace_id": 1, "project_id": 1 }); await progressCollection.createIndex({ "status": 1, "date": -1 }); await progressCollection.createIndex({ "parent_id": 1 }); // Hierarchical relationships // System patterns collection const systemPatternsCollection = this.database.collection('system_patterns'); await systemPatternsCollection.createIndex({ "workspace_id": 1 }); await systemPatternsCollection.createIndex({ "pattern_name": "text", "description": "text" }); // Context links collection (Knowledge Graph relationships) const contextLinksCollection = this.database.collection('context_links'); await contextLinksCollection.createIndex({ "workspace_id": 1, "source_item_type": 1, "source_item_id": 1 }); await contextLinksCollection.createIndex({ "workspace_id": 1, "target_item_type": 1, "target_item_id": 1 }); await contextLinksCollection.createIndex({ "relationship_type": 1, "timestamp": -1 }); // 3. Workspaces collection const workspacesCollection = this.database.collection('workspaces'); await workspacesCollection.createIndex({ "workspace_id": 1 }, { unique: true }); await workspacesCollection.createIndex({ "project_id": 1 }); await workspacesCollection.createIndex({ "metadata.last_accessed": -1 }); // 4. Project contexts collection const projectCollection = this.database.collection('project_contexts'); await projectCollection.createIndex({ "project_dna.project_id": 1 }, { unique: true }); await projectCollection.createIndex({ "metadata.last_accessed": -1 }); await projectCollection.createIndex({ "metadata.active": 1 }); // 5. Context history collection (Context Portal versioning) const historyCollection = this.database.collection('context_history'); await historyCollection.createIndex({ "workspace_id": 1, "project_id": 1 }); await historyCollection.createIndex({ "context_type": 1, "context_item_id": 1, "version": -1 }); await historyCollection.createIndex({ "timestamp": -1 }); await historyCollection.createIndex({ "change_source": 1, "change_type": 1 }); // 6. Vector embeddings collection (MongoDB Atlas Vector Search) const embeddingsCollection = this.database.collection('embeddings'); await embeddingsCollection.createIndex({ "workspace_id": 1, "project_id": 1 }); await embeddingsCollection.createIndex({ "item_type": 1, "item_id": 1 }); await embeddingsCollection.createIndex({ "metadata.tech_stack": 1 }); await embeddingsCollection.createIndex({ "metadata.effectiveness_score": -1 }); await embeddingsCollection.createIndex({ "metadata.created_at": -1 }); // Vector search index (requires MongoDB Atlas - created separately) // This would be created through MongoDB Atlas UI or Atlas Admin API: // { // "name": "vector_index", // "definition": { // "fields": [ // { // "type": "vector", // "path": "embedding", // "numDimensions": 1024, // "similarity": "cosine" // } // ] // } // } // 7. Effectiveness tracking collection const effectivenessCollection = this.database.collection('effectiveness_tracking'); await effectivenessCollection.createIndex({ "analytics.pattern_id": 1 }); await effectivenessCollection.createIndex({ "measurements.timestamp": -1 }); await effectivenessCollection.createIndex({ "measurements.agent_type": 1 }); // 8. Agent profiles collection const agentProfilesCollection = this.database.collection('agent_profiles'); await agentProfilesCollection.createIndex({ "agent_type": 1 }, { unique: true }); await agentProfilesCollection.createIndex({ "performance_metrics.overall_effectiveness": -1 }); console.log('✅ All database indexes created successfully'); // Log index creation summary const collections = await this.database.listCollections().toArray(); console.log(`📊 Database collections: ${collections.length}`); } catch (error) { console.warn('⚠️ Warning: Could not create some indexes:', error); // Don't throw error as vector search indexes require MongoDB Atlas // and some indexes might already exist } } /** * Health check for monitoring */ async healthCheck() { const startTime = Date.now(); try { await this.ensureConnected(); const collections = await this.database.listCollections().toArray(); const latency = Date.now() - startTime; return { connected: true, database: this.config.database, collections: collections.map(c => c.name), latency }; } catch (error) { return { connected: false, database: this.config.database, collections: [], latency: Date.now() - startTime }; } } } // Singleton instance let connectionManager = null; /** * Get singleton connection manager instance */ export function getConnectionManager(config) { if (!connectionManager) { if (!config) { // Use default environment configuration if no config provided const { mongoConfig } = require('../../config/environment.js'); connectionManager = new MongoConnectionManager(mongoConfig); } else { connectionManager = new MongoConnectionManager(config); } } return connectionManager; } /** * Initialize and connect to MongoDB */ export async function initializeDatabase(config) { const manager = getConnectionManager(config); await manager.connect(); return manager; }