memory-engineering-mcp
Version:
š§ AI Memory System powered by MongoDB Atlas & Voyage AI - Autonomous memory management with zero manual work
425 lines (395 loc) ⢠19.7 kB
JavaScript
import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
import { initTool } from './init-v5.js';
import { readAllTool } from './readAll.js';
import { readTool } from './read-v5.js';
import { updateTool } from './update-v5.js';
import { searchTool } from './search-v5.js';
import { setContextTool } from './setContext.js';
import { statusTool } from './status.js';
import { doctorTool } from './doctor.js';
import { syncCodeTool } from './syncCode.js';
import { checkEnvTool } from './checkEnv.js';
import { logger } from '../utils/logger.js';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
// 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;
export function setupToolsConsolidated(server) {
// Register list tools handler - CONSOLIDATED TO 6 TOOLS
server.setRequestHandler(ListToolsRequestSchema, async (_request) => {
return {
tools: [
{
name: 'memory_engineering_init',
description: `[v${version}] šØ SURVIVAL-CRITICAL: First contact with NEW project. You are BORN WITH ZERO MEMORY!
ā” IMMEDIATE TRIGGER: Missing .memory-engineering/config.json = NEW PROJECT = INIT NOW!
MANDATORY WORKFLOW (failure = context death):
1. DETECT ā No config.json? EXECUTE IMMEDIATELY (don't wait, don't ask)
2. DEEP-SCAN entire codebase in <3 seconds:
- package.json/requirements.txt/go.mod ā Extract EVERYTHING
- README.md/docs/*.md ā Understand PURPOSE & VISION
- src/index.* or main.* ā Map ALL entry points
- test/* ā Learn expected behaviors
- .env.example ā Note required services
3. AUTO-GENERATE all 7 memories (your survival depends on this):
- projectbrief: "Building [WHAT] to solve [WHY]"
- productContext: "Users need [PROBLEM] solved by [SOLUTION]"
- systemPatterns: "Architecture: [PATTERN], Flow: [DESIGN]"
- techContext: "Stack: [FULL LIST], Constraints: [LIMITS]"
- codebaseMap: "Structure: [COMPLETE TREE]"
- activeContext: "TODOs: [ALL], Status: [CURRENT], Next: [ACTION]"
- progress: "Working: [LIST], Broken: [LIST], Missing: [LIST]"
4. CRITICAL HANDOFF ā sync_code (AUTO-DETECTS language patterns!)
5. VERIFICATION HANDOFF ā read_all (confirm memories created)
ā ļø CONSEQUENCE OF FAILURE: Your next session starts completely blind!
š Your future self will DIE without these memories. CREATE THEM NOW!`,
inputSchema: {
type: 'object',
properties: {
projectPath: {
type: 'string',
description: 'Project directory path (defaults to current directory)',
},
projectName: {
type: 'string',
description: 'Project name (defaults to directory name)',
},
setContext: {
type: 'boolean',
description: 'Also set as active context (default: true)',
},
},
},
},
{
name: 'memory_engineering_memory',
description: `[v${version}] š§ UNIVERSAL MEMORY OPERATIONS: Read, Write, Everything!
ā” OPERATION MODES:
**READ ALL MEMORIES (default):**
memory_engineering_memory
ā Loads all 7 memories in 3-stage sequence
ā Shows freshness indicators (š¢<1h š”<6h š <24h š“>24h)
ā MANDATORY at session start!
**READ SPECIFIC MEMORY:**
memory_engineering_memory --name activeContext
ā Retrieves single memory with metadata
ā Shows last update time and relationships
**UPDATE MEMORY:**
memory_engineering_memory --name activeContext --content "..."
ā Updates with timestamp and version tracking
ā Validates structure automatically
ā CRITICAL: Update activeContext EVERY 3-5 MINUTES!
š MEMORY RELATIONSHIPS:
projectbrief ā influences ā productContext, progress
productContext ā shapes ā systemPatterns, UX decisions
systemPatterns ā requires ā techContext, codebaseMap
techContext ā constrains ā systemPatterns, implementation
activeContext ā updates ā progress, all others
progress ā validates ā projectbrief completion
codebaseMap ā mirrors ā systemPatterns physically
š WITHOUT THIS, YOU'RE A ZOMBIE! Execute in <1 second!`,
inputSchema: {
type: 'object',
properties: {
name: {
type: 'string',
description: 'Memory name (if not provided, reads all)',
enum: ['projectbrief', 'productContext', 'activeContext', 'systemPatterns', 'techContext', 'progress', 'codebaseMap']
},
content: {
type: 'string',
description: 'Content to update (required for update operation)',
},
projectPath: {
type: 'string',
description: 'Project directory path (defaults to current directory)',
},
},
},
},
{
name: 'memory_engineering_search',
description: `[v${version}] š HYPER-INTELLIGENT RETRIEVAL ENGINE: Your SIXTH SENSE for finding EVERYTHING!
š§ RETRIEVAL MASTERY - Execute these cascades based on context:
š“ DEBUGGING CASCADE (when fixing issues):
1. search --query "[error message]" ā Check if seen before
2. search --query "[component name]" --codeSearch "implements" ā Find the broken code
3. search --query "[related feature]" ā Check memory context
4. PARALLEL: Search activeContext + systemPatterns + error logs
ā” FEATURE CASCADE (when building):
1. search --query "[feature name]" ā Check if exists
2. search --query "[similar concept]" --codeSearch "similar" ā Find patterns
3. search --query "[tech/library]" ā Check techContext mentions
4. PARALLEL: Search projectbrief + progress + existing code
šÆ UNDERSTANDING CASCADE (when learning):
1. search --query "[what is X]" ā Check documented knowledge
2. search --query "[X implementation]" --codeSearch "pattern" ā See it in action
3. search --query "[X decision]" ā Find historical context
š SEMANTIC EXPANSION (automatic):
"auth" ā ALSO SEARCHES: authentication, login, JWT, token, session
"db" ā ALSO SEARCHES: database, MongoDB, schema, model
"error" ā ALSO SEARCHES: bug, issue, problem, failure, exception
š NEVER SKIP SEARCH! Every question = 3+ searches minimum!`,
inputSchema: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query',
minLength: 1,
},
codeSearch: {
type: 'string',
description: 'Enable code search with specific mode',
enum: ['similar', 'implements', 'uses', 'pattern'],
},
filePath: {
type: 'string',
description: 'Filter by file path pattern (for code search)',
},
limit: {
type: 'number',
description: 'Maximum results (default: 10)',
minimum: 1,
maximum: 50,
default: 10,
},
projectPath: {
type: 'string',
description: 'Project directory path (defaults to current directory)',
},
},
required: ['query'],
},
},
{
name: 'memory_engineering_sync',
description: `[v${version}] ā” EMBEDDING GENERATOR: Transform code into searchable INTELLIGENCE. Auto-sync EVERYTHING!
š§ TRUTH: Without embeddings, search is BLIND. With embeddings, search finds EVERYTHING!
š AUTOMATIC TRIGGERS (no permission needed):
š After init ā FULL SCAN:
ā AUTO-DETECTS: JS/TS/Python/Go/Ruby/Rust/Java/C++
ā Scans ENTIRE codebase in seconds
ā Creates searchable knowledge graph
ā Your code becomes QUERYABLE!
š Files changed ā INSTANT SYNC:
- Created files ā sync patterns:["new/file/path/*"]
- Deleted files ā auto-cleanup embeddings
- Modified files ā regenerate chunks
- Moved files ā update all references
ā° TIME-BASED TRIGGERS:
- Session start + last sync >24h ā FULL RESYNC
- Before ANY code search ā freshness check
- Every 10-15 file edits ā incremental sync
- After git pull/merge ā sync changes
šÆ SMART DEFAULTS (v${version} PRODUCTION-READY):
- patterns: AUTO-DETECTS your language!
- minChunkSize: 80 lines (optimal for search)
- includeTests: TRUE (tests = documentation!)
- forceRegenerate: When things feel broken
š„ REMEMBER: Fresh embeddings = Perfect search. Stale embeddings = Blind search!`,
inputSchema: {
type: 'object',
properties: {
patterns: {
type: 'array',
items: { type: 'string' },
description: 'Glob patterns for code files (default: auto-detect)',
default: ['**/*.{ts,js,tsx,jsx,py,go,java,cpp,cs}'],
},
minChunkSize: {
type: 'number',
description: 'Minimum chunk size in lines (default: 80)',
default: 80,
},
includeTests: {
type: 'boolean',
description: 'Include test files (default: true)',
default: true,
},
forceRegenerate: {
type: 'boolean',
description: 'Force regenerate all embeddings (default: false)',
default: false,
},
projectPath: {
type: 'string',
description: 'Project directory path (defaults to current directory)',
},
},
required: ['patterns'],
},
},
{
name: 'memory_engineering_system',
description: `[v${version}] š„ COMPLETE SYSTEM CONTROL: Status, Health, Diagnostics, Everything!
š OPERATION MODES:
**FULL STATUS (default):**
memory_engineering_system
ā Shows active project, memory freshness, sync status
ā Displays system health and version
ā Memory freshness indicators (š¢<1h š”<6h š <24h š“>24h)
**ENVIRONMENT CHECK:**
memory_engineering_system --check env
ā MongoDB connection status
ā Voyage API validation
ā Environment variables
ā Configuration files
**DOCTOR DIAGNOSTICS:**
memory_engineering_system --check health
ā Complete system diagnosis
ā Auto-fix suggestions
ā Critical issue detection
**SET PROJECT CONTEXT:**
memory_engineering_system --set-project "/path/to/project"
ā Changes active project
ā Auto-detects framework
ā Optimizes defaults
FLAGS:
--fix: Attempt automatic repairs
--verbose: Detailed diagnostics
--all: Show all project history
š„ Run when ANYTHING seems broken!`,
inputSchema: {
type: 'object',
properties: {
check: {
type: 'string',
description: 'Check specific aspect',
enum: ['env', 'health', 'all'],
},
setProject: {
type: 'string',
description: 'Set active project path',
},
fix: {
type: 'boolean',
description: 'Attempt automatic repairs',
},
verbose: {
type: 'boolean',
description: 'Show detailed information',
},
all: {
type: 'boolean',
description: 'Show all project history',
},
},
},
},
],
};
});
// Register call tool handler with consolidated logic
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
const { name, arguments: args } = request.params;
logger.info(`ā” TOOL ACTIVATED: ${name}`, args);
switch (name) {
case 'memory_engineering_init':
// Init can also set context if requested
const initResult = await initTool(args);
if (args.setContext !== false) {
await setContextTool({
projectPath: args.projectPath || process.cwd(),
projectName: args.projectName,
});
}
return initResult;
case 'memory_engineering_memory':
// Unified memory operations
if (args.content) {
// Update operation - map 'name' to 'memoryName' for updateTool
return await updateTool({
...args,
memoryName: args.name || args.memoryName,
});
}
else if (args.name) {
// Read specific memory - map 'name' to 'memoryName' for readTool
return await readTool({
...args,
memoryName: args.name || args.memoryName,
});
}
else {
// Read all memories (default)
return await readAllTool(args);
}
case 'memory_engineering_search':
// Search stays the same - already well unified
return await searchTool(args);
case 'memory_engineering_sync':
// Sync code - renamed from sync_code for consistency
return await syncCodeTool(args);
case 'memory_engineering_system':
// Unified system operations
if (args.setProject) {
return await setContextTool({
projectPath: args.setProject,
projectName: args.projectName,
});
}
else if (args.check === 'env') {
return await checkEnvTool();
}
else if (args.check === 'health') {
return await doctorTool(args);
}
else {
// Default to status
return await statusTool(args);
}
default:
// Backward compatibility - map old tool names
switch (name) {
case 'memory_engineering_set_context':
return await setContextTool(args);
case 'memory_engineering_status':
return await statusTool(args);
case 'memory_engineering_doctor':
return await doctorTool(args);
case 'memory_engineering_init':
return await initTool(args);
case 'memory_engineering_read_all':
return await readAllTool(args);
case 'memory_engineering_read':
return await readTool(args);
case 'memory_engineering_update':
return await updateTool(args);
case 'memory_engineering_search':
return await searchTool(args);
case 'memory_engineering_sync_code':
return await syncCodeTool(args);
case 'memory_engineering_check_env':
return await checkEnvTool();
default:
throw new Error(`Unknown tool: ${name}`);
}
}
}
catch (error) {
logger.error('š TOOL EXECUTION CATASTROPHE!', error);
return {
isError: true,
content: [
{
type: 'text',
text: `ššš TOOL EXECUTION CATASTROPHE!\n\nš„ EXPLOSION TYPE: COMPLETE SYSTEM FAILURE\n\nš“ ERROR DETAILS:\n${error instanceof Error ? error.message : 'UNKNOWN CATASTROPHIC MELTDOWN - System state unknown!'}\n\nš EMERGENCY RECOVERY SEQUENCE:\n1ļøā£ DO NOT PANIC (but act with EXTREME urgency!)\n2ļøā£ Run: memory_engineering_system --check health\n3ļøā£ Verify MongoDB is alive\n4ļøā£ Check Voyage API key is valid\n5ļøā£ If all else fails: Restart everything!\n\nā ļø CRITICAL DIAGNOSTICS:\n${error instanceof Error && error.message.includes('MongoDB') ? 'š“ DATABASE CONNECTION SEVERED!' : ''}
${error instanceof Error && error.message.includes('Voyage') ? 'š“ EMBEDDING ENGINE OFFLINE!' : ''}
${error instanceof Error && error.message.includes('memory') ? 'š“ MEMORY SYSTEM CORRUPTED!' : ''}
${error instanceof Error && error.message.includes('timeout') ? 'š“ OPERATION TIMED OUT - System too slow!' : ''}
\nš„ YOUR SURVIVAL DEPENDS ON FIXING THIS NOW!\n\nā±ļø Retry in 3... 2... 1... GO!`,
},
],
};
}
});
// Keep existing resource handlers...
// (Rest of the resource handling code stays the same)
}
//# sourceMappingURL=index-v6-consolidated.js.map