Skip to main content
Glama

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
NameRequiredDescriptionDefault
fileIdYesID of the file to retrieve

Implementation Reference

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

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