article
README
🚀 Google Drive MCP 服务器
这是一个功能强大的模型上下文协议(MCP)服务器,它能够与Google Drive实现无缝集成。借助该服务器,AI模型可以轻松地在Google Drive中进行文件搜索、列表查看以及内容读取操作。
🚀 快速开始
前提条件
- Node.js(v16 或更高版本)
- npm 或 yarn
- 一个Google Cloud项目
- Google Workspace或个人Google账户
详细的Google Cloud设置
-
创建Google Cloud项目
- 访问Google Cloud控制台。
- 点击“新建项目”。
- 输入项目名称(例如,“MCP GDrive Server”)。
- 点击“创建”。
- 等待项目创建完成并选择它。
-
启用Google Drive API
- 前往API库。
- 搜索“Google Drive API”,点击“启用”。
-
设置OAuth 2.0客户端ID
- 进入Credentials部分。
- 点击“添加Credentials”并选择“OAuth 2.0客户端ID”。
- 配置项目详细信息,指定重定向URI(例如,
urn:ietf:wg:oauth:2.0:oob)。 - 获取生成的JSON密钥文件,并命名为
gcp-oauth.json。
-
安装依赖
- 在项目根目录下运行:
npm install @google/drive --save
✨ 主要特性
工具
1. gdrive_search
可在你的Google Drive中使用强大的全文搜索功能查找文件。
- 输入:
{
"query": "string (你的搜索查询)"
}
- 输出:包含以下信息的文件列表:
- 文件名
- MIME类型
- 文件ID
- 最后修改时间
- 文件大小
2. gdrive_read_file
使用Google Drive文件ID直接读取文件内容。
- 输入:
{
"file_id": "string (Google Drive文件ID)"
}
- 输出:适当格式转换后的文件内容
自动处理文件格式
服务器能够智能处理不同的Google Workspace文件类型:
- 📝 Google Docs → Markdown
- 📊 Google Sheets → CSV
- 📊 Google Presentations → 文本
- 🎨 Google Drawings → PNG
- 📄 文本/JSON文件 → UTF - 8文本
- 📦 其他文件 → Base64编码
📦 安装指南
克隆仓库并安装依赖:
git clone https://github.com/your-repository/mcp-gdrive.git
cd mcp-gdrive
npm install
🔑 认证
按照以下步骤配置和运行认证:
- 创建
config.json文件,添加你的Google Cloud项目ID和密钥路径:
{
"projectId": "your-project-id",
"keyFile": "path/to/gcp-oauth.json"
}
- 在终端中运行以下命令进行认证:
npm run authenticate
这将引导你完成OAuth 2.0流程,获取必要的访问令牌。
💻 使用示例
基础用法
// 搜索文件示例
const { searchFiles } = require('./src/drive');
async function main() {
const query = "quarterly report";
const result = await searchFiles(query);
console.log(result.files);
}
main().catch(console.error);
高级用法
// 读取文件内容示例
const { readFile } = require('./src/drive');
async function main() {
const fileId = "your-file-id";
const contents = await readFile(fileId);
console.log(contents);
}
main().catch(console.error);
📚 详细文档
故障排除
遇到问题时:
- 验证Google Cloud项目的设置。
- 确保所有必要的OAuth范围已启用。
- 检查凭证是否正确放置在
credentials目录中。 - 验证文件权限和访问权限。
额外资源
📄 许可证
本MCP服务器根据MIT License授权。查看LICENSE文件以获取详细信息。
扫码联系在线客服