使用说明
Google Drive MCP服务器
用于Google Drive与OAuth 2.0身份验证集成的模型上下文协议(MCP)服务器。
特性
- OAuth 2.0身份验证:通过自动令牌刷新保护Google OAuth流
- MCP协议支持:全面实施模型上下文协议
- 谷歌云端硬盘运营:
- 获取用户配置文件信息
- 列出共享驱动器
- 使用高级查询搜索文件
- 获取最近修改的文档
- 获取文件内容(包括谷歌文档导出)
- 会话管理:具有自动令牌刷新功能的持久会话
- TypeScript:完全使用TypeScript类型,以获得更好的开发体验
先决条件
- Node.js 18+
- 启用驱动器API的谷歌云项目
- OAuth 2.0凭据(客户端ID和客户端密钥)
设置
1.谷歌云配置
- 首选 谷歌云控制台
- 创建新项目或选择现有项目
- 启用Google Drive API
- 创建OAuth 2.0凭据:
- 应用程序类型:Web应用程序
- 授权重定向URI:
http://localhost:3002/oauth/callback
- 复制您的客户端ID和客户端密码
2.安装
# Clone or navigate to the project directory
cd google-drive-mcp-server
# Install dependencies
npm install
3.配置
创建一个 .env 根目录中的文件:
cp env.example .env
编辑 .env 根据您的配置:
# Server Configuration
PORT=3002
NODE_ENV=development
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:3002/oauth/callback
# Frontend URL (where to redirect after OAuth)
FRONTEND_URL=http://localhost:5173
# MCP Server Configuration
MCP_SERVER_NAME=google-drive-mcp-server
MCP_SERVER_VERSION=1.0.0
用法
发展模式
npm run dev
生产模式
# Build
npm run build
# Start
npm start
API终点
健康检查
GET /health
OAuth流
1.启动授权
GET /oauth/authorize?userId={userId}&state={state}
答复:
{
"authorizationUrl": "https://accounts.google.com/o/oauth2/v2/auth?...",
"state": "your-state-value"
}
2.OAuth回调(自动处理)
GET /oauth/callback?code={code}&state={state}
3.刷新令牌
POST /oauth/refresh
Content-Type: application/json
{
"userId": "user123",
"refreshToken": "refresh_token_here"
}
4.断开连接
POST /oauth/disconnect
Content-Type: application/json
{
"userId": "user123"
}
MCP端点
POST /mcp
GET /mcp
DELETE /mcp
MCP端点遵循工具执行的模型上下文协议规范。
MCP工具
1.drive_get.profile
获取当前Drive用户的个人资料信息。
输入:
{
"userId": "user123"
}
输出:
{
"user": {
"displayName": "John Doe",
"emailAddress": "john@example.com",
"permissionId": "123456789"
}
}
所需范围:
userinfo.emailuserinfo.profile
2.驱动器_列表_驱动器
列出用户可访问的共享驱动器。
输入:
{
"userId": "user123"
}
输出:
{
"drives": [
{
"id": "drive123",
"name": "Team Drive",
"kind": "drive#drive"
}
],
"count": 1
}
所需范围:
drive.readonly
3.驾驶探索
使用具有Google Drive搜索语法的查询搜索Drive文件。
输入:
{
"userId": "user123",
"query": "name contains 'report' and mimeType='application/pdf'",
"maxResults": 50,
"orderBy": "modifiedTime desc",
"includeItemsFromAllDrives": false,
"supportsAllDrives": false
}
输出:
{
"files": [
{
"id": "file123",
"name": "Annual Report.pdf",
"mimeType": "application/pdf",
"size": "1048576",
"modifiedTime": "2025-01-15T12:00:00Z",
"webViewLink": "https://drive.google.com/...",
"thumbnailLink": "https://..."
}
],
"count": 1
}
所需范围:
drive.readonly
4.drive_recent_documents
返回最近修改的文档。
输入:
{
"userId": "user123",
"maxResults": 20
}
输出:
{
"documents": [
{
"id": "doc123",
"name": "Meeting Notes.docx",
"mimeType": "application/vnd.google-apps.document",
"modifiedTime": "2025-01-15T14:30:00Z",
"webViewLink": "https://docs.google.com/..."
}
],
"count": 1
}
所需范围:
drive.readonly
5.驱动_蚀刻
下载Drive文件的内容。
输入:
{
"userId": "user123",
"fileId": "file123"
}
输出:
{
"file": {
"id": "file123",
"name": "document.txt",
"mimeType": "text/plain",
"content": "File content here...",
"size": "1024",
"modifiedTime": "2025-01-15T12:00:00Z"
}
}
所需范围:
drive.readonly
注: Google文档文件会自动导出为纯文本/CSV格式。
与ZeroTwoApi集成
要将此Google Drive MCP服务器与ZeroTwoApi后端集成:
1.更新前端OAuth配置
在 ZeroTwo/src/services/oauthProviders.js,Google Drive配置已更新:
google_drive: {
provider: "google_drive",
clientId: import.meta.env.VITE_GOOGLE_CLIENT_ID,
flowType: OAuthFlowType.GOOGLE_GIS,
scopes: [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.file",
],
backendExchangeEndpoint: "/api/auth/google-drive/callback",
backendRefreshEndpoint: "/api/auth/google-drive/refresh",
mcpEnabled: true,
mcpEndpoint: "http://localhost:3002/mcp",
},
2.后端路由
以下路线已在中创建 ZeroTwoApi/routes/ai/tools/google-drive.js:
POST /api/ai/tools/google-drive-列出文件POST /api/ai/tools/google-drive/search-搜索文件POST /api/ai/tools/google-drive/recent-获取最新文档POST /api/ai/tools/google-drive/profile-获取用户资料POST /api/ai/tools/google-drive/drives-列出共享驱动器POST /api/ai/tools/google-drive-content-获取文件内容(使用现有路由)
3.创建MCP集成
OAuth身份验证后,在数据库中创建MCP集成:
INSERT INTO mcp_integrations (
user_id,
name,
provider,
endpoint,
mcp_type,
connection_status,
auth_type,
description
) VALUES (
'user_id_here',
'Google Drive',
'google_drive',
'http://localhost:3002/mcp',
'remote_mcp',
'connected',
'oauth',
'Google Drive integration via MCP'
);
建筑
┌─────────────────┐
│ Frontend │
│ (ZeroTwo) │
└────────┬────────┘
│
│ OAuth Flow
▼
┌─────────────────┐ ┌──────────────────┐
│ Google Drive │◄────►│ Google OAuth │
│ MCP Server │ │ & Drive API │
└────────┬────────┘ └──────────────────┘
│
│ MCP Protocol
▼
┌─────────────────┐
│ Backend │
│ (ZeroTwoApi) │
└─────────────────┘
发展
项目结构
google-drive-mcp-server/
├── src/
│ ├── auth/
│ │ └── oauth-manager.ts # OAuth token management
│ ├── config/
│ │ └── index.ts # Configuration management
│ ├── drive/
│ │ └── client.ts # Google Drive API wrapper
│ ├── mcp/
│ │ ├── server.ts # MCP server implementation
│ │ └── tools.ts # MCP tool definitions
│ ├── types/
│ │ └── index.ts # TypeScript type definitions
│ ├── utils/
│ │ └── logger.ts # Logging utility
│ └── index.ts # Entry point
├── package.json
├── tsconfig.json
├── env.example
└── README.md
脚本
npm run dev-使用热重新加载启动开发服务器npm run build-将TypeScript构建为JavaScriptnpm start-启动生产服务器npm run lint-运行ESLintnpm run format-使用Prettier格式化代码npm run type-check-TypeScript类型检查
Google Drive搜索查询语法
这 drive_search 该工具支持Google Drive的完整搜索语法:
基本运算符
name contains 'text'-文件名包含文本fullText contains 'text'-内容包含文本mimeType = 'type'-特定MIME类型modifiedTime > '2025-01-01'-日期后修改trashed = false-排除被丢弃的文件
逻辑运算符
and-这两个条件都必须为真or-任一条件都可以为真not-否定一个条件
示例
// Find PDFs modified in the last 7 days
"mimeType='application/pdf' and modifiedTime > '2025-01-08'"
// Find Google Docs in a specific folder
"'folderId' in parents and mimeType='application/vnd.google-apps.document'"
// Find files shared with me
"sharedWithMe = true and trashed = false"
// Find starred files
"starred = true"
安全考虑
- OAuth令牌:令牌存储在内存中。对于生产,请使用安全的数据库或加密存储。
- 跨域资源共享:为生产适当配置CORS源。
- 超文本传输安全协议:在生产环境中使用HTTPS进行安全通信。
- 环境变量:从不承诺
.env文件夹。在生产中使用安全的秘密管理。 - 速率限制:对生产部署实施速率限制。
- 令牌刷新:使用5分钟缓冲区实现自动令牌刷新。
故障排除
OAuth错误
- “缺少必需的环境变量”:检查您的
.env文件包含所有必需的变量 - “交换授权码失败”:在Google Cloud控制台中验证您的重定向URI是否完全匹配
- “令牌已过期”:如果刷新令牌可用,服务器会自动刷新令牌
MCP连接问题
- “没有有效的会话ID”:确保MCP客户端发送正确的会话标头
- “需要身份验证”:用户需要先完成OAuth流程
Google Drive API错误
- 401未经授权:令牌已过期,请尝试刷新或重新验证
- 403禁止:检查是否已授予所有必需的范围
- 404未找到:文件ID不存在或用户没有访问权限
- 速率限制:实现重试的指数回退
与Gmail MCP服务器的比较
此Google Drive MCP服务器遵循与Gmail MCP服务器相同的模式:
相似之处
- OAuth 2.0身份验证流程
- MCP协议实现
- 具有自动令牌刷新功能的会话管理
- TypeScript实现
- Express.js服务器
差异
| 功能 | Gmail | 谷歌云端硬盘 |
|---|---|---|
| 港口 | 3001 | 3002 |
| 主要API | Gmail API | 驱动API |
| 主要作战行动 | 读取/发送电子邮件、管理标签 | 搜索文件、读取内容、列出驱动器 |
| 范围 | gmail.* | 驱动器。只读,用户信息。* |
| 内容导出 | 电子邮件正文解析 | 谷歌文档导出 |
许可证
麻省理工学院
贡献
欢迎投稿!请随时提交拉取请求。
由ZeroTwo提供技术支持
此Google Drive MCP连接器是 ZeroTwo人工智能平台 --一体化的人工智能工作区,让您通过GPT-5、Claude和Gemini搜索、读取和管理您的Google Drive文件。
| 🌐 ZeroTwo——一个应用程序中的所有AI模型 | 使用GPT-5、Claude和Gemini在一个地方搜索和分析您的Google Drive文件。 |
| ✨ 零两个功能 | AI文档分析、文件管理、网络搜索和MCP驱动的Google Drive工具。 |
| 🤖 人工智能模型——GPT-5、克劳德和双子座 | 使用世界上最好的人工智能来汇总文档、查找文件和自动化Drive工作流程。 |
| 🔌 ZeroTwo连接器和集成 | 将Google Drive、Gmail、Google Docs、Dropbox等连接到您的AI工作流程。 |
| 💰 零二定价 | 一个替代ChatGPT Plus、Claude Pro和Gemini Advanced的订阅。 |
| 📝 ZeroTwo博客 | 人工智能文档提示、谷歌工作区指南和ZeroTwo产品更新。 |
| 🚀 免费试用ZeroTwo | 让人工智能与您的谷歌云端硬盘协同工作——立即免费开始。 |
专为ZeroTwo打造 --使用此Google Drive MCP服务器 ZeroTwo的AI连接器系统 通过AI助手中的自然语言搜索文件、阅读文档和管理Drive存储。
