Skip to main content
Glama

cleanup_memories

Remove old or low-relevance short-term memories by deleting those older than 1 year or with very low scores while preserving at least 512 memories.

Instructions

Manually trigger cleanup of old or low-relevance short-term memories. This removes memories older than 1 year or with very low relevance scores, keeping at least 512 memories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
conversation_idYes

Implementation Reference

  • The handler function for the 'cleanup_memories' tool. It calls memoryManager.cleanup() to perform the cleanup and saves the updated memories to storage.
    handler: async (args) => { try { const removedCount = memoryManager.cleanup(); await storageManager.saveShortTermMemories(memoryManager.getMemories()); return { success: true, removedCount, remainingCount: memoryManager.getMemories().length, message: `Cleanup complete: removed ${removedCount} memor${removedCount === 1 ? 'y' : 'ies'}` }; } catch (error) { return { success: false, error: error.message }; } }
  • Zod input schema for the tool, requiring a conversation_id.
    inputSchema: z.object({ conversation_id: z.string().describe('Conversation ID for storage') }),
  • Tool definition and registration within createShortTermTools(), which is called dynamically per conversation in src/index.js.
    { name: 'cleanup_memories', description: 'Manually trigger cleanup of old or low-relevance short-term memories. This removes memories older than 1 year or with very low relevance scores, keeping at least 512 memories.', inputSchema: z.object({ conversation_id: z.string().describe('Conversation ID for storage') }), handler: async (args) => { try { const removedCount = memoryManager.cleanup(); await storageManager.saveShortTermMemories(memoryManager.getMemories()); return { success: true, removedCount, remainingCount: memoryManager.getMemories().length, message: `Cleanup complete: removed ${removedCount} memor${removedCount === 1 ? 'y' : 'ies'}` }; } catch (error) { return { success: false, error: error.message }; } } },
  • Core cleanup implementation in ShortTermMemoryManager class. Removes memories older than 1 year or below relevance threshold, retains at least 512 highest relevance ones.
    cleanup(currentTimeStamp = Date.now()) { const initialCount = this.memories.length; const oneYearAgo = currentTimeStamp - MEMORY_TTL_MS; const passingMemories = []; const failingMemories = []; for (const mem of this.memories) { const relevance = this.calculateRelevance(mem, [], currentTimeStamp, {}); mem._relevance = relevance; if (mem.time_stamp.getTime() < oneYearAgo) { failingMemories.push(mem); } else if (relevance >= CLEANUP_MIN_SCORE_THRESHOLD) { passingMemories.push(mem); } else { failingMemories.push(mem); } } if (passingMemories.length >= MIN_RETAINED_MEMORIES) { this.memories = passingMemories; } else { const neededFromFailing = MIN_RETAINED_MEMORIES - passingMemories.length; failingMemories.sort((a, b) => b._relevance - a._relevance); const supplementaryMemories = failingMemories.slice(0, neededFromFailing); this.memories = [...passingMemories, ...supplementaryMemories]; } // 清理临时属性 for (const mem of this.memories) { delete mem._relevance; } this.lastCleanupTime = currentTimeStamp; const removed = initialCount - this.memories.length; if (removed > 0) { console.log(`[Memory] Cleanup: removed ${removed} entries, ${this.memories.length} remaining`); } return removed; }
  • src/index.js:284-289 (registration)
    Dynamic recreation and execution of short-term tools (including cleanup_memories) for the specific conversation_id during tool call handling in the MCP server.
    if (toolScope === 'short-term' || toolName.includes('short_term')) { manager = await getShortTermManager(conversationId); storage = getStorageManager(conversationId); const tools = createShortTermTools(manager, storage, queryCache); const tool = tools.find(t => t.name === toolName); result = await withTimeout(tool.handler(validatedArgs), timeout, `Tool ${toolName} timeout`);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/win10ogod/memory-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server