google_drive_create_file
Create and save new files directly in Google Drive by specifying the file name, content, MIME type, and folder location. Integrates with Google MCP for efficient file management.
Instructions
Create a new file in Google Drive
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content of the file | |
| folderId | No | ID of the folder to create the file in | |
| mimeType | No | MIME type of the file (e.g., 'text/plain', 'application/vnd.google-apps.document') | |
| name | Yes | Name of the file to create |
Implementation Reference
- handlers/drive.ts:46-64 (handler)The MCP tool handler function that validates input arguments using isCreateFileArgs and delegates the file creation to the GoogleDrive instance's createFile method, returning the result in the expected format.export async function handleDriveCreateFile( args: any, googleDriveInstance: GoogleDrive ) { if (!isCreateFileArgs(args)) { throw new Error("Invalid arguments for google_drive_create_file"); } const { name, content, mimeType, folderId } = args; const result = await googleDriveInstance.createFile( name, content, mimeType, folderId ); return { content: [{ type: "text", text: result }], isError: false, }; }
- tools/drive/index.ts:47-73 (schema)The Tool object definition providing the name, description, and inputSchema (JSON Schema) for the google_drive_create_file tool.export const CREATE_FILE_TOOL: Tool = { name: "google_drive_create_file", description: "Create a new file in Google Drive", inputSchema: { type: "object", properties: { name: { type: "string", description: "Name of the file to create", }, content: { type: "string", description: "Content of the file", }, mimeType: { type: "string", description: "MIME type of the file (e.g., 'text/plain', 'application/vnd.google-apps.document')", }, folderId: { type: "string", description: "ID of the folder to create the file in", }, }, required: ["name", "content"], }, };
- server-setup.ts:191-195 (registration)The switch case in the main CallToolRequestSchema handler that registers and routes calls to the specific drive handler function.case "google_drive_create_file": return await driveHandlers.handleDriveCreateFile( args, googleDriveInstance );
- utils/drive.ts:105-153 (helper)The core implementation in GoogleDrive class that uses Google Drive API v3 to create files, handling both Google Apps mimeTypes and regular files with media body, optionally in a folder.async createFile( name: string, content: string, mimeType: string = "text/plain", folderId?: string ) { try { const fileMetadata: any = { name: name, }; if (folderId) { fileMetadata.parents = [folderId]; } // If creating a Google Doc, Spreadsheet, etc. if (mimeType.includes("application/vnd.google-apps")) { const response = await this.drive.files.create({ requestBody: fileMetadata, fields: "id,name,webViewLink", mimeType: mimeType, }); const { id, webViewLink } = response.data; return `Created ${mimeType} with name: ${name}\nID: ${id}\nLink: ${webViewLink}`; } // For regular files with content const response = await this.drive.files.create({ requestBody: fileMetadata, media: { mimeType: mimeType, body: content, }, fields: "id,name,webViewLink", }); const { id, webViewLink } = response.data; return `Created file with name: ${name}\nID: ${id}\nLink: ${ webViewLink || "N/A" }`; } catch (error) { throw new Error( `Failed to create file: ${ error instanceof Error ? error.message : String(error) }` ); } }
- utils/helper.ts:259-272 (schema)Type guard function for input validation matching the tool's inputSchema, used in the handler to check arguments before execution.export function isCreateFileArgs(args: any): args is { name: string; content: string; mimeType?: string; folderId?: string; } { return ( args && typeof args.name === "string" && typeof args.content === "string" && (args.mimeType === undefined || typeof args.mimeType === "string") && (args.folderId === undefined || typeof args.folderId === "string") ); }