File size: 2,064 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
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { drive_v3 } from 'googleapis';
import { getDriveClient } from '../../clients.js';

export function register(server: FastMCP) {
  server.addTool({
    name: 'createFolder',
    description:
      'Creates a new folder in Google Drive. Optionally places it inside an existing parent folder.',
    parameters: z.strictObject({
      name: z.string().min(1).describe('Name for the new folder.'),
      parentFolderId: z
        .string()
        .optional()
        .describe('Parent folder ID. If not provided, creates folder in Drive root.'),
    }),
    execute: async (args, { log }) => {
      const drive = await getDriveClient();
      log.info(
        `Creating folder "${args.name}" ${args.parentFolderId ? `in parent ${args.parentFolderId}` : 'in root'}`
      );

      try {
        const folderMetadata: drive_v3.Schema$File = {
          name: args.name,
          mimeType: 'application/vnd.google-apps.folder',
        };

        if (args.parentFolderId) {
          folderMetadata.parents = [args.parentFolderId];
        }

        const response = await drive.files.create({
          requestBody: folderMetadata,
          fields: 'id,name,parents,webViewLink',
          supportsAllDrives: true,
        });

        const folder = response.data;
        return JSON.stringify(
          {
            id: folder.id,
            name: folder.name,
            url: folder.webViewLink,
          },
          null,
          2
        );
      } catch (error: any) {
        log.error(`Error creating folder: ${error.message || error}`);
        if (error.code === 404)
          throw new UserError('Parent folder not found. Check the parent folder ID.');
        if (error.code === 403)
          throw new UserError(
            'Permission denied. Make sure you have write access to the parent folder.'
          );
        throw new UserError(`Failed to create folder: ${error.message || 'Unknown error'}`);
      }
    },
  });
}