backup_memories
Backup conversation memories to files with metadata and timestamps. Export short-term and long-term memories from Memory MCP Server for preservation and analysis.
Instructions
將指定對話的所有記憶備份到文件。支持導出短期和長期記憶,包含完整的元數據和時間戳。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| conversation_id | Yes | 對話 ID | |
| output_path | No | 輸出文件路徑(默認為 data/backups/) | |
| include_short_term | No | 是否包含短期記憶 | |
| include_long_term | No | 是否包含長期記憶 | |
| compress | No | 是否壓縮(保留用於將來實現) |
Implementation Reference
- src/tools/backup-tools.js:35-95 (handler)The async handler that implements the backup_memories tool logic: loads specified memories, creates timestamped JSON backup file in data/backups/, returns file path, memory counts, and size.async handler(args) { const { conversation_id, output_path, include_short_term, include_long_term } = args; try { const storage = getStorageManager(conversation_id); const backup = { version: '1.0', timestamp: new Date().toISOString(), conversation_id, short_term: null, long_term: null }; let totalMemories = 0; // 加載短期記憶 if (include_short_term) { const shortTerm = await storage.loadShortTermMemories(); backup.short_term = shortTerm; totalMemories += shortTerm.length; } // 加載長期記憶 if (include_long_term) { const longTerm = await storage.loadLongTermMemories(); backup.long_term = longTerm; totalMemories += longTerm.length; } // 確定輸出路徑 const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const defaultPath = path.join(__dirname, '../../data/backups'); const backupDir = output_path || defaultPath; await fs.mkdir(backupDir, { recursive: true }); const fileName = `backup_${conversation_id}_${timestamp}.json`; const filePath = path.join(backupDir, fileName); // 寫入備份文件 const content = JSON.stringify(backup, null, 2); await fs.writeFile(filePath, content, 'utf-8'); const sizeKB = (Buffer.byteLength(content, 'utf-8') / 1024).toFixed(2); return { success: true, backup_path: filePath, total_memories: totalMemories, short_term_count: backup.short_term?.length || 0, long_term_count: backup.long_term?.length || 0, size_kb: sizeKB, timestamp: backup.timestamp }; } catch (error) { return { success: false, error: error.message }; } }
- src/tools/backup-tools.js:28-34 (schema)Zod input schema for backup_memories tool parameters.inputSchema: z.object({ conversation_id: z.string().describe('對話 ID'), output_path: z.string().optional().describe('輸出文件路徑(默認為 data/backups/)'), include_short_term: z.boolean().default(true).describe('是否包含短期記憶'), include_long_term: z.boolean().default(true).describe('是否包含長期記憶'), compress: z.boolean().default(false).describe('是否壓縮(保留用於將來實現)') }),
- src/tools/backup-tools.js:25-96 (registration)Tool definition pushed to the tools array returned by createBackupTools, including name, description, schema, and handler.tools.push({ name: 'backup_memories', description: '將指定對話的所有記憶備份到文件。支持導出短期和長期記憶,包含完整的元數據和時間戳。', inputSchema: z.object({ conversation_id: z.string().describe('對話 ID'), output_path: z.string().optional().describe('輸出文件路徑(默認為 data/backups/)'), include_short_term: z.boolean().default(true).describe('是否包含短期記憶'), include_long_term: z.boolean().default(true).describe('是否包含長期記憶'), compress: z.boolean().default(false).describe('是否壓縮(保留用於將來實現)') }), async handler(args) { const { conversation_id, output_path, include_short_term, include_long_term } = args; try { const storage = getStorageManager(conversation_id); const backup = { version: '1.0', timestamp: new Date().toISOString(), conversation_id, short_term: null, long_term: null }; let totalMemories = 0; // 加載短期記憶 if (include_short_term) { const shortTerm = await storage.loadShortTermMemories(); backup.short_term = shortTerm; totalMemories += shortTerm.length; } // 加載長期記憶 if (include_long_term) { const longTerm = await storage.loadLongTermMemories(); backup.long_term = longTerm; totalMemories += longTerm.length; } // 確定輸出路徑 const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const defaultPath = path.join(__dirname, '../../data/backups'); const backupDir = output_path || defaultPath; await fs.mkdir(backupDir, { recursive: true }); const fileName = `backup_${conversation_id}_${timestamp}.json`; const filePath = path.join(backupDir, fileName); // 寫入備份文件 const content = JSON.stringify(backup, null, 2); await fs.writeFile(filePath, content, 'utf-8'); const sizeKB = (Buffer.byteLength(content, 'utf-8') / 1024).toFixed(2); return { success: true, backup_path: filePath, total_memories: totalMemories, short_term_count: backup.short_term?.length || 0, long_term_count: backup.long_term?.length || 0, size_kb: sizeKB, timestamp: backup.timestamp }; } catch (error) { return { success: false, error: error.message }; } } });
- src/index.js:161-162 (registration)Registers all backup tools (including backup_memories) from createBackupTools into the server's toolRegistry with 'backup' scope.const backupTools = createBackupTools(getShortTermManager, getLongTermManager, getStorageManager); backupTools.forEach(tool => registerTool(tool, 'backup'));