mongodb-memory-bank-mcp-v2
Version:
MongoDB-powered Memory Bank MCP server with hybrid search capabilities for AI assistants
126 lines (114 loc) ⢠4.71 kB
JavaScript
import { join } from 'path';
import { readFileSync, existsSync } from 'fs';
import { ExecutePRPSchema } from '../types/memory.js';
import { getMemoryCollection } from '../db/connection.js';
import { logger } from '../utils/logger.js';
import { getContextEngineeringTemplate } from '../services/context-engineering.js';
/**
* Context Engineering Phase 2: Execute PRP with validation loops
* Fetches the original /execute-prp system prompt from MongoDB
*/
export async function executePRPTool(args) {
try {
const params = ExecutePRPSchema.parse(args);
const projectPath = params.projectPath || process.cwd();
// Read project config
const configPath = join(projectPath, '.memory-bank', 'config.json');
if (!existsSync(configPath)) {
return {
content: [{
type: 'text',
text: 'Memory bank not initialized. Run memory_bank/init first.',
}],
};
}
const config = JSON.parse(readFileSync(configPath, 'utf-8'));
// Find the most recent PRP to execute
let prpName = params.prp;
if (!prpName) {
// Auto-detect the most recent PRP
prpName = (await findLatestPRP(config.projectId, projectPath)) || undefined;
if (!prpName) {
return {
content: [{
type: 'text',
text: `No PRP found to execute. Either:
1. Generate a PRP first: memory_bank/generate-prp --request "your request"
2. Specify PRP: memory_bank/execute-prp --prp "prp-name"
Available PRPs can be found via: memory_bank/search --query "prp_"`,
}],
};
}
}
logger.info('Fetching Context Engineering /execute-prp template...');
// Fetch the exact original /execute-prp system prompt from MongoDB
const systemPrompt = await getContextEngineeringTemplate('execute-prp');
logger.info(`š Context Engineering Phase 2: Implementation & Validation`);
return {
content: [{
type: 'text',
text: `š **Context Engineering Phase 2: Implementation & Validation**
**PRP to Execute**: ${prpName}
**Project**: ${config.projectId}
---
## š¤ AI Assistant Instructions
You are now in Context Engineering Phase 2. Follow these instructions EXACTLY:
${systemPrompt}
---
## šÆ Your Task
**PRP File**: ${prpName}
**Steps to Follow**:
1. **Load PRP**: Use \`memory_bank/read --fileName "prp_${prpName}.md"\` to load the PRP
2. **ULTRATHINK**: Plan implementation strategy based on PRP content
3. **Execute Implementation**: Follow the PRP blueprint exactly
4. **Run Validation Gates**: Execute each validation command from the PRP
5. **Self-Correction**: Fix failures and retry until all gates pass
6. **Complete**: Ensure all requirements are met
**Available Tools**:
- \`memory_bank/read\` - Read PRP and memory files
- \`memory_bank/search\` - Find implementation patterns
- \`memory_bank/update\` - Update progress
- Standard coding tools (bash, file operations, etc.)
š **Start implementation NOW following the Context Engineering methodology above!**`,
}],
};
}
catch (error) {
logger.error('PRP execution error:', error);
return {
content: [{
type: 'text',
text: `Error fetching Context Engineering template: ${error instanceof Error ? error.message : 'Unknown error occurred'}`,
}],
};
}
}
// Helper function to find the latest PRP
async function findLatestPRP(projectId, projectPath) {
try {
// First check MongoDB for latest PRP
const collection = getMemoryCollection();
const latestPRP = await collection.findOne({
projectId,
fileName: { $regex: /^prp_.*\.md$/ }
}, { sort: { 'metadata.lastUpdated': -1 } });
if (latestPRP) {
return latestPRP.fileName.replace('prp_', '').replace('.md', '');
}
// Fallback: check PRPs directory
const prpsDir = join(projectPath, 'PRPs');
if (existsSync(prpsDir)) {
const fs = await import('fs');
const files = fs.readdirSync(prpsDir)
.filter(f => f.endsWith('.md'))
.map(f => f.replace('.md', ''));
return files.length > 0 ? files[files.length - 1] || null : null;
}
return null;
}
catch (error) {
logger.error('Error finding latest PRP:', error);
return null;
}
}
//# sourceMappingURL=execute-prp.js.map