Spaces:
Sleeping
Sleeping
File size: 2,843 Bytes
7dc28be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getDriveClient } from '../../clients.js';
export function register(server: FastMCP) {
server.addTool({
name: 'deleteFile',
description:
'Moves a file or folder to the trash, or permanently deletes it. Set permanent=true for irreversible deletion.',
parameters: z.strictObject({
fileId: z
.string()
.describe('The file or folder ID from a Google Drive URL or a previous tool result.'),
permanent: z
.boolean()
.optional()
.default(false)
.describe('If true, permanently deletes the file instead of moving it to trash.'),
}),
execute: async (args, { log }) => {
const drive = await getDriveClient();
log.info(`Deleting file ${args.fileId} ${args.permanent ? '(permanent)' : '(to trash)'}`);
try {
// Get file info before deletion
const fileInfo = await drive.files.get({
fileId: args.fileId,
fields: 'name,mimeType',
supportsAllDrives: true,
});
const fileName = fileInfo.data.name;
const isFolder = fileInfo.data.mimeType === 'application/vnd.google-apps.folder';
if (args.permanent) {
await drive.files.delete({
fileId: args.fileId,
supportsAllDrives: true,
});
return JSON.stringify(
{
success: true,
action: 'permanently_deleted',
fileId: args.fileId,
fileName,
type: isFolder ? 'folder' : 'file',
message: `Permanently deleted ${isFolder ? 'folder' : 'file'} "${fileName}".`,
},
null,
2
);
} else {
await drive.files.update({
fileId: args.fileId,
requestBody: {
trashed: true,
},
supportsAllDrives: true,
});
return JSON.stringify(
{
success: true,
action: 'trashed',
fileId: args.fileId,
fileName,
type: isFolder ? 'folder' : 'file',
message: `Moved ${isFolder ? 'folder' : 'file'} "${fileName}" to trash. It can be restored from the trash.`,
},
null,
2
);
}
} catch (error: any) {
log.error(`Error deleting file: ${error.message || error}`);
if (error.code === 404) throw new UserError('File not found. Check the file ID.');
if (error.code === 403)
throw new UserError('Permission denied. Make sure you have delete access to this file.');
throw new UserError(`Failed to delete file: ${error.message || 'Unknown error'}`);
}
},
});
}
|