memory-engineering
Version:
Advanced Memory-Aware Code Context System with claude-flow-inspired architecture, showcasing MongoDB + Voyage AI strategic positioning
82 lines (71 loc) ⢠2.79 kB
text/typescript
/**
* Memory Search Tool
* Using claude-flow-inspired MemoryManager for conflict-free operations
*/
import { Tool } from '@modelcontextprotocol/sdk/types.js';
import { IMemoryManager } from '../memory/memory-manager.js';
import { logger } from '../utils/logger.js';
export const memorySearchTool: Tool = {
name: 'memory_search',
description: 'Search through project memories',
inputSchema: {
type: 'object',
properties: {
projectPath: { type: 'string', description: 'Project path to search within' },
query: { type: 'string', description: 'Search query' },
memoryType: { type: 'string', description: 'Memory type filter (optional)' },
tags: { type: 'array', items: { type: 'string' }, description: 'Tag filters (optional)' },
limit: { type: 'number', description: 'Maximum results (default: 10)' }
},
required: ['projectPath', 'query']
}
};
export function createMemorySearchHandler(memoryManager: IMemoryManager) {
return async (args: any) => {
try {
const { projectPath, query, memoryType, tags, limit = 10 } = args;
const searchQuery = {
projectPath,
search: query,
memoryType,
tags,
limit: Math.min(limit, 50) // Cap at 50 for performance
};
const results = await memoryManager.query(searchQuery);
logger.info('Memory search completed', {
projectPath,
query: query.substring(0, 100),
resultCount: results.length,
memoryType,
tags
});
if (results.length === 0) {
return {
content: [{
type: 'text',
text: `š No memories found for query: "${query}"\n\nTry:\n- Broader search terms\n- Different memory types\n- Check if memories exist for this project`
}],
isError: false
};
}
const formattedResults = results.map((entry, index) => {
const snippet = entry.searchableText?.substring(0, 200) ||
JSON.stringify(entry.content).substring(0, 200);
return `${index + 1}. **${entry.memoryType}** (v${entry.version})\n ${snippet}...\n Tags: ${entry.tags.join(', ')}\n Updated: ${entry.timestamp.toISOString().split('T')[0]}`;
}).join('\n\n');
return {
content: [{
type: 'text',
text: `š Found ${results.length} memories for "${query}":\n\n${formattedResults}\n\nš” Use specific memory tools to view full details.`
}],
isError: false
};
} catch (error) {
logger.error('Failed to search memories', error);
return {
content: [{ type: 'text', text: `ā Search error: ${error instanceof Error ? error.message : 'Unknown error'}` }],
isError: true
};
}
};
}