// File Operations MCP Tools
import { FileOperationsService } from '../services/file-operations-service.js';
import { ElementorService } from '../services/elementor/elementor-service.js';
import { logger } from '../utils/logger.js';
export function createFileOperationsTools(
fileOpsService: FileOperationsService,
elementorService: ElementorService
) {
const tools: any[] = [];
// ========== EXPORT TOOLS ==========
tools.push({
name: 'export_elementor_page',
description: 'Export Elementor page data to a JSON file',
inputSchema: {
type: 'object',
properties: {
post_id: {
type: 'number',
description: 'Post/Page ID to export'
},
include_metadata: {
type: 'boolean',
description: 'Include metadata (checksum, version)',
default: true
},
prettify: {
type: 'boolean',
description: 'Pretty print JSON (default: true)',
default: true
}
},
required: ['post_id']
},
handler: async (args: any) => {
const data = await elementorService.data.getElementorData(args.post_id);
const filePath = await fileOpsService.exportToFile(
args.post_id,
data,
{
includeMetadata: args.include_metadata !== false,
prettify: args.prettify !== false
}
);
return {
content: [{
type: 'text',
text: `Page exported successfully to: ${filePath}`
}]
};
}
});
tools.push({
name: 'import_elementor_page',
description: 'Import Elementor page data from a JSON file',
inputSchema: {
type: 'object',
properties: {
file_path: {
type: 'string',
description: 'Path to the import file'
},
post_id: {
type: 'number',
description: 'Target post/page ID (uses postId from file if not specified)'
},
validate_structure: {
type: 'boolean',
description: 'Validate data structure before import',
default: true
},
backup_before: {
type: 'boolean',
description: 'Create backup before importing',
default: true
}
},
required: ['file_path']
},
handler: async (args: any) => {
const importData = await fileOpsService.importFromFile(
args.file_path,
{
validateStructure: args.validate_structure !== false
}
);
const targetPostId = args.post_id || importData.postId;
// Create backup if requested
if (args.backup_before !== false) {
const currentData = await elementorService.data.getElementorData(targetPostId);
await fileOpsService.createBackup(targetPostId, currentData);
}
// Import data
await elementorService.data.updateElementorData(targetPostId, importData.data);
return {
content: [{
type: 'text',
text: `Data imported successfully to post ${targetPostId}`
}]
};
}
});
// ========== BACKUP TOOLS ==========
tools.push({
name: 'create_page_backup',
description: 'Create a backup of a page before making changes',
inputSchema: {
type: 'object',
properties: {
post_id: {
type: 'number',
description: 'Post/Page ID to backup'
}
},
required: ['post_id']
},
handler: async (args: any) => {
const data = await elementorService.data.getElementorData(args.post_id);
const filePath = await fileOpsService.createBackup(args.post_id, data);
return {
content: [{
type: 'text',
text: `Backup created successfully: ${filePath}`
}]
};
}
});
tools.push({
name: 'restore_from_backup',
description: 'Restore a page from a backup file',
inputSchema: {
type: 'object',
properties: {
file_path: {
type: 'string',
description: 'Path to the backup file'
},
post_id: {
type: 'number',
description: 'Target post ID (uses backup postId if not specified)'
}
},
required: ['file_path']
},
handler: async (args: any) => {
const backupData = await fileOpsService.restoreFromBackup(args.file_path);
const targetPostId = args.post_id || backupData.postId;
await elementorService.data.updateElementorData(targetPostId, backupData.data);
return {
content: [{
type: 'text',
text: `Page ${targetPostId} restored successfully from backup`
}]
};
}
});
tools.push({
name: 'list_exports',
description: 'List all exported files',
inputSchema: {
type: 'object',
properties: {}
},
handler: async (args: any) => {
const exports = await fileOpsService.listExports();
return {
content: [{
type: 'text',
text: JSON.stringify(exports, null, 2)
}]
};
}
});
tools.push({
name: 'list_backups',
description: 'List available backups, optionally filtered by post ID',
inputSchema: {
type: 'object',
properties: {
post_id: {
type: 'number',
description: 'Filter backups for specific post ID'
}
}
},
handler: async (args: any) => {
const backups = await fileOpsService.listBackups(args.post_id);
return {
content: [{
type: 'text',
text: JSON.stringify(backups, null, 2)
}]
};
}
});
tools.push({
name: 'cleanup_old_backups',
description: 'Delete backups older than specified days',
inputSchema: {
type: 'object',
properties: {
days_to_keep: {
type: 'number',
description: 'Number of days to keep backups (default: 30)',
default: 30
}
}
},
handler: async (args: any) => {
const deletedCount = await fileOpsService.cleanupOldBackups(
args.days_to_keep || 30
);
return {
content: [{
type: 'text',
text: `Cleanup complete. Deleted ${deletedCount} old backups.`
}]
};
}
});
tools.push({
name: 'get_file_info',
description: 'Get information about an export or backup file',
inputSchema: {
type: 'object',
properties: {
file_path: {
type: 'string',
description: 'Path to the file'
}
},
required: ['file_path']
},
handler: async (args: any) => {
const info = await fileOpsService.getFileInfo(args.file_path);
return {
content: [{
type: 'text',
text: JSON.stringify(info, null, 2)
}]
};
}
});
return tools;
}