Processing File Content Using Gemini CLI with an MCP Server Built by Google Apps Script

Gists

Abstract

This report details two methods for processing files using the Gemini CLI and a Google Apps Script MCP server: direct Base64 encoding and indirect transfer via the Google Drive API using ggsrun. The direct method proved ineffective due to token limits. The recommended approach, leveraging ggsrun, allows for efficient, scalable file transfers by using file IDs instead of embedding content within the prompt, enabling advanced automation capabilities.

Introduction

This report demonstrates how to efficiently process file content using the Gemini CLI integrated with an MCP (Magical Control Panel) server built on Google Apps Script Web Apps. By seamlessly handling data across your local PC, Google Drive, and Gmail, this integration significantly expands automation and data management capabilities, leading to enhanced productivity.

Building on the concepts from a previous report, “Gemini CLI with MCP Server: Expanding Possibilities with Google Apps Script” (Ref), this article dives deeper into the crucial area of file content processing. We will explore and compare two distinct methods for transferring files between a local machine and Google services through the Gemini CLI, highlighting the most effective approach for robust and scalable workflows.

Prerequisites

Before proceeding, you must install and configure the MCP server and the Gemini CLI. Please follow the instructions provided in the repository links below.

1. Set up the MCP Server

Detailed instructions can be found at this repository: Preparing the MCP Server.

2. Set up the Gemini CLI

Detailed instructions can be found at this repository: Preparing Gemini CLI.

Method 1: Direct File Transfer via Base64 Encoding

The first method involves transferring file content directly between the Gemini CLI and the MCP server by encoding the file into base64 data. This approach embeds the entire file content within the prompt itself.

Configuration

To enable the Gemini CLI to handle base64 encoding and decoding, add the following tool definitions to your GEMINI.md file. This file is typically located at ~/.gemini/GEMINI.md on macOS/Linux or %USERPROFILE%\.gemini\GEMINI.md on Windows.

# Processing base64 data

* Encode text data to base64
  ```bash
  echo "###text data###" | base64
  ```

* Encode a file to base64
  ```bash
  base64 [filename]
  ```

* Decode base64 data
  ```bash
  echo "###base64 data###" | base64 -d
  ```

* Decode a file of base64 data
  ```bash
  base64 -d [filename]
  ```

Performance Test

A 450 KB image file (sample.png) was used to test the performance of the direct transfer method.

Test 1: Download from Google Drive

In this case, a tool get_file_from_google_drive is called by the Gemini CLI. Ref

Prompt:

Download a file "sample.png" from Google Drive, and save it as a file by decoding the base64.

Result: The process failed immediately with an error.

✕ [API Error: got status: INVALID_ARGUMENT. {"error":{"code":400,"message":"The input token count (193988) exceeds the maximum number of tokens allowed (131072).","status":"INVALID_ARGUMENT"}}]

Test 2: Upload to Google Drive

In this case, a tool put_file_to_google_drive is called by the Gemini CLI. Ref

Prompt:

Upload a file of "sample.png" to Google Drive by encoding to base64.

Result: The process did not complete after more than 10 minutes and was manually terminated.

Conclusion on Direct Transfer

Transferring file content as base64 data significantly increases the prompt size. This leads to critical issues, including high processing costs and errors from exceeding the model’s maximum token limit. While this method may be feasible for extremely small files (a few KBs), it is not scalable. Due to these limitations, the direct transfer method is not recommended for general use.

Method 2: Indirect File Transfer via Google Drive API with ggsrun

The second, more robust method involves transferring files indirectly using the Google Drive API. Instead of sending the file content, we only pass file identifiers (like filenames or file IDs) in the prompt. This process is streamlined by using ggsrun, a CLI tool designed to interact with Google Drive.

1. ggsrun Setup Guide

To use this method, you first need to set up ggsrun.

  1. Download ggsrun: Get the latest release from the ggsrun releases page. The main repository is here.
  2. Create a Google Cloud Project: Go to the Google Cloud Platform Resource Manager and click CREATE PROJECT.
  3. Enable Drive API: In your new project, navigate to APIs & Services > Enabled APIs & Services. Search for and enable the Drive API.
  4. Create Credentials: Navigate to APIs & Services > Credentials.
  5. Configure OAuth Client ID: Click Create credentials, select OAuth client ID, and choose Web Application.
  6. Set Redirect URI: Under “Authorized redirect URIs,” add http://localhost:8080.
  7. Create and Download Credentials: Click Create, then Download JSON. Important: Rename the downloaded file to client_secret.json and place it in your working directory.
  8. Authenticate ggsrun:

8-1. Open your terminal in the directory containing client_secret.json.

8-2. Run the command ggsrun auth.

8-3. Copy the URL displayed in the terminal, paste it into your browser, and authorize the requested scopes.

8-4. Copy the authorization code from your browser’s address bar and paste it back into the terminal.

To verify the setup, run ggsrun di to display information about your Google Drive.

2. Configuring Gemini CLI for ggsrun

To enable the Gemini CLI to use ggsrun, add the following tool definitions to your GEMINI.md file.

# Transferring file content between local PC and Google Drive using ggsrun

* GitHub Repository: [https://github.com/tanaikech/ggsrun](https://github.com/tanaikech/ggsrun)
* To download a file from Google Drive by filename, use this command:
  ```bash
  ggsrun d -f [filename]
  ```
* To download a file from Google Drive by file ID, use this command:
  ```bash
  ggsrun d -i [fileId]
  ```
* To upload files from the local PC to the root directory of Google Drive, use this command:
  ```bash
  ggsrun u -f "[filename1],[filename2],,,"
  ```
* To upload files from the local PC to a specific directory of Google Drive, use this command:
  ```bash
  ggsrun u -p [folderId] -f "[filename1],[filename2],,,"
  ```
* To search for files and folders on Google Drive, use this command:
  ```bash
  ggsrun sf -q "[search query of Drive API v3 ([https://developers.google.com/workspace/drive/api/guides/search-shareddrives](https://developers.google.com/workspace/drive/api/guides/search-shareddrives))]"
  ```
* To search for files and folders on Google Drive, use this command:
  ```bash
  ggsrun ls -s [filename]

3. Performance Test

The same 450 KB image file (sample.png) was used to test the indirect transfer method.

Test 1: Download from Google Drive

In this case, the file is downloaded without using the MCP server. At the MCP server, the file can be managed with the file ID.

Prompt:

Download a file "sample.png" from Google Drive using ggsrun.

Result: The command executed successfully and downloaded the file to the local working directory.

✔  Shell ggsrun d -f "sample.png" (Download a file from Google Drive using ggsrun by filename.)

✦ I have downloaded the file "sample.png" from Google Drive.

The download time was approximately 1 second.

Test 2: Upload to Google Drive

In this case, the file is uploaded without using the MCP server. At the MCP server, the file can be managed with the file ID.

Prompt:

Upload a file of "sample.png" to Google Drive using ggsrun.

Result: The command executed successfully and uploaded the file to the root folder of Google Drive.

✔  Shell ggsrun u -f "sample.png" (Uploads the file "sample.png" to Google Drive using ggsrun.)

✦ I have uploaded the file "sample.png" to Google Drive.

The upload time was approximately 1 second.

4. Conclusion on Indirect Transfer

The ggsrun CLI tool is highly effective for transferring files between a local PC and Google Drive via the Gemini CLI. Because prompts only contain small identifiers like filenames, they remain concise and avoid token limits. This method is fast, reliable, and supports advanced features like resumable uploads for large files. This functionality is a powerful complement to an MCP server built with Google Apps Script, enabling sophisticated, automated workflows.

Summary

  • Integrating Gemini CLI with a Google Apps Script MCP server allows for powerful, automated file processing workflows between your local machine and Google services.
  • The direct transfer method, which encodes file content into base64, is severely limited by API token counts and high costs, making it impractical for all but the smallest files.
  • The recommended approach is the indirect transfer method, which uses a CLI tool like ggsrun to interact with the Google Drive API.
  • The indirect method is highly efficient, scalable, and cost-effective because it only passes small file identifiers in the prompts, not the entire file content.
  • This combination of Gemini CLI, a dedicated tool like ggsrun, and an MCP server unlocks advanced automation possibilities, allowing users to manage files on Google Drive using simple, natural language commands.

 Share!