UNPKG

mongodb-memory-bank-mcp-v2

Version:

MongoDB-powered Memory Bank MCP server with hybrid search capabilities for AI assistants

321 lines (293 loc) • 13.9 kB
import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js'; import { initTool } from './init.js'; import { readTool } from './read.js'; import { updateTool } from './update.js'; import { searchTool } from './search.js'; import { syncTool } from './sync.js'; import { generatePRPTool } from './generate-prp.js'; import { executePRPTool } from './execute-prp.js'; import { logger } from '../utils/logger.js'; export function setupTools(server) { // Register list tools handler server.setRequestHandler(ListToolsRequestSchema, async (_request) => { return { tools: [ { name: 'memory_bank/init', description: `šŸŽÆ Initialize Memory Bank - Your AI's Persistent Context System! CREATES: - MongoDB collection for your project (isolated by projectId) - 6 core memory files with AI-optimized templates - Vector and text search indexes for hybrid search - Configuration for project isolation BENEFITS: - AI remembers EVERYTHING across sessions - MongoDB hybrid search finds patterns instantly - No more "context window" limitations - Perfect for Context Engineering workflows! EXAMPLE: memory_bank/init šŸ’” NEXT STEPS: After init, update memory files with project details, then run memory_bank/sync!`, 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)', }, }, }, }, { name: 'memory_bank/read', description: `šŸ“– Read Memory Bank Files - Access Your AI's Knowledge! WHAT YOU CAN READ: - Core memory files (projectbrief.md, activeContext.md, etc.) - PRP blueprints (prp_[name].md) - Any file stored in the memory bank EXAMPLES: - memory_bank/read --fileName "projectbrief.md" - memory_bank/read --fileName "prp_user-auth.md" - memory_bank/read --fileName "systemPatterns.md" šŸ’” TIP: Use memory_bank/search to discover files when you don't know exact names!`, inputSchema: { type: 'object', properties: { fileName: { type: 'string', description: 'Name of the memory file to read (e.g., projectbrief.md)', pattern: '^[a-zA-Z0-9-_]+\\.md$' }, projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, }, required: ['fileName'], }, }, { name: 'memory_bank/update', description: `šŸ“ Update Memory Bank Files - Keep Your AI Context Fresh! CORE MEMORY FILES (6 foundational documents): - projectbrief.md - Project goals and scope - productContext.md - Why this exists, problems solved - activeContext.md - Current work focus - systemPatterns.md - Architecture decisions - techContext.md - Technologies and setup - progress.md - What works, what's next EXAMPLES: - memory_bank/update --fileName "activeContext.md" --content "[current focus]" - memory_bank/update --fileName "progress.md" --content "[latest status]" šŸ”„ AFTER UPDATING: Run memory_bank/sync to regenerate embeddings!`, inputSchema: { type: 'object', properties: { fileName: { type: 'string', description: 'Name of the memory file to update', pattern: '^[a-zA-Z0-9-_]+\\.md$' }, content: { type: 'string', description: 'New content for the memory file', minLength: 1 }, projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, }, required: ['fileName', 'content'], }, }, { name: 'memory_bank/search', description: `šŸ” Search Memory Bank - The BRAIN of Context Engineering! **THE CROWN JEWEL**: MongoDB $rankFusion hybrid search finds EVERYTHING instantly! - 🧠 70% Semantic Understanding (what concepts mean) - šŸ“ 30% Keyword Matching (exact words you type) - šŸš€ One search to rule them ALL! **WHEN TO USE**: - Find existing patterns: "How did we implement authentication?" - Discover similar features: "Show me all validation approaches" - Research before building: "Find examples of user management" - Context gathering: "What do we know about dark mode?" **EXAMPLES**: - memory_bank/search --query "authentication patterns" - memory_bank/search --query "dark mode toggle" - memory_bank/search --query "validation gates" - memory_bank/search --query "user interface patterns" --limit 20 šŸ’Ž **This is your RESEARCH SUPERPOWER - use it constantly for Context Engineering!**`, inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', minLength: 1 }, projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, limit: { type: 'number', description: 'Maximum number of results (default: 10, max: 50)', default: 10, minimum: 1, maximum: 50 }, searchType: { type: 'string', enum: ['hybrid', 'vector', 'text'], description: 'Type of search to perform (default: hybrid)', default: 'hybrid', }, }, required: ['query'], }, }, { name: 'memory_bank/sync', description: `⚔ Sync Memory Bank - Power Up Your Context Engine! **WHAT IT DOES**: - 🧠 Generates Voyage AI embeddings (1024-dimension vectors) - šŸ”— Creates MongoDB vector + text search indexes - šŸ“Š Updates cross-references between memory files - šŸš€ Activates MongoDB $rankFusion hybrid search **WHEN TO RUN**: - āœ… After memory_bank/update (always!) - āœ… After memory_bank/init (to activate search) - āœ… When search results seem stale - āœ… Before starting Context Engineering workflow **EXAMPLE**: memory_bank/sync šŸ”„ **CRITICAL**: Without sync, search won't work! This powers the entire Context Engineering brain.`, inputSchema: { type: 'object', properties: { projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, forceRegenerate: { type: 'boolean', description: 'Force regeneration of all embeddings (default: false)', default: false, }, }, }, }, { name: 'memory_bank/generate-prp', description: `🧠 Context Engineering Phase 1: Research & PRP Generation šŸ¤– **AI ASSISTANT TRIGGER**: When user says "I want to add/build/create/implement", START HERE! **DEVELOPMENT REQUESTS THAT TRIGGER THIS TOOL**: - "I want to add [feature]" → Use this tool FIRST - "Create a [component/page]" → Use this tool FIRST - "Build [functionality]" → Use this tool FIRST - "Implement [system]" → Use this tool FIRST - "How do I build..." → Use this tool FIRST **THE RESEARCH WORKFLOW** (DO NOT SKIP!): 1. šŸ” **Pattern Discovery**: Use memory_bank/search to find similar features 2. 🌐 **External Research**: Gather docs, examples, best practices 3. 🧠 **ULTRATHINK**: Synthesize ALL findings into comprehensive context 4. šŸ“ **PRP Creation**: Store complete implementation guide via memory_bank/update **RESULT**: A bulletproof PRP (Product Requirements Prompt) with ALL context needed for ONE-PASS implementation success! āž”ļø **NEXT**: Use memory_bank/execute-prp to implement the generated PRP.`, inputSchema: { type: 'object', properties: { request: { type: 'string', description: 'Your simple feature request in natural language (e.g., "I want a dark mode toggle")', minLength: 3 }, projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, }, }, }, { name: 'memory_bank/execute-prp', description: `⚔ Context Engineering Phase 2: Execute PRP with Validation Loops šŸŽÆ **IMPLEMENTATION POWERHOUSE**: Takes a research-backed PRP and implements it PERFECTLY! **THE EXECUTION WORKFLOW**: 1. šŸ“– **Load PRP**: Gets complete implementation context from memory_bank/read 2. 🧠 **ULTRATHINK**: Plans strategy based on discovered patterns 3. ⚔ **Execute**: Implements following EXACT patterns from research 4. āœ… **Validate**: Runs ALL validation gates (TypeScript, tests, integration) 5. šŸ”„ **Self-Correct**: Fixes failures automatically and retries 6. šŸŽ‰ **Complete**: Continues until ALL gates pass! **VALIDATION GATES**: - āœ… TypeScript compilation (zero errors) - āœ… Unit tests (>80% coverage) - āœ… Integration tests (end-to-end) - āœ… Performance benchmarks (no regression) **AUTO-DETECTS**: Latest PRP if not specified ā¬…ļø **PREVIOUS**: memory_bank/generate-prp (run that first!)`, inputSchema: { type: 'object', properties: { prp: { type: 'string', description: 'PRP name to execute (auto-detects latest if not specified)', }, projectPath: { type: 'string', description: 'Project directory path (defaults to current directory)', }, force: { type: 'boolean', description: 'Force execution even with low confidence PRP', default: false, }, }, }, }, ], }; }); // Register call tool handler server.setRequestHandler(CallToolRequestSchema, async (request) => { try { const { name, arguments: args } = request.params; logger.info(`Tool called: ${name}`, args); switch (name) { case 'memory_bank/init': return await initTool(args); case 'memory_bank/read': return await readTool(args); case 'memory_bank/update': return await updateTool(args); case 'memory_bank/search': return await searchTool(args); case 'memory_bank/sync': return await syncTool(args); case 'memory_bank/generate-prp': return await generatePRPTool(args); case 'memory_bank/execute-prp': return await executePRPTool(args); default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { logger.error('Tool execution error:', error); return { content: [ { type: 'text', text: `Error: ${error instanceof Error ? error.message : 'Unknown error occurred'}`, }, ], }; } }); } //# sourceMappingURL=index.js.map