Skip to main content
Glama

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
NameRequiredDescriptionDefault
contentYesContent of the file
folderIdNoID of the folder to create the file in
mimeTypeNoMIME type of the file (e.g., 'text/plain', 'application/vnd.google-apps.document')
nameYesName of the file to create

Implementation Reference

  • 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, }; }
  • 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"], }, };
  • 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 );
  • 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) }` ); } }
  • 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") ); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vakharwalad23/google-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server