batch_file_operations
Execute multiple file operations—create, update, append, delete, rename—in a single transaction with rollback capabilities for error handling. Enhances directory management efficiency.
Instructions
Perform multiple file operations in a single transaction
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operations | Yes | Array of file operations to perform | |
| rollback_on_error | No | Rollback all operations if any fails |
Input Schema (JSON Schema)
{
"properties": {
"operations": {
"description": "Array of file operations to perform",
"items": {
"properties": {
"operation": {
"description": "Type of operation",
"enum": [
"create",
"update",
"append",
"delete",
"rename"
],
"type": "string"
},
"params": {
"description": "Parameters for the operation",
"type": "object"
}
},
"required": [
"operation",
"params"
],
"type": "object"
},
"type": "array"
},
"rollback_on_error": {
"default": true,
"description": "Rollback all operations if any fails",
"type": "boolean"
}
},
"required": [
"operations"
],
"type": "object"
}
Implementation Reference
- server.js:727-802 (handler)The core handler function that implements the batch_file_operations tool logic. It processes multiple file operations (create, update, append, delete, rename) in sequence, tracks successful operations for rollback if needed, and returns a JSON summary of results.async handleBatchFileOperations(args) { const { operations, rollback_on_error = true } = args; const results = []; const performedOperations = []; try { for (const op of operations) { const { operation, params } = op; try { let result; switch (operation) { case 'create': result = await this.handleCreateFile(params); performedOperations.push({ type: 'create', path: params.path }); break; case 'update': result = await this.handleUpdateFile({ ...params, backup: false }); performedOperations.push({ type: 'update', path: params.path, backupPath: `${params.path}.batch-backup` }); break; case 'append': result = await this.handleAppendToFile(params); performedOperations.push({ type: 'append', path: params.path }); break; case 'delete': result = await this.handleDeleteFile({ ...params, backup: false }); performedOperations.push({ type: 'delete', path: params.path }); break; case 'rename': result = await this.handleRenameFile(params); performedOperations.push({ type: 'rename', oldPath: params.old_path, newPath: params.new_path }); break; default: throw new Error(`Unknown operation: ${operation}`); } results.push({ operation, status: 'success', message: result.content[0].text, }); } catch (error) { results.push({ operation, status: 'error', message: error.message, }); if (rollback_on_error) { // Rollback performed operations await this.rollbackOperations(performedOperations); throw new Error(`Batch operation failed at step ${results.length}. All changes rolled back. Error: ${error.message}`); } } } return { content: [ { type: 'text', text: JSON.stringify({ summary: `Batch operations completed: ${results.filter(r => r.status === 'success').length}/${operations.length} successful`, results, }, null, 2), }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Batch operations failed: ${error.message}`); } }
- server.js:340-369 (schema)Input schema definition for the batch_file_operations tool, specifying the structure of operations array and rollback option.inputSchema: { type: 'object', properties: { operations: { type: 'array', description: 'Array of file operations to perform', items: { type: 'object', properties: { operation: { type: 'string', enum: ['create', 'update', 'append', 'delete', 'rename'], description: 'Type of operation', }, params: { type: 'object', description: 'Parameters for the operation', }, }, required: ['operation', 'params'], }, }, rollback_on_error: { type: 'boolean', description: 'Rollback all operations if any fails', default: true, }, }, required: ['operations'], },
- server.js:337-370 (registration)Registration of the batch_file_operations tool in the ListTools response, including name, description, and full input schema.{ name: 'batch_file_operations', description: 'Perform multiple file operations in a single transaction', inputSchema: { type: 'object', properties: { operations: { type: 'array', description: 'Array of file operations to perform', items: { type: 'object', properties: { operation: { type: 'string', enum: ['create', 'update', 'append', 'delete', 'rename'], description: 'Type of operation', }, params: { type: 'object', description: 'Parameters for the operation', }, }, required: ['operation', 'params'], }, }, rollback_on_error: { type: 'boolean', description: 'Rollback all operations if any fails', default: true, }, }, required: ['operations'], }, },
- server.js:491-492 (registration)Dispatch case in the CallToolRequestSchema handler that routes calls to the batch_file_operations handler function.case 'batch_file_operations': return await this.handleBatchFileOperations(args);
- server.js:804-836 (helper)Supporting helper function used by the batch handler to rollback changes in case of errors when rollback_on_error is true.async rollbackOperations(operations) { // Rollback in reverse order for (let i = operations.length - 1; i >= 0; i--) { const op = operations[i]; try { switch (op.type) { case 'create': await fs.unlink(path.resolve(this.workingDirectory, op.path)); break; case 'update': if (op.backupPath) { const fullPath = path.resolve(this.workingDirectory, op.path); const backupPath = path.resolve(this.workingDirectory, op.backupPath); await fs.copyFile(backupPath, fullPath); await fs.unlink(backupPath); } break; case 'rename': await fs.rename( path.resolve(this.workingDirectory, op.newPath), path.resolve(this.workingDirectory, op.oldPath) ); break; // append and delete are harder to rollback without backups } } catch (error) { console.error(`Failed to rollback operation:`, op, error); } } }