backup_and_edit
Safeguard your files by creating backups before editing them, ensuring data integrity and minimizing risks during modifications.
Instructions
Create backups of files before editing them
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | List of files to backup and edit | |
| operation | Yes | The edit operation to perform |
Implementation Reference
- src/router/operation-router.ts:551-579 (handler)Main handler logic for the 'backup_and_edit' tool within the hybrid executor. Creates backups for all affected files, attempts the edit operation, and restores backups if the edit fails.case 'backup_and_edit': // Create backups with file system const backups = await Promise.all( operation.affectedFiles.map(file => this.fileSystemManager.createBackup(file) ) ); try { // Use Edit for the edits const result = await this.executeWithEdit({ ...operation, type: operation.params.operation.type }); return { ...result, backups }; } catch (error) { // If edits fail, restore backups await Promise.all( operation.affectedFiles.map((file, index) => this.fileSystemManager.restoreBackup(backups[index], file) ) ); throw error; }
- src/index.ts:365-388 (registration)Registers the 'backup_and_edit' MCP tool with the server, including name, description, input schema, and annotations.mcpServer.registerTool({ name: 'backup_and_edit', description: 'Create backups of files before editing them', inputSchema: { type: 'object', properties: { files: { type: 'array', description: 'List of files to backup and edit' }, operation: { type: 'object', description: 'The edit operation to perform' } }, required: ['files', 'operation'] }, annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false } });
- Helper function to create a timestamped backup file for a given file path, used by the backup_and_edit handler.public async createBackup(filePath: string): Promise<string> { try { const content = await this.readFile(filePath); const backupPath = `${filePath}.backup.${Date.now()}`; await this.writeFile(backupPath, content); this.backups.set(filePath, backupPath); return backupPath; } catch (error: any) { throw new Error(`Failed to create backup of ${filePath}: ${error.message}`); }
- Helper function to restore a backup file over the original file, used by the backup_and_edit handler on failure.public async restoreBackup(backupPath: string, originalPath: string): Promise<void> { try { const content = await this.readFile(backupPath); await this.writeFile(originalPath, content); } catch (error: any) { throw new Error(`Failed to restore backup ${backupPath} to ${originalPath}: ${error.message}`); }
- src/index.ts:369-382 (schema)Input schema definition for the 'backup_and_edit' tool, specifying required 'files' array and 'operation' object.type: 'object', properties: { files: { type: 'array', description: 'List of files to backup and edit' }, operation: { type: 'object', description: 'The edit operation to perform' } }, required: ['files', 'operation'] }, annotations: {