google_drive_get_file_content
Retrieve file content from Google Drive by providing the file ID. Integrates with AI clients via the Google MCP server for streamlined data access.
Instructions
Get the content of a file from Google Drive
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileId | Yes | ID of the file to retrieve |
Implementation Reference
- handlers/drive.ts:31-44 (handler)Handler function that validates input arguments and delegates to GoogleDrive.getFileContent to execute the tool logic.export async function handleDriveGetFileContent( args: any, googleDriveInstance: GoogleDrive ) { if (!isGetFileContentArgs(args)) { throw new Error("Invalid arguments for google_drive_get_file_content"); } const { fileId } = args; const result = await googleDriveInstance.getFileContent(fileId); return { content: [{ type: "text", text: result }], isError: false, }; }
- utils/drive.ts:49-103 (helper)Core implementation in GoogleDrive class that interacts with Google Drive API to fetch file content, handling various MIME types appropriately.async getFileContent(fileId: string) { try { // First get the file metadata to check its type const fileMetadata = await this.drive.files.get({ fileId: fileId, fields: "name,mimeType", }); const { name, mimeType } = fileMetadata.data; // Handle text files directly if ( mimeType === "text/plain" || mimeType === "application/json" || mimeType.includes("text/") || mimeType.includes("application/javascript") ) { const response = await this.drive.files.get({ fileId: fileId, alt: "media", }); return `File: ${name}\nContent:\n\n${response.data}`; } // For Google Docs, get the content as plain text else if ( mimeType === "application/vnd.google-apps.document" || mimeType === "application/vnd.google-apps.spreadsheet" ) { let exportMimeType = "text/plain"; if (mimeType === "application/vnd.google-apps.spreadsheet") { exportMimeType = "text/csv"; } const response = await this.drive.files.export({ fileId: fileId, mimeType: exportMimeType, }); return `File: ${name}\nContent (exported as ${exportMimeType}):\n\n${response.data}`; } // For other file types, just return metadata else { return `File: ${name}\nType: ${mimeType}\nThis file type cannot be displayed as text. You can access it via Google Drive directly.`; } } catch (error) { throw new Error( `Failed to get file content: ${ error instanceof Error ? error.message : String(error) }` ); } }
- tools/drive/index.ts:32-45 (schema)Tool schema defining the name, description, and input schema (requiring fileId).export const GET_FILE_CONTENT_TOOL: Tool = { name: "google_drive_get_file_content", description: "Get the content of a file from Google Drive", inputSchema: { type: "object", properties: { fileId: { type: "string", description: "ID of the file to retrieve", }, }, required: ["fileId"], }, };
- server-setup.ts:186-190 (registration)Registration in the main server switch statement that routes tool calls to the appropriate handler.case "google_drive_get_file_content": return await driveHandlers.handleDriveGetFileContent( args, googleDriveInstance );
- utils/helper.ts:253-257 (schema)Type guard function for runtime validation of tool input arguments.export function isGetFileContentArgs(args: any): args is { fileId: string; } { return args && typeof args.fileId === "string"; }