Skip to content

Batch Operations

The batch_operations tool allows you to execute multiple file operations atomically with automatic rollback on failure. This is perfect for complex multi-step operations where you need all-or-nothing execution.

  • Atomic Transactions: All operations succeed or all are rolled back
  • Automatic Backups: Creates backups before destructive operations
  • Pre-execution Validation: Validates all operations before executing
  • Rollback on Failure: Automatically reverts changes if any operation fails
  • Progress Tracking: Detailed results for each operation
  • Validate-Only Mode: Test operations without executing them
{
"type": "write",
"path": "/path/to/file.txt",
"content": "file content here"
}
{
"type": "edit",
"path": "/path/to/file.txt",
"old_text": "text to replace",
"new_text": "new text"
}
{
"type": "move",
"source": "/path/to/old.txt",
"destination": "/path/to/new.txt"
}
{
"type": "copy",
"source": "/path/to/source.txt",
"destination": "/path/to/destination.txt"
}
{
"type": "delete",
"path": "/path/to/file.txt"
}
{
"type": "create_dir",
"path": "/path/to/new/directory"
}
{
"tool": "batch_operations",
"arguments": {
"request_json": "{\"operations\":[{\"type\":\"write\",\"path\":\"C:\\\\temp\\\\file1.txt\",\"content\":\"Hello\"},{\"type\":\"write\",\"path\":\"C:\\\\temp\\\\file2.txt\",\"content\":\"World\"}],\"atomic\":true,\"create_backup\":true}"
}
}
{
"operations": [
{
"type": "edit",
"path": "src/utils.js",
"old_text": "function oldName(",
"new_text": "function newName("
},
{
"type": "edit",
"path": "src/index.js",
"old_text": "oldName()",
"new_text": "newName()"
},
{
"type": "edit",
"path": "tests/utils.test.js",
"old_text": "oldName()",
"new_text": "newName()"
}
],
"atomic": true,
"create_backup": true
}
{
"operations": [
{
"type": "create_dir",
"path": "src/components"
},
{
"type": "move",
"source": "Header.js",
"destination": "src/components/Header.js"
},
{
"type": "move",
"source": "Footer.js",
"destination": "src/components/Footer.js"
},
{
"type": "write",
"path": "src/components/index.js",
"content": "export { Header } from './Header';\nexport { Footer } from './Footer';"
}
],
"atomic": true,
"create_backup": true
}
{
"operations": [
{"type": "delete", "path": "important.txt"},
{"type": "write", "path": "backup.txt", "content": "backup"}
],
"atomic": true,
"create_backup": true,
"validate_only": true
}

Array of operations to execute. Each operation must have a type field and type-specific fields.

If true, all operations are rolled back if any single operation fails.

If true, creates a timestamped backup of all affected files before execution.

If true, only validates operations without executing them. Useful for testing.

✅ Batch Operations Completed Successfully
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Summary:
Total operations: 3
Completed: 3
Failed: 0
Execution time: 45.2ms
Backup created: C:\Temp\mcp-batch-backups\batch-20250124-153045
📋 Operation Details:
✓ [0] write: C:\temp\file1.txt (5 B)
✓ [1] write: C:\temp\file2.txt (5 B)
✓ [2] move: old.txt (1.2 KB)
❌ Batch Operations Failed
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Summary:
Total operations: 3
Completed: 1
Failed: 1
Execution time: 23.1ms
Backup created: C:\Temp\mcp-batch-backups\batch-20250124-153045
⚠️ Rollback performed - all changes reverted
📋 Operation Details:
✓ [0] write: C:\temp\file1.txt (5 B)
✗ [1] write: C:\temp\readonly.txt - Error: permission denied
❌ Errors:
• Operation 1 failed, rollback completed: permission denied
✅ Batch Validation Results
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✓ All 3 operations validated successfully
✓ Ready to execute
Section titled “1. Always Use Atomic Mode for Related Operations”
{
"operations": [...],
"atomic": true // ✅ Ensures consistency
}

2. Create Backups for Destructive Operations

Section titled “2. Create Backups for Destructive Operations”
{
"operations": [
{"type": "delete", "path": "file.txt"}
],
"create_backup": true // ✅ Safety net
}
// Step 1: Validate
{
"operations": [...],
"validate_only": true
}
// Step 2: If validation succeeds, execute
{
"operations": [...],
"validate_only": false
}

Operations execute in sequence. Ensure dependencies are ordered correctly:

{
"operations": [
{"type": "create_dir", "path": "new_folder"}, // ✅ Create folder first
{"type": "write", "path": "new_folder/file.txt", "content": "..."} // ✅ Then write file
]
}

If validation fails, you’ll get a detailed error response:

✗ Validation failed
Errors:
• Op 0: parent directory does not exist: /nonexistent
• Op 2: file does not exist: /missing.txt

If execution fails and atomic: true:

  • All completed operations are automatically rolled back
  • Original state is restored
  • Detailed error message is provided

If atomic: false:

  • Operations continue despite failures
  • Partial results are returned
  • No rollback occurs

Rename functions/variables across multiple files atomically.

Move files, create directories, update imports - all or nothing.

Update multiple config files together, rollback if any fails.

Apply schema changes across multiple files atomically.

Copy files, update versions, create backups - all in one transaction.

  • Validation: ~1-5ms per operation
  • Backup Creation: Depends on file sizes
  • Execution: Depends on operation type
  • Rollback: Usually faster than forward execution

Backups are stored in:

  • Windows: %TEMP%\mcp-batch-backups\
  • Linux/Mac: /tmp/mcp-batch-backups/

Backups include:

  • Timestamped folder (e.g., batch-20250124-153045)
  • Original files before modification
  • metadata.json with operation details

Old backups are automatically cleaned, keeping only the last 10.

On Windows with Claude Desktop, be aware that:

  • Writing operations (write, edit, copy) use the same mechanism as individual file operations
  • The copy operation DOES work in batch_operations
  • However, see WINDOWS_FILESYSTEM_PERSISTENCE.md for limitations

For guaranteed persistence on Windows:

  1. Use copy operation in batch_operations (✅ it works)
  2. Or use individual copy_file tool
  3. Both write to the filesystem correctly

Example for backup on Windows:

{
"operations": [
{
"type": "copy",
"source": "C:\\path\\to\\file.txt",
"destination": "C:\\path\\to\\backup\\file.txt"
}
],
"atomic": true,
"create_backup": true
}
  • Maximum operations per batch: No hard limit (recommended < 100)
  • File size limits: Same as individual operations
  • Nested transactions: Not supported
  • Concurrent batches: Managed via mutex (one at a time)
  • Edit operations in batch_operations use simple string replacement (not fuzzy matching)
{
"operations": [
{
"type": "create_dir",
"path": "backup"
},
{
"type": "copy",
"source": "important.txt",
"destination": "backup/important.txt"
},
{
"type": "edit",
"path": "important.txt",
"old_text": "version: 1.0",
"new_text": "version: 2.0"
},
{
"type": "write",
"path": "CHANGELOG.md",
"content": "## Version 2.0\n- Updated version"
}
],
"atomic": true,
"create_backup": true,
"validate_only": false
}

This will:

  1. Create backup directory
  2. Copy important.txt to backup
  3. Edit version in important.txt
  4. Write changelog

If any step fails, all changes are reverted.