mcp-google-sheets

repository·main·Indexed 21 days ago

https://github.com/xing5/mcp-google-sheets

A Python-based Model Context Protocol (MCP) server that integrates with Google Drive and Google Sheets via the Google Sheets API. It provides AI assistants with tools for automation, data manipulation, and CRUD operations, including reading/writing cell data, managing tabs, creating spreadsheets, and adding charts. Supports multiple authentication methods including Service Accounts, OAuth 2.0, and Application Default Credentials.

Tokens
8.4K
Snippets
27
Records
36
Agent score
76%

What's inside mcp-google-sheets

  1. What is mcp-google-sheets?

    main
    mcp-google-sheets is a Python-based Model Context Protocol (MCP) server that acts as a bridge between MCP-compatible clients (such as Claude Desktop) and the Google Sheets API. It enables AI assistants to interact with Google Spreadsheets through a set of specialized tools for automation, data manipulation, and CRUD operations.
  2. Reduce context usage with Tool Filtering

    main

    By default, the server exposes 19 tools, consuming approximately 13,000 tokens. To save context window space in your AI conversations, you can enable only the specific tools you need using either a command-line argument or an environment variable.

    Method 1: Using --include-tools argument

    Add the --include-tools flag followed by a comma-separated list of tool names to the args array in your client configuration.

    Method 2: Using ENABLED_TOOLS environment variable

    Set the ENABLED_TOOLS environment variable in your client configuration with a comma-separated list of tool names.

    Note: Tool names must be exact and contain no spaces.

    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": [
            "mcp-google-sheets@latest",
            "--include-tools",
            "get_sheet_data,update_cells,list_spreadsheets,list_sheets"
          ],
          "env": {
            "SERVICE_ACCOUNT_PATH": "/path/to/credentials.json"
          }
        }
      }
    }
  3. Google Cloud Platform Setup Requirements

    main

    Before using the MCP Google Sheets server, you must complete the following GCP configuration:

    1. Create/Select a GCP Project in the Google Cloud Console.
    2. Enable APIs: In the 'APIs & Services' -> 'Library' section, you must enable:
      • Google Sheets API
      • Google Drive API
    3. Configure Credentials: Choose an authentication method (Service Account is recommended).
  4. Configure uvx with Application Default Credentials (ADC)

    main

    You can use Google's Application Default Credentials (ADC) in two ways:

    1. Via Environment Variable: Set GOOGLE_APPLICATION_CREDENTIALS to the path of your service account JSON.
    2. Via gcloud CLI: If you have already authenticated via gcloud, no environment variables are needed in the config.

    Prerequisites for gcloud method:

    1. Run: gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive
    2. Set quota project: gcloud auth application-default set-quota-project <project_id>
    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": ["mcp-google-sheets@latest"],
          "env": {
            "GOOGLE_APPLICATION_CREDENTIALS": "/path/to/service-account.json"
          }
        }
      }
    }
  5. Configure uvx with CREDENTIALS_CONFIG (Base64 Service Account)

    main

    Instead of a file path, you can provide a Base64 encoded string of your service account configuration via the CREDENTIALS_CONFIG environment variable. DRIVE_FOLDER_ID is still required for Service Account context.

    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": ["mcp-google-sheets@latest"],
          "env": {
            "CREDENTIALS_CONFIG": "ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAi...",
            "DRIVE_FOLDER_ID": "your_shared_folder_id_here"
          }
        }
      }
    }
  6. Configure mcp-google-sheets for Claude Desktop

    main

    To use this MCP server with Claude Desktop, add a configuration block to your claude_desktop_config.json under the mcpServers key.

    Important for macOS Users: If you encounter a spawn uvx ENOENT error, you must use the absolute path to the uvx binary (e.g., /Users/yourusername/.local/bin/uvx) instead of just uvx.

    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": ["mcp-google-sheets@latest"],
          "env": {
            "SERVICE_ACCOUNT_PATH": "/full/path/to/your/service-account-key.json",
            "DRIVE_FOLDER_ID": "your_shared_folder_id_here"
          }
        }
      }
    }
  7. Configure uvx with OAuth 2.0

    main

    Use OAuth 2.0 if you want to act as a specific user. You must provide the path to your credentials.json and a token.json file.

    Note: A browser window may open for Google login during the first use. Ensure the TOKEN_PATH directory is writable.

    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": ["mcp-google-sheets@latest"],
          "env": {
            "CREDENTIALS_PATH": "/full/path/to/your/credentials.json",
            "TOKEN_PATH": "/full/path/to/your/token.json"
          }
        }
      }
    }
  8. Configure local development environment

    main

    If you are developing the server from a cloned repository, use uv run with the --directory flag to point to the project root.

    {
      "mcpServers": {
        "mcp-google-sheets-local": {
          "command": "uv",
          "args": [
            "run",
            "--directory",
            "/path/to/your/mcp-google-sheets",
            "mcp-google-sheets"
          ],
          "env": {
            "SERVICE_ACCOUNT_PATH": "/path/to/your/mcp-google-sheets/service_account.json",
            "DRIVE_FOLDER_ID": "your_drive_folder_id_here"
          }
        }
      }
    }
  9. Quick Start with uvx

    main

    The fastest way to run the server is using uvx, which automatically downloads and runs the latest version.

    Prerequisites

    1. Google Cloud Setup: You must configure Google Cloud Platform credentials and enable the necessary APIs. Using a Service Account is strongly recommended.
    2. Install uv: uvx is part of the uv package manager.

    Installation Steps

    1. Install uv

    • macOS / Linux: curl -LsSf https://astral.sh/uv/install.sh | sh
    • Windows: powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
    • via pip: pip install uv

    2. Set Environment Variables (Service Account method) Replace the placeholders with your actual service account JSON path and Google Drive folder ID.

    • Linux/macOS:
      export SERVICE_ACCOUNT_PATH="/path/to/your/service-account-key.json"
      export DRIVE_FOLDER_ID="YOUR_DRIVE_FOLDER_ID"
    • Windows CMD:
      set SERVICE_ACCOUNT_PATH="C:\path\to\your\service-account-key.json"
      set DRIVE_FOLDER_ID="YOUR_DRIVE_FOLDER_ID"
    • Windows PowerShell:
      $env:SERVICE_ACCOUNT_PATH = "C:\path\to\your\service-account-key.json"
      $env:DRIVE_FOLDER_ID = "YOUR_DRIVE_FOLDER_ID"

    3. Run the Server

    uvx mcp-google-sheets@latest

    4. Connect your MCP Client Configure your client (e.g., Claude Desktop) to launch the server. It is recommended to run the command manually once to verify the setup works.

    uvx mcp-google-sheets@latest
  10. Configure uvx with Service Account (Recommended)

    main

    The recommended way to run the server is using a Google Service Account. You must provide the path to your service account JSON key and a DRIVE_FOLDER_ID to provide context for the files the service account can access.

    macOS Note: Use the full path to uvx if the command is not found.

    {
      "mcpServers": {
        "google-sheets": {
          "command": "uvx",
          "args": ["mcp-google-sheets@latest"],
          "env": {
            "SERVICE_ACCOUNT_PATH": "/full/path/to/your/service-account-key.json",
            "DRIVE_FOLDER_ID": "your_shared_folder_id_here"
          }
        }
      }
    }
  11. Configure Authentication for mcp-google-sheets

    main

    The server requires Google credentials to access Sheets and Drive. You can choose from four authentication methods depending on your environment. The server checks for credentials in the following priority order:

    1. CREDENTIALS_CONFIG (Base64 encoded JSON content)
    2. SERVICE_ACCOUNT_PATH (Path to Service Account JSON)
    3. CREDENTIALS_PATH (Path to OAuth JSON)
    4. Application Default Credentials (ADC) (Automatic fallback)

    Best for headless environments.

    1. Create a Service Account in the GCP Console and download the JSON key.
    2. Create a folder in Google Drive and note its Folder ID.
    3. Share the folder with the Service Account's client_email and grant Editor access.
    4. Set SERVICE_ACCOUNT_PATH (path to JSON) and DRIVE_FOLDER_ID.

    Method B: OAuth 2.0 (Interactive / Personal Use)

    Best for local development where a browser login is acceptable.

    1. Configure an OAuth consent screen in GCP with .../auth/spreadsheets and .../auth/drive scopes.
    2. Create an OAuth client ID (Desktop app type) and download the JSON.
    3. Set CREDENTIALS_PATH (path to JSON) and TOKEN_PATH (where to save the refresh token).

    Method C: Direct Credential Injection (Advanced)

    Best for Docker, Kubernetes, or CI/CD to avoid managing secret files.

    1. Convert your credentials JSON file to a Base64 string.
    2. Set the CREDENTIALS_CONFIG environment variable to that Base64 string.

    Method D: Application Default Credentials (ADC)

    Best for Google Cloud environments (GKE, Compute Engine, etc.).

    1. Use gcloud auth application-default login locally with required scopes.
    2. Or, set the standard GOOGLE_APPLICATION_CREDENTIALS environment variable.
    3. Or, attach a service account to your Google Cloud resource.
    # Example: Service Account setup
    export SERVICE_ACCOUNT_PATH="/path/to/service-account.json"
    export DRIVE_FOLDER_ID="your_folder_id_here"
    
    # Example: OAuth 2.0 setup
    export CREDENTIALS_PATH="/path/to/credentials.json"
    export TOKEN_PATH="/path/to/token.json
    
    # Example: Direct Injection (Base64)
    export CREDENTIALS_CONFIG="ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb..."
  12. Run the mcp-google-sheets server

    main

    Depending on your use case, you can run the server using uvx, local development mode, or Docker.

    This is the easiest method for most users. It fetches and runs the package temporarily.

    For Development (Cloning the Repo)

    If you need to modify the source code:

    1. Clone the repository.
    2. Set your authentication environment variables.
    3. Run using uv run.

    Using Docker (SSE transport)

    To run the server in a container using SSE (Server-Sent Events) transport. It is recommended to use CREDENTIALS_CONFIG (Base64) in Docker to avoid mounting files.

    # Quick Start with uvx
    uvx mcp-google-sheets@latest
    
    # Development mode
    git clone https://github.com/yourusername/mcp-google-sheets.git && cd mcp-google-sheets
    uv run mcp-google-sheets
    
    # Docker (SSE on port 8000)
    docker build -t mcp-google-sheets .
    docker run --rm -p 8000:8000 \
      -e HOST=0.0.0.0 \
      -e PORT=8000 \
      -e CREDENTIALS_CONFIG=YOUR_BASE64_CREDENTIALS \
      -e DRIVE_FOLDER_ID=YOUR_DRIVE_FOLDER_ID \
      mcp-google-sheets