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
JavaScript
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