Google Docs MCP 服务器
该项目提供了一个与 Google Docs API 配合使用的 MCP(模型上下文协议)服务器。我们正在实现一个使用生成式人工智能来操作 Google Docs 的界面。
功能
该 MCP 服务器提供以下功能:
- 阅读 Google Docs 文档
- 创建新的 Google Docs 文档
- 更新现有的 Google Docs 文档
- 搜索 Google Docs 文档
技术堆栈
- Node.js (建议使用 v14 或更高版本)
- TypeScript
- @modelcontextprotocol/sdk - MCP SDK 的官方实现
- Google APIs Node.js 客户端- 访问 Google API
先决条件
- Node.js(建议使用 v14 或更高版本)
- npm 或 yarn
- Google Cloud Platform 项目和访问凭据
设置
1. 克隆或下载项目
2.安装依赖项
3. Google云平台设置
- 在Google Cloud Console中创建项目(或选择现有项目)
- 启用 Google Drive API 和 Google Docs API
- 创建 OAuth 2.0 客户端 ID 并下载凭据
- 将下载的凭证文��作为
credentials.json
放在项目根目录中。
4. 偏好设置
- 在项目根目录中创建一个
.env
文件并在那里设置环境变量:
环境变量解释:
NODE_ENV
:应用程序的执行环境(开发、生产、测试)LOG_LEVEL
:日志详细级别(ERROR、WARN、INFO、DEBUG、TRACE)LOG_USE_STDERR
:是否将日志输出到标准错误输出(MCP 规范使用标准错误输出)SERVER_NAME
:MCP 服务器名称SERVER_VERSION
:MCP 服务器版本CREDENTIALS_PATH
:Google API 凭证文件的路径TOKEN_PATH
:存储身份验证令牌的路径
- 启动开发服务器并获取令牌:执行后终端中会显示授权URL。在浏览器中访问该URL,使用您的Google帐户登录并执行授权。授权完成后,复制显示的授权码,粘贴到终端,然后按 Enter。这将生成一个
token.json
文件,并从此自动对您进行身份验证。
构建并运行
建造
执行
作为常规服务器运行:
以开发模式运行:
用作 MCP 服务器
该项目是一个符合模型上下文协议(MCP)规范的服务器。您可以直接从 MCP 客户端(Cursor、Claude.ai 等)连接。
MCP 客户端上的设置
使用光标设置
要与 Cursor 一起使用它,请将以下设置添加到.cursor/mcp.json
文件中:
其他 MCP 客户端
其他 MCP 客户端使用标准输入/输出 (stdio) 进行通信。根据您的客户端设置指定适当的命令。
提供的 MCP 工具
read_google_document
阅读 Google Docs 文档的内容。
参数:
documentId
(字符串):要读取的 Google Docs 文档的 ID。
使用示例:
创建Google文档
创建一个新的 Google Docs 文档。
参数:
title
(字符串):新文档的标题。content
(字符串,可选):文档的初始内容。
使用示例:
更新谷歌文档
更新现有的 Google Docs 文档。
参数:
documentId
(字符串):要更新的 Google Docs 文档的 ID。content
(字符串):要添加或更新的内容。startPosition
(数字,可选):开始更新的位置。endPosition
(数字,可选):结束更新的位置。
使用示例:
search_google_documents
搜索 Google Docs 文档。
参数:
query
(字符串):搜索查询。maxResults
(数字,可选):要检索的最大结果数(默认值:10)。
使用示例:
程序使用示例
从 TypeScript 或 JavaScript 程序使用 MCP 客户端的示例:
故障排除
如果 Cursor 发生连接错误
- 完全重启 Cursor。
- 请确保
.cursor/mcp.json
设置正确。 - 手动启动 MCP 服务器并检查其是否正常工作:验证在运行此命令时是否看到消息“Google Docs MCP Server 已启动”,以及该进程是否继续运行而不会退出。
- 检查 Cursor 设置中的“MCP 服务器”部分,并确保列出了“google-docs”服务器。
如果您收到 Google 身份验证错误
- 确保
credentials.json
文件正确放置在项目根目录中。 - 如果
token.json
文件存在,请删除它并再次尝试验证。 - 在 Google Cloud Console 中验证您的项目是否启用了 Google Drive API 和 Google Docs API。
扩展和配置
此 MCP 服务器在设计时考虑了可扩展性,允许您添加新功能,例如:
src/googleDocsService.ts
- 向 GoogleDocsService 类添加新方法。src/index.ts
- 定义新工具并在服务器上注册它们
笔记
- 第一次运行该应用程序时,将显示 Google 身份验证的授权屏幕。身份验证后,令牌将保存到文件中并在后续运行时自动使用。
- 根据您对 API 的使用情况,可能会收取 Google Cloud Platform 费用。
执照
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
模型上下文协议服务器,为 AI 模型提供与 Google Docs 交互的接口,支持读取、创建、更新和搜索 Google 文档。
Related MCP Servers
- AsecurityAlicenseAqualityA Model Context Protocol server that enables AI assistants like Claude to interact with Outline document services, supporting document searching, reading, creation, editing, and comment management.Last updated -2519PythonMIT License
- AsecurityAlicenseAqualityA Model Context Protocol server that enables AI assistants to interact with Confluence content, supporting operations like retrieving, searching, creating, and updating pages and spaces.Last updated -93TypeScriptMIT License
- AsecurityAlicenseAqualityA Model Context Protocol server implementation that enables AI assistants like Claude to perform Google searches and retrieve web data directly through natural language requests.Last updated -1753TypeScriptMIT License
- -securityAlicense-qualityA Model Context Protocol server that enables AI assistants like Claude to read from, append to, and format text in Google Documents programmatically.Last updated -24TypeScriptMIT License