Mcp Google Sheets

Mcp Google Sheets

🚀 如何将Google Sheets与Claude集成:完整指南

本指南将详细介绍如何将Google Sheets与Claude集成,帮助你轻松实现相关功能。

🚀 快速开始

在开始集成之前,你需要完成一些准备工作,并按照特定步骤进行操作。

✨ 主要特性

  • 提供多种身份验证配置方式,满足不同场景需求。
  • 支持读取电子表格内容和创建新的工作表。
  • 可在Claude中进行集成配置,并实现数据的读取和处理。

📦 安装指南

在开始之前,请确保安装了以下工具:

  • Python 3.6或更高版本
  • pip
  • 谷歌浏览器或其他现代浏览器

💻 使用示例

基础用法

配置身份验证

方法一:使用服务账户(推荐)
  1. 创建服务账户
    • 登录Google Cloud Console
    • 进入项目设置 > 人员 > 添加成员。
    • 选择“服务账户”并赋予所需权限(如“Compute Engine Admin”或“Storage Admin”)。
  2. 下载JSON密钥文件
    • 完成后,点击项目名称进入项目概览页面。
    • 在左侧菜单中选择“APIs & Services” > “Credentials”。
    • 点击“Create Credentials”并选择“Service account key”。
    • 下载生成的JSON文件(通常命名为service-account-key.json)。
  3. 设置环境变量
export SERVICE_ACCOUNT_PATH="/path/to/service-account-key.json"
export DRIVE_FOLDER_ID="your-shared-folder-id"
方法二:使用OAuth 2.0
  1. 创建OAuth 2.0客户端ID
    • 在Google Cloud Console中,导航至“APIs & Services” > “Credentials”。
    • 点击“Create Credentials”并选择“OAuth client ID”。
    • 配置重定向URI(如http://localhost:8000/callback)。
  2. 获取用户同意
    • 运行脚本后,浏览器将自动打开授权页面。
    • 用户需要登录Google账户并允许权限。
  3. 设置环境变量
export CREDENTIALS_PATH="/path/to/credentials.json"
export TOKEN_PATH="/path/to/token.json"
方法三:使用CREDENTIALS_CONFIG(Base64编码)
  1. 将JSON密钥文件转换为Base64字符串
    • 使用工具如base64命令或在线转换器。
    • 示例:
echo '{"type":"service_account","project_id":"your-project-id","private_key_id":"key-id","private_key":"your-private-key","client_email":"service-account@your-project.iam.gserviceaccount.com","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/certs"}' | base64 -w 0 > credentials_base64.txt
  1. 设置环境变量
export CREDENTIALS_CONFIG="Base64-encoded-string"
export DRIVE_FOLDER_ID="your-shared-folder-id"

安装依赖

pip install google-api-python-client fastmcp

编写代码

读取电子表格内容
from fastmcp import FastMCP
from googleapiclient.discovery import build

# 初始化FastMCP客户端
client = FastMCP(
auth_method="service_account",
service_account_key_path="/path/to/service-account-key.json"
)

# 创建Sheets API服务
sheets_service = build('sheets', 'v4', credentials=client.credentials)

# 获取电子表格数据
spreadsheet_id = "your-spreadsheet-id"
result = sheets_service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range="A1:B2"
).execute()

print(result)
创建新的工作表
from fastmcp import FastMCP
from googleapiclient.discovery import build

# 初始化FastMCP客户端
client = FastMCP(
auth_method="service_account",
service_account_key_path="/path/to/service-account-key.json"
)

# 创建Sheets API服务
sheets_service = build('sheets', 'v4', credentials=client.credentials)

# 新建工作表请求体
new_sheet_request = {
"addSheet": {
"properties": {
"title": "New Sheet",
"sheetType": "GRID"
}
}
}

# 执行API调用
result = sheets_service.spreadsheets().batchUpdate(
spreadsheetId="your-spreadsheet-id",
body={"requests": [new_sheet_request]}
).execute()

print("新工作表已创建")

在Claude中集成

{
"configurations": {
"google_sheets": {
"auth_method": "service_account",
"service_account_key_path": "/path/to/service-account-key.json",
"spreadsheet_id": "your-spreadsheet-id",
"drive_folder_id": "your-shared-folder-id"
}
},
"enabled_providers": ["google_sheets"]
}

使用说明

  1. 启动服务
python your_script.py
  1. 发送请求
{
"provider": "google_sheets",
"operation": "read_range",
"params": {
"spreadsheet_id": "your-spreadsheet-id",
"range": "A1:A10"
}
}
  1. 处理响应
{
"status": "success",
"data": {
"values": [
["Header"],
["Row 1 Data"],
...
]
}
}

🔧 技术细节

额外注意事项

  • 错误处理:确保在代码中添加适当的异常处理,以捕获并重试可能的API调用错误。
  • 日志记录:建议启用日志记录功能,以便于调试和监控服务状态。
  • 安全提示
    • 避免直接将密钥文件提交到版本控制系统。
    • 定期更新和服务账户密钥。
    • 在生产环境中使用环境变量管理敏感信息。

通过以上步骤,您可以成功地将Google Sheets与Claude集成,并根据需要进行扩展和定制。

  • 0 关注
  • 0 收藏,18 浏览
  • system 提出于 2025-09-21 09:18

相似服务问题

相关AI产品