关于 Google Docs MCP Server
Google Docs MCP Server 将兼容 MCP 的助手连接到 Google Docs,用于管理文档。它通过专用工具支持创建、搜索、共享、更新和导出。
主要功能
- 01创建和删除文档
- 02读取和更新文档内容
- 03列出并搜索文档
- 04导出为其他格式
- 05与用户共享文档
- 06验证 API 连接
适用场景
- 01通过助手工作流创建文档
- 02按标题或内容查找文档
- 03更新并共享现有文档
- 04将文档导出为 PDF 或纯文本
MCP 相关
它向兼容 MCP 的助手提供 Google Docs 工具,包括创建、读取、编辑、搜索、共享和导出文档。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Google Docs MCP 服务
功能强大的模型上下文协议(MCP)服务实现,可无缝集成 Google Docs API,让 AI 助手能够创建、读取、更新和管理 Google Docs。
功能
- 使用自定义标题和内容创建新的 Google Docs
- 获取文档内容和元数据
- 使用新内容更新现有文档
- 列出所有可访问的文档
- 删除文档
- 将文档导出为不同格式(PDF、纯文本等)
- 与特定用户共享文档
- 按标题或内容搜索文档
- 验证连接和凭据
前提条件
- Node.js 18 或更高版本
- 已启用 Google Docs API 的 Google Cloud 项目
- 身份验证凭据(API 密钥、服务账号或 OAuth2)
安装
-
克隆此仓库:
git clone https://github.com/lkm1developer/google-docs-mcp-server.git cd google-docs-mcp-server -
安装依赖:
npm install -
创建包含 Google Cloud 凭据的
.env文件:# Required GOOGLE_CLOUD_PROJECT_ID=your-project-id # Choose one authentication method: # Option 1A: Service Account with file path (recommended for production) GOOGLE_APPLICATION_CREDENTIALS=path/to/your-service-account-key.json # Option 1B: Service Account with JSON content directly # Useful for environments where you can't create files GOOGLE_APPLICATION_CREDENTIALS_JSON={"type":"service_account","project_id":"...","private_key":"...","client_email":"..."} # Option 2: API Key (simpler for development) GOOGLE_API_KEY=your-api-key # Option 3: OAuth2 (required for user-specific operations) client_id=your-oauth-client-id client_secret=your-oauth-client-secret refresh_token=your-oauth-refresh-token
身份验证设置
服务账号身份验证
- 前往 Google Cloud Console
- 创建新项目或选择现有项目
- 启用 Google Docs API 和 Google Drive API
- 前往“IAM 与管理”>“服务账号”
- 创建新的服务账号
- 为其授予必要的角色(例如,“Docs API 用户”、“Drive API 用户”)
- 为服务账号创建并下载 JSON 密钥
- 在
.env文件中设置此 JSON 文件的路径
OAuth2 身份验证
对于需要用户授权的操作(例如创建/编辑文档):
- 前往 Google Cloud Console
- 创建新项目或选择现有项目
- 启用 Google Docs API 和 Google Drive API
- 前往“API 和服务”>“凭据”
- 创建 OAuth 客户端 ID 凭据(Web 应用类型)
- 添加已获授权的重定向 URI(例如,http://localhost:3000/oauth2callback)
- 记下客户端 ID 和客户端密钥
- 使用以下脚本获取刷新令牌:
// get-refresh-token.js
const { google } = require('googleapis');
const http = require('http');
const url = require('url');
const open = require('open');
const destroyer = require('server-destroy');
async function getRefreshToken() {
const oauth2Client = new google.auth.OAuth2(
'YOUR_CLIENT_ID',
'YOUR_CLIENT_SECRET',
'http://localhost:3000/oauth2callback'
);
const scopes = [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive'
];
const authorizeUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
prompt: 'consent'
});
console.log('Opening browser for authorization...');
open(authorizeUrl);
return new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
try {
const queryParams = url.parse(req.url, true).query;
if (queryParams.code) {
res.end('Authentication successful! You can close this window.');
server.destroy();
const { tokens } = await oauth2Client.getToken(queryParams.code);
console.log('\nRefresh Token:', tokens.refresh_token);
console.log('\nAdd this refresh token to your .env file as refresh_token');
resolve(tokens.refresh_token);
}
} catch (e) {
reject(e);
}
}).listen(3000);
destroyer(server);
});
}
getRefreshToken().catch(console.error);
使用以下命令运行此脚本:
npm install googleapis open server-destroy
node get-refresh-token.js
构建和运行
-
构建项目:
npm run build -
测试连接:
npx tsx src/test-connection.ts -
运行服务:
npm start或使用指定凭据运行:
npm start -- --GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json --GOOGLE_CLOUD_PROJECT_ID=your-project-id # Or with JSON content directly: npm start -- --GOOGLE_APPLICATION_CREDENTIALS_JSON='{"type":"service_account","project_id":"..."}' --GOOGLE_CLOUD_PROJECT_ID=your-project-id -
运行带身份验证的 SSE 服务:
npx mcp-proxy-auth node dist/index.js
在 SSE 服务中实现身份验证
SSE 服务使用 mcp-proxy-auth 包进行身份验证。实现身份验证的步骤如下:
-
安装该包:
npm install mcp-proxy-auth -
将
AUTH_SERVER_URL环境变量设置为指向你的 API 密钥验证端点:export AUTH_SERVER_URL=https://your-auth-server.com/verify -
运行带身份验证的 SSE 服务:
npx mcp-proxy-auth node dist/index.js -
SSE URL 将可通过以下地址访问:
localhost:8080/sse?apiKey=apikey将
apikey替换为你的实际 API 密钥以进行身份验证。
mcp-proxy-auth 包充当代理,用于:
- 拦截发往 SSE 服务的请求
- 根据身份验证服务验证 API 密钥
- 仅允许通过身份验证的请求到达 SSE 端点
Docker 支持
你也可以使用 Docker 运行服务:
-
构建 Docker 镜像:
docker build -t google-docs-mcp-server . -
运行容器:
docker run -p 8080:8080 \ -e GOOGLE_CLOUD_PROJECT_ID=your-project-id \ -e GOOGLE_APPLICATION_CREDENTIALS_JSON='{"type":"service_account","project_id":"..."}' \ -e client_id=your-client-id \ -e client_secret=your-client-secret \ -e refresh_token=your-refresh-token \ -e AUTH_SERVER_URL=https://your-auth-server.com/verify \ google-docs-mcp-server
MCP 集成
若要将此服务与 Claude 或其他兼容 MCP 的助手配合使用,请将其添加到 MCP 设置中:
{
"mcpServers": {
"google-docs": {
"command": "node",
"args": ["/path/to/google-docs-mcp-server/dist/index.js"],
"env": {
"GOOGLE_CLOUD_PROJECT_ID": "your-project-id",
"GOOGLE_APPLICATION_CREDENTIALS": "/path/to/your-service-account-key.json",
"GOOGLE_APPLICATION_CREDENTIALS_JSON": "{\"type\":\"service_account\",\"project_id\":\"...\"}",
"GOOGLE_API_KEY": "your-api-key",
"client_id": "your-oauth-client-id",
"client_secret": "your-oauth-client-secret",
"refresh_token": "your-oauth-refresh-token"
},
"disabled": false,
"autoApprove": []
}
}
}
可用工具
| 工具名称 | 描述 | 必需参数 |
|---|---|---|
google_docs_create | 创建新的 Google 文档 | title、content(可选) |
google_docs_get | 根据 ID 获取 Google 文档 | documentId |
google_docs_update | 使用新内容更新 Google 文档 | documentId、content、replaceAll(可选) |
google_docs_list | 列出已通过身份验证的用户可访问的 Google 文档 | pageSize(可选)、pageToken(可选) |
google_docs_delete | 删除 Google 文档 | documentId |
google_docs_export | 将 Google 文档导出为不同格式 | documentId、mimeType(可选) |
google_docs_share | 与特定用户共享 Google 文档 | documentId、emailAddress、role(可选) |
google_docs_search | 按标题或内容搜索 Google 文档 | query、pageSize(可选)、pageToken(可选) |
google_docs_verify_connection | 验证与 Google Docs API 的连接 | 无 |
使用示例
以下是一些工具用法示例:
创建新文档
{
"name": "google_docs_create",
"arguments": {
"title": "My New Document",
"content": "This is the content of my new document."
}
}
获取文档
{
"name": "google_docs_get",
"arguments": {
"documentId": "1Ax7vsdg3_YhKjkl2P0TZ5XYZ123456"
}
}
更新文档
{
"name": "google_docs_update",
"arguments": {
"documentId": "1Ax7vsdg3_YhKjkl2P0TZ5XYZ123456",
"content": "This is the new content.",
"replaceAll": true
}
}
搜索文档
{
"name": "google_docs_search",
"arguments": {
"query": "meeting notes",
"pageSize": 5
}
}
许可证
MIT
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。