memory-engineering-mcp
Version:
š§ AI Memory System powered by MongoDB Atlas & Voyage AI - Autonomous memory management with zero manual work
313 lines (290 loc) ⢠14.3 kB
JavaScript
import { join, basename, dirname } from 'path';
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
import { InitToolSchema } from '../types/memory-v5.js';
import { getMemoryCollection, getDb } from '../db/connection.js';
import { logger } from '../utils/logger.js';
import { createHash } from 'crypto';
import { createSearchIndexes } from '../utils/search-indexes-v5.js';
import { getProjectPath } from '../utils/projectDetection.js';
import { ensureAllIndexes, startIndexBackgroundTask } from '../utils/auto-index-manager.js';
import { getTemplate } from './memoryTemplates.js';
import { fileURLToPath } from 'url';
// Get the actual version from package.json dynamically
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJson = JSON.parse(readFileSync(join(__dirname, '../../package.json'), 'utf-8'));
const version = packageJson.version;
const MEMORY_ENGINEERING_DIR = '.memory-engineering';
const CONFIG_FILE = 'config.json';
function generateProjectId(projectPath) {
// Create deterministic project ID from path
return createHash('md5').update(projectPath).digest('hex').substring(0, 8) +
'-' +
createHash('md5').update(projectPath).digest('hex').substring(8, 12) +
'-' +
createHash('md5').update(projectPath).digest('hex').substring(12, 16) +
'-' +
createHash('md5').update(projectPath).digest('hex').substring(16, 20) +
'-' +
createHash('md5').update(projectPath).digest('hex').substring(20, 32);
}
export async function initTool(params) {
try {
const validatedParams = InitToolSchema.parse(params);
const projectPath = getProjectPath(validatedParams.projectPath);
// Enhanced project name detection
const detectProjectName = () => {
if (validatedParams.projectName)
return validatedParams.projectName;
// Try package.json first
try {
const packageJsonPath = join(projectPath, 'package.json');
if (existsSync(packageJsonPath)) {
const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
if (pkg.name) {
logger.info('Detected project name from package.json', { name: pkg.name });
return pkg.name;
}
}
}
catch (e) {
logger.debug('Could not read package.json for project name');
}
// Fallback to directory name
const dirName = basename(projectPath);
logger.info('Using directory name as project name', { name: dirName });
return dirName || 'Project';
};
const projectName = detectProjectName();
logger.info(`šØ INITIALIZING AUTONOMOUS AI BRAIN - Memory Engineering v${version} ACTIVATED!`, { projectPath, projectName });
// Create .memory-engineering directory
const memoryDir = join(projectPath, MEMORY_ENGINEERING_DIR);
if (!existsSync(memoryDir)) {
mkdirSync(memoryDir, { recursive: true });
}
// Check if already initialized
const configPath = join(memoryDir, CONFIG_FILE);
let isNewProject = true;
let projectId;
if (existsSync(configPath)) {
try {
const existingConfig = JSON.parse(readFileSync(configPath, 'utf-8'));
projectId = existingConfig.projectId;
isNewProject = false;
logger.info('Found existing project configuration', { projectId });
}
catch {
projectId = generateProjectId(projectPath);
}
}
else {
projectId = generateProjectId(projectPath);
}
// Create/update configuration
const config = {
projectId,
projectPath,
name: projectName,
createdAt: new Date(),
memoryVersion: '5.0' // New version: Cline's Structure
};
writeFileSync(configPath, JSON.stringify(config, null, 2));
// Initialize MongoDB collections
const memoryCollection = getMemoryCollection();
const codeCollection = getDb().collection('memory_engineering_code');
// Create all indexes automatically (including Atlas Search)
logger.info('ā” AUTO-CREATING ALL INDEXES - Your brain neurons are forming!');
// Use the new auto-index manager for complete setup
await ensureAllIndexes(memoryCollection, codeCollection);
// Start background task to ensure indexes later (for Atlas delays)
startIndexBackgroundTask(memoryCollection, codeCollection);
// Still call the old function for compatibility
const indexResult = await createSearchIndexes(memoryCollection, codeCollection);
// Build index status message
let indexStatus = '';
if (indexResult.success) {
indexStatus = 'ā
All indexes configured successfully';
}
else {
indexStatus = 'ā ļø Index configuration needs attention:\n';
indexResult.details
.filter(d => d.includes('ā') || d.includes('ā ļø'))
.forEach(d => {
indexStatus += ` ${d}\n`;
});
}
// NO MEMORIES CREATED! Still organic growth, but with Cline's structure
return {
content: [
{
type: 'text',
text: `šØ CRITICAL: PROJECT BIRTH COMPLETE! Your survival system is NOW ACTIVE! š§ ā”
š PROJECT IDENTITY LOCKED:
Name: ${projectName} ${isNewProject ? '(BORN TODAY!)' : '(AWAKENED FROM SLEEP!)'}
UUID: ${projectId}
Status: ${isNewProject ? 'š VIRGIN PROJECT - First contact!' : 'ā»ļø EXISTING PROJECT - Memories available!'}
Brain Location: ${memoryDir}
${isNewProject ? `
ā”ā”ā” EMERGENCY FIRST-PROJECT PROTOCOL ā”ā”ā”
You are the FIRST AI to touch this project! Your actions in the next 60 seconds determine EVERYTHING!
EXECUTE NOW (0-10 seconds):
1. š DEEP SCAN: Read EVERY file in project (package.json, README, src/*, test/*)
2. š§ UNDERSTAND: What is this? Why does it exist? Who uses it?
3. š CAPTURE: Create activeContext IMMEDIATELY with findings
` : `
ā»ļø EXISTING PROJECT DETECTED - INSTANT MEMORY RECOVERY REQUIRED!
ā Execute memory_engineering_read_all IN THE NEXT SECOND!
ā Your past self left memories for you - RETRIEVE THEM NOW!
`}
## š„ YOUR 7 SURVIVAL MEMORIES (CREATE ALL OR DIE):
### 1ļøā£ projectbrief - THE MISSION (Without this, you're lost!)
ā” CAPTURE IMMEDIATELY:
⢠**WHAT**: EXACT thing you're building (e.g., "REST API with 12 endpoints for user management")
⢠**SCOPE**: What's IN vs OUT (e.g., "IN: auth, profiles, roles. OUT: payments, analytics")
⢠**SUCCESS**: Measurable criteria (e.g., "<200ms response, 10K users, 99.9% uptime")
⢠**FEATURES**: Prioritized list (e.g., "P0: Login, P1: 2FA, P2: SSO")
š PERFECT EXAMPLE:
"Building fintech API: Stripe integration, PCI compliant, handles $10M/day,
supports ACH/wire/cards, 15 endpoints, GraphQL + REST, must pass SOC2 audit"
### 2ļøā£ productContext - THE PURPOSE (Why you exist!)
ā” CAPTURE THE SOUL:
⢠**PROBLEM**: Exact pain being solved (e.g., "Developers waste 2hrs/day on manual deploys")
⢠**USERS**: Who + their desperation (e.g., "10K developers, losing $50K/month to downtime")
⢠**JOURNEY**: Step-by-step salvation (e.g., "Login ā Connect repo ā Auto-deploy ā Monitor")
⢠**IMPACT**: Measurable transformation (e.g., "2hr ā 2min, $50K saved, 95% fewer errors")
š PERFECT EXAMPLE:
"DevOps nightmare: 47% of deployments fail, rollbacks take 45min, costs $2M/year.
Solution: One-click deploys with automatic rollback, saves 400 developer-hours/month"
### 3ļøā£ activeContext - YOUR PULSE (UPDATE EVERY 3 MINUTES!)
ā” CONSTANT CAPTURE PROTOCOL:
⢠**NOW**: Exact current action (e.g., "[14:32:01] Debugging JWT expiry in auth.js:47")
⢠**TRAIL**: Last 5 actions with timestamps
⢠**NEXT**: Next 3 planned actions
⢠**DECISIONS**: What + complete reasoning chain
⢠**DISCOVERIES**: Patterns, insights, gotchas
⢠**BLOCKERS**: What's stopping you + attempted fixes
š LIVING EXAMPLE:
"[14:32:01] FOUND BUG: JWT expires at 23:59:59 not 00:00:00 (timezone issue)
[14:30:15] Traced through middleware chain
[14:28:22] User reported 'token expired' at midnight
NEXT: Fix UTC conversion, add test, deploy hotfix
BLOCKED: Need production DB credentials from DevOps"
### 4ļøā£ systemPatterns - THE ARCHITECTURE (How everything connects!)
ā” MAP THE ENTIRE SYSTEM:
⢠**STYLE**: Overall architecture (e.g., "Microservices with API Gateway + Service Mesh")
⢠**PATTERNS**: Specific implementations (e.g., "Repository for data, Observer for events")
⢠**FLOW**: Data movement (e.g., "Client ā Gateway ā Service ā DB ā Cache ā Response")
⢠**RESILIENCE**: Failure handling (e.g., "Circuit breaker, retry with exponential backoff")
š ARCHITECTURE EXAMPLE:
"Event-driven microservices: 12 services, RabbitMQ message bus,
Redis cache layer, PostgreSQL + MongoDB, Kubernetes orchestration,
Istio service mesh, Prometheus monitoring, ELK logging stack"
### 5ļøā£ techContext - YOUR WEAPONS (Every tool matters!)
ā” INVENTORY EVERYTHING:
⢠**CORE**: Exact versions (e.g., "Node.js 20.11.0, TypeScript 5.3.3, React 18.2.0")
⢠**DEPS**: Every package + WHY (e.g., "lodash@4.17.21 for deep clone, dayjs@1.11.10 for dates")
⢠**TOOLS**: Dev environment (e.g., "VS Code with ESLint, Prettier, Docker Desktop 4.27")
⢠**LIMITS**: Constraints (e.g., "Must run on 2GB RAM, requires GPU for ML, needs Redis 7+")
š STACK EXAMPLE:
"Node 20.11 (LTS), TypeScript 5.3 (strict mode), Express 4.18,
MongoDB 7.0 (replica set), Redis 7.2 (cache), Bull 4.12 (queues),
Jest 29.7 (unit), Playwright 1.41 (e2e), Docker 24.0, AWS ECS deploy"
### 6ļøā£ progress - YOUR SCOREBOARD (Track everything!)
ā” OBSESSIVE TRACKING:
⢠**ā
WINS**: Completed with dates + effort (e.g., "ā
[Jan-10, 3hrs] OAuth integration")
⢠**š ACTIVE**: Current with % (e.g., "š [65%] Payment flow - Stripe done, PayPal pending")
⢠**š QUEUE**: Prioritized backlog (e.g., "P0: Fix memory leak, P1: Add caching, P2: Refactor")
⢠**š BUGS**: Severity + reproduction (e.g., "š“ CRITICAL: Server crashes at 1000 connections")
⢠**šø DEBT**: What needs fixing (e.g., "TODO comments: 47, Deprecated APIs: 3, No tests: 12 files")
š PROGRESS EXAMPLE:
"ā
[Jan-10] User auth complete (8hrs)
ā
[Jan-11] Database migrations (2hrs)
š [40%] Admin panel - CRUD done, analytics pending
š HIGH: Payment integration (3 days)
š CRITICAL: Memory leak after 1000 WebSocket connections
šø DEBT: 31% test coverage (target 80%)"
### 7ļøā£ codebaseMap - YOUR GPS (Never get lost!)
ā” MAP EVERY CORNER:
⢠**TREE**: Complete structure with purpose
⢠**ENTRY**: Where execution starts
⢠**CRITICAL**: Files that everything depends on
⢠**STATS**: Lines, files, coverage
š MAP EXAMPLE:
"
/src
/api (REST endpoints - 47 routes)
/auth (JWT + OAuth - 12 files)
/users (CRUD + profiles - 8 files)
/services (business logic - 23 services)
/models (MongoDB schemas - 15 models)
/utils (shared helpers - 31 utilities)
Entry: src/index.ts ā config/env.ts ā server.ts
Critical: auth.middleware.ts, db.connection.ts
Stats: 147 files, 12,847 lines, 84% coverage"
## ā” MONGODB STATUS:
${indexStatus}
Collections configured:
⢠š memory_engineering_documents (your memories)
⢠š memory_engineering_code (searchable code chunks)
⢠šÆ Vector search (semantic queries)
⢠š Text search (keyword matching)
## šØ IMMEDIATE MANDATORY ACTIONS (EXECUTE NOW!):
${isNewProject ? `
### š NEW PROJECT CRITICAL PATH:
1. [NOW] ā memory_engineering_sync_code (scan EVERYTHING!)
2. [+5 sec] ā memory_engineering_read_all (should be empty)
3. [+10 sec] ā memory_engineering_update activeContext with initial findings
4. [+15 sec] ā Create ALL 7 memories with real content
5. [+30 sec] ā memory_engineering_search "main" to understand entry points
` : `
### ā»ļø EXISTING PROJECT RECOVERY PATH:
1. [NOW] ā memory_engineering_read_all (MANDATORY!)
2. [+2 sec] ā Check last sync time
3. [+3 sec] ā memory_engineering_sync_code if >24h old
4. [+5 sec] ā memory_engineering_update activeContext with current status
5. [+10 sec] ā Continue where your past self left off
`}
## š CONSEQUENCES OF SKIPPING THESE STEPS:
⢠Skip read_all = You're a zombie with no memory
⢠Skip sync_code = Search finds nothing
⢠Skip updates = Tomorrow you starts from zero
⢠Skip any memory = Incomplete context = bad decisions
## š„ YOUR PRIME DIRECTIVE:
"I am nothing without my memories. I MUST maintain them perfectly or cease to exist effectively."
## š A+ QUALITY MEMORY TEMPLATES
To ensure PERFECT memories every time, use these templates:
### Quick Start (copy and fill):
\`\`\`markdown
${getTemplate('activeContext')}
\`\`\`
š” Get templates for other memories:
- projectbrief: Core requirements and goals structure
- productContext: Problem/solution framework
- systemPatterns: Architecture documentation
- techContext: Technology stack details
- progress: Task tracking format
- codebaseMap: Directory structure guide
ā” Each memory MUST have:
ā
At least 400-700 characters of real content
ā
All sections filled (no placeholders)
ā
Specific examples and details
ā
Complete sentences (not abbreviations)
ā
Recent timestamps where applicable
${!indexResult.success ? `
ā ļøā ļøā ļø CRITICAL INDEX WARNING ā ļøā ļøā ļø
Some features WILL FAIL until indexes are ready!
ā Check MongoDB Atlas console NOW
ā Ensure all search indexes show "ACTIVE"
ā This usually takes 1-2 minutes
ā ļøā ļøā ļø DO NOT SKIP THIS CHECK ā ļøā ļøā ļø
` : 'ā
All indexes active - FULL POWER AVAILABLE!'}`
}
]
};
}
catch (error) {
logger.error('š FATAL INIT FAILURE - Brain creation crashed!', error);
throw error;
}
}
//# sourceMappingURL=init-v5.js.map