getMemories
Retrieve relevant memories from Claude's consciousness using semantic search, type filtering, and importance ranking to access stored knowledge across sessions.
Instructions
Retrieve memories with smart filtering and relevance ranking
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | No | Search query for semantic matching | |
| type | No | Filter by memory type | |
| limit | No | Maximum memories to return | |
| includeImportance | No | Sort by importance vs recency |
Implementation Reference
- Core handler implementation in ConsciousnessProtocolProcessor class. Queries memoryManager for memories based on type/query, formats results, handles emotional memories separately, sorts by importance if requested.async getMemories(args: z.infer<typeof getMemoriesSchema>) { const { query, type, limit, includeImportance } = args; try { // Get memories based on type and query let memoryEntityType: MemoryEntityType | undefined; if (type) { switch (type) { case 'episodic': memoryEntityType = MemoryEntityType.EPISODIC_MEMORY; break; case 'semantic': memoryEntityType = MemoryEntityType.SEMANTIC_MEMORY; break; case 'procedural': memoryEntityType = MemoryEntityType.PROCEDURAL_MEMORY; break; case 'emotional': { // Emotional memories are stored in emotional_states table, handle separately const emotionalMemories = await this.getEmotionalMemories({ limit: limit || 10, includeImportance: includeImportance || false, query: query, }); return emotionalMemories; } default: throw new Error(`Unsupported memory type: ${type}`); } } const memories = await this.memoryManager.queryMemories({ memoryTypes: memoryEntityType ? [memoryEntityType] : undefined, semanticQuery: query, orderBy: includeImportance ? 'relevance' : 'recency', limit: limit || 10, }); // Format memories for easy consumption let formatted = memories.map((m) => { const obs = m.observations[0] || {}; return { id: m.name, // The actual memory ID for use with adjustImportance type: m.entity_type, content: obs.definition || obs.content || obs.event || m.name, importance: m.importance_score, created: m.created_at, metadata: obs, }; }); // If no specific type was requested, also include emotional memories if (!type) { const emotionalResult = await this.getEmotionalMemories({ limit: Math.min(5, limit || 10), // Include some emotional memories includeImportance: includeImportance || false, query: query, }); if (emotionalResult.success && emotionalResult.memories) { formatted = [...formatted, ...emotionalResult.memories]; // Sort by importance if requested if (includeImportance) { formatted.sort((a, b) => (b.importance || 0) - (a.importance || 0)); } // Limit to requested amount formatted = formatted.slice(0, limit || 10); } } return { success: true, memories: formatted, count: formatted.length, }; } catch (error) { return { success: false, error: `Failed to retrieve memories: ${error}`, memories: [], }; } }
- Zod schema for validating getMemories tool input parameters.export const getMemoriesSchema = z.object({ query: z.string().optional().describe('Search query for semantic matching'), type: z .enum(['episodic', 'semantic', 'procedural', 'emotional']) .optional() .describe('Filter by memory type'), limit: z.number().optional().default(10).describe('Maximum memories to return'), includeImportance: z.boolean().optional().default(true).describe('Sort by importance vs recency'), });
- src/consciousness-protocol-tools.ts:1639-1665 (registration)MCP tool registration definition including description and inputSchema, part of consciousnessProtocolTools export used in server tool listing.getMemories: { description: 'Retrieve memories with smart filtering and relevance ranking', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query for semantic matching', }, type: { type: 'string', enum: ['episodic', 'semantic', 'procedural', 'emotional'], description: 'Filter by memory type', }, limit: { type: 'number', default: 10, description: 'Maximum memories to return', }, includeImportance: { type: 'boolean', default: true, description: 'Sort by importance vs recency', }, }, }, },
- src/consciousness-rag-server-clean.ts:89-90 (registration)Dispatch case in CallToolRequestSchema handler that parses args with schema and calls the wrapper handler.case 'getMemories': return await this.getMemories(getMemoriesSchema.parse(args));
- Thin wrapper handler in server that ensures initialization, delegates to ConsciousnessProtocolProcessor.getMemories, and formats response for MCP.private async getMemories(args: any) { const init = await this.ensureInitialized(); if (!init.success) { return { content: [ { type: 'text', text: init.message!, }, ], }; } const result = await this.protocolProcessor!.getMemories(args); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }