Skip to main content
Glama
google-drive-mcp
by JovemDaniel

Google Drive MCP Server

用于从 Claude Code 会话中读写 Google Sheets、Slides 和 Drive 的本地 Python MCP 服务器。


工作原理

该服务器使用 GCP 的 OAuth 2.0 Desktop App。首次使用时,会打开浏览器进行同意流程。批准后,会在本地保存一个 token.json。后续运行时,会复用该令牌并在过期时自动刷新——无需再次打开浏览器。

认证文件:

文件

说明

credentials.json

从 GCP Console 下载的 OAuth 应用凭据

token.json

首次登录后生成的访问令牌(自动刷新)


Related MCP server: google-workspace-mcp-with-script

初始配置(首次)

1. GCP 凭据

  1. 访问 GCP Console → APIs & Services → Credentials

  2. 创建一个 OAuth 2.0 Client ID(类型:Desktop App)

  3. 下载 JSON 并保存为 credentials.json 放在此文件夹中

  4. 在 GCP 项目中启用以下 API:

    • Google Sheets API

    • Google Slides API

    • Google Drive API

2. 安装依赖

Windows (PowerShell):

cd "C:\Users\<usuario>\Documents\MCP_Servers\googleDrive"
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt

macOS / Linux:

cd ~/Documents/MCP_Servers/googleDrive
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

3. 首次登录(OAuth)

手动运行以打开浏览器并授权:

Windows:

.venv\Scripts\activate
python main.py

macOS / Linux:

source .venv/bin/activate
python main.py

在浏览器中批准后,会创建 token.json 并启动服务器。可以用 Ctrl+C 结束——令牌会保存,供下次使用。


在 Claude Code 中注册

有两种注册 MCP 服务器的方式:

选项 A — 按项目(.mcp.json)

在项目根目录创建 .mcp.json 文件。MCP 仅在该项目中生效。

Windows:

{
  "mcpServers": {
    "google-drive": {
      "type": "stdio",
      "command": "C:\\Users\\<usuario>\\Documents\\MCP_Servers\\googleDrive\\.venv\\Scripts\\python.exe",
      "args": [
        "C:\\Users\\<usuario>\\Documents\\MCP_Servers\\googleDrive\\main.py"
      ]
    }
  }
}

macOS / Linux:

{
  "mcpServers": {
    "google-drive": {
      "type": "stdio",
      "command": "/Users/<usuario>/Documents/MCP_Servers/googleDrive/.venv/bin/python",
      "args": [
        "/Users/<usuario>/Documents/MCP_Servers/googleDrive/main.py"
      ]
    }
  }
}

选项 B — 用户全局(~/.claude/settings.json)

将 MCP 添加到 Claude Code 的全局设置中——在所有项目中均可用。

编辑 ~/.claude/settings.json(Windows:C:\Users\<usuario>\.claude\settings.json)并添加 mcpServers 键:

Windows:

{
  "mcpServers": {
    "google-drive": {
      "type": "stdio",
      "command": "C:\\Users\\<usuario>\\Documents\\MCP_Servers\\googleDrive\\.venv\\Scripts\\python.exe",
      "args": [
        "C:\\Users\\<usuario>\\Documents\\MCP_Servers\\googleDrive\\main.py"
      ]
    }
  }
}

macOS / Linux:

{
  "mcpServers": {
    "google-drive": {
      "type": "stdio",
      "command": "/Users/<usuario>/Documents/MCP_Servers/googleDrive/.venv/bin/python",
      "args": [
        "/Users/<usuario>/Documents/MCP_Servers/googleDrive/main.py"
      ]
    }
  }
}

当 MCP 仅针对特定项目时,使用选项 A。当需要在任何项目中访问时,使用选项 B。


环境变量(可选)

默认情况下,服务器会在自身文件夹中查找 credentials.json 和 token.json。要更改路径,请基于 .env.example 创建 .env:

GOOGLE_CREDENTIALS_PATH=credentials.json
GOOGLE_TOKEN_PATH=token.json

可用工具(17 个工具)

Google Sheets (7)

工具

说明

sheets_read_range

读取单元格区域的值

sheets_write_range

在区域中写入值

sheets_append_rows

在数据末尾添加行

sheets_batch_update_values

一次写入多个区域

sheets_get_spreadsheet

返回元数据(标题、工作表)

sheets_create_spreadsheet

创建新电子表格

sheets_add_sheet

向电子表格添加工作表

Google Slides (7)

工具

描述

slides_get_presentation

返回幻灯片的结构和标题

slides_get_slide_content

返回某张幻灯片的文本和形状

slides_create_slide

添加新幻灯片

slides_insert_text

在形状中插入文本

slides_replace_text

查找并替换文本

slides_delete_slide

删除幻灯片

slides_batch_update

执行 API 原始批量操作

Google Drive (3)

工具

描述

drive_list_files

列出/筛选文件(按类型、查询)

drive_search_files

按名称搜索文件

drive_get_file_metadata

返回文件的详细元数据

所有工具都接受直接 ID 或完整的 Google URL:

  • 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms

  • https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms/edit


故障排查

invalid_grant / 令牌过期且未自动刷新

删除 token.json 并重新运行 python main.py 以重新执行 OAuth 流程。

Windows:

Remove-Item ".\token.json"
python main.py

macOS / Linux:

rm token.json
python main.py

API 未启用

检查 GCP Console 中三个 API 是否已启用:Google Sheets API、Google Slides API、Google Drive API。

MCP 未出现在 Claude Code 中

检查 Python 可执行文件的路径是否正确:

  • Windows:.venv\Scripts\python.exe

  • macOS/Linux:.venv/bin/python

在 .mcp.json 或 settings.json 中使用绝对路径——相对路径无效。

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers