Google Workspace文档操作服务

提供对Google Sheets和Google Docs的读写访问功能的MCP服务器。

  • by HarleyCoops
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 HarleyCoops/google-workspace-mcp

使用说明

谷歌工作区MCP服务器

MCP(模型上下文协议)服务器,提供对Google表格和Google文档的读/写访问。

可用工具

工具 说明
read_sheet 从谷歌表格电子表格中读取数据
read_doc 从Google文档中读取文本内容
list_sheets 列出电子表格中的所有工作表/选项卡
append_to_doc 将内容预置到Google文档
append_to_sheet 将行附加到电子表格
update_sheet 更新电子表格中的特定范围

快速开始

1.从GitHub安装

npm install -g github:HarleyCoops/google-workspace-mcp

或者克隆并在本地构建:

git clone https://github.com/HarleyCoops/google-workspace-mcp.git
cd google-workspace-mcp
npm install
npm run build

2.设置Google OAuth凭据

  1. 首选 Google 云控制台
  2. 创建新项目(或选择现有项目)
  3. 启用这些API:
    • 谷歌网页API
    • 谷歌文档API
    • Google Drive API
  4. 创建OAuth 2.0凭据(桌面应用程序类型)
  5. 下载为 credentials.json

3.生成OAuth令牌

pip install google-auth-oauthlib google-api-python-client
python regenerate_google_token.py

这创造了 token.json 使用您的OAuth凭据。

4.配置您的MCP客户端

设置 GOOGLE_TOKEN_PATH 指向您的环境变量 token.json 文件。


MCP配置

标准JSON配置(克劳德桌面、光标、VS代码等)

{
  "mcpServers": {
    "google-workspace": {
      "command": "npx",
      "args": ["github:HarleyCoops/google-workspace-mcp"],
      "env": {
        "GOOGLE_TOKEN_PATH": "/path/to/your/token.json"
      }
    }
  }
}

如果全局安装

{
  "mcpServers": {
    "google-workspace": {
      "command": "google-workspace-mcp",
      "env": {
        "GOOGLE_TOKEN_PATH": "/path/to/your/token.json"
      }
    }
  }
}

如果本地克隆

{
  "mcpServers": {
    "google-workspace": {
      "command": "node",
      "args": ["/path/to/google-workspace-mcp/build/index.js"],
      "env": {
        "GOOGLE_TOKEN_PATH": "/path/to/your/token.json"
      }
    }
  }
}

Emergent.sh设置

对于 Emergent.sh,您有两个选择:

选项A:用作stdio MCP(如果支持)

在Emergent的MCP配置中:

{
  "command": "npx",
  "args": ["github:HarleyCoops/google-workspace-mcp"],
  "env": {
    "GOOGLE_TOKEN_PATH": "/path/to/token.json"
  }
}

选项B:自主机作为HTTP端点

如果Emergent需要HTTP/SSE端点,则需要包装此服务器。请参阅 MCP HTTP传输文档 了解详情。


环境变量

变量 描述 默认值
GOOGLE_TOKEN_PATH OAuth token.json的路径 ./token.json
DAILY_REPO_PATH .env加载的基本路径 C:\Users\chris\Daily

令牌生成脚本

将此另存为 regenerate_google_token.py:

from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = [
    'https://www.googleapis.com/auth/drive.readonly',
    'https://www.googleapis.com/auth/documents.readonly',
    'https://www.googleapis.com/auth/spreadsheets.readonly',
    'https://www.googleapis.com/auth/drive.file'
]

def main():
    flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
    creds = flow.run_local_server(port=0)
    with open('token.json', 'w') as f:
        f.write(creds.to_json())
    print("Token saved to token.json")

if __name__ == '__main__':
    main()

使用示例

阅读表格

从URL中提取电子表格ID:

https://docs.google.com/spreadsheets/d/1Zlxn88pgMi0WAKFo.../edit
                                        ^^^^^^^^^^^^^^^^
                                        This is the ID

阅读文档

从URL中提取文档ID:

https://docs.google.com/document/d/17p5DfXbyEYhsMy.../edit
                                   ^^^^^^^^^^^^^^^
                                   This is the ID

安全说明

  • 永不承诺 credentials.json 或 token.json 到版本控制
  • token.json 包含提供持久访问的刷新令牌
  • 随时撤销访问权限:https://myaccount.google.com/permissions

许可证

麻省理工学院