import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
type CallToolRequest,
type ListToolsRequest,
} 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: Server): void {
// Register list tools handler - CONSOLIDATED TO 6 TOOLS
server.setRequestHandler(ListToolsRequestSchema, async (_request: ListToolsRequest) => {
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: CallToolRequest) => {
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)
}