Google Docs MCP 服务器
该项目提供了一个与 Google Docs API 配合使用的 MCP(模型上下文协议)服务器。我们正在实现一个使用生成式人工智能来操作 Google Docs 的界面。
功能
该 MCP 服务器提供以下功能:
阅读 Google Docs 文档
创建新的 Google Docs 文档
更新现有的 Google Docs 文档
搜索 Google Docs 文档
Related MCP server: MCP Google Suite
技术堆栈
Node.js (建议使用 v14 或更高版本)
@modelcontextprotocol/sdk - MCP SDK 的官方实现
Google APIs Node.js 客户端- 访问 Google API
先决条件
Node.js(建议使用 v14 或更高版本)
npm 或 yarn
Google Cloud Platform 项目和访问凭据
设置
1. 克隆或下载项目
git clone [リポジトリURL]
cd docs-mcp2.安装依赖项
npm install3. Google云平台设置
在Google Cloud Console中创建项目(或选择现有项目)
启用 Google Drive API 和 Google Docs API
创建 OAuth 2.0 客户端 ID 并下载凭据
将下载的凭证文件作为
credentials.json放在项目根目录中。
4. 偏好设置
在项目根目录中创建一个
.env文件并在那里设置环境变量:
# アプリケーション環境
NODE_ENV=development
# ログ設定
# ログレベル: ERROR, WARN, INFO, DEBUG, TRACE
LOG_LEVEL=INFO
# 標準エラー出力にログを出力するかどうか(MCPの仕様に準拠)
LOG_USE_STDERR=true
# サーバー設定
SERVER_NAME=google-docs-mcp-server
SERVER_VERSION=1.0.0
# Google API認証情報
# 認証情報ファイルのパス(デフォルトは./credentials.json)
CREDENTIALS_PATH=./credentials.json
# トークンファイルのパス(デフォルトは./token.json)
TOKEN_PATH=./token.json环境变量解释:
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:存储身份验证令牌的路径
启动开发服务器并获取令牌:
npm run dev执行后终端中会显示授权URL。在浏览器中访问该URL,使用您的Google帐户登录并执行授权。授权完成后,复制显示的授权码,粘贴到终端,然后按 Enter。这将生成一个
token.json文件,并从此自动对您进行身份验证。
构建并运行
建造
npm run build执行
作为常规服务器运行:
npm start以开发模式运行:
npm run dev用作 MCP 服务器
该项目是一个符合模型上下文协议(MCP)规范的服务器。您可以直接从 MCP 客户端(Cursor、Claude.ai 等)连接。
MCP 客户端上的设置
使用光标设置
要与 Cursor 一起使用它,请将以下设置添加到.cursor/mcp.json文件中:
{
"mcpServers": {
"google-docs": {
"command": "node",
"args": ["/{プロジェクトへの絶対パス}/docs-mcp/dist/index.js"]
}
}
}其他 MCP 客户端
其他 MCP 客户端使用标准输入/输出 (stdio) 进行通信。根据您的客户端设置指定适当的命令。
提供的 MCP 工具
read_google_document
阅读 Google Docs 文档的内容。
参数:
documentId(字符串):要读取的 Google Docs 文档的 ID。
使用示例:
// MCPクライアントでの使用例
const response = await client.callTool({
name: "read_google_document",
arguments: {
documentId: "your-document-id"
}
});创建Google文档
创建一个新的 Google Docs 文档。
参数:
title(字符串):新文档的标题。content(字符串,可选):文档的初始内容。
使用示例:
const response = await client.callTool({
name: "create_google_document",
arguments: {
title: "ドキュメントタイトル",
content: "初期コンテンツ"
}
});更新谷歌文档
更新现有的 Google Docs 文档。
参数:
documentId(字符串):要更新的 Google Docs 文档的 ID。content(字符串):要添加或更新的内容。startPosition(数字,可选):开始更新的位置。endPosition(数字,可选):结束更新的位置。
使用示例:
const response = await client.callTool({
name: "update_google_document",
arguments: {
documentId: "your-document-id",
content: "追加または更新するコンテンツ",
startPosition: 1,
endPosition: 10
}
});search_google_documents
搜索 Google Docs 文档。
参数:
query(字符串):搜索查询。maxResults(数字,可选):要检索的最大结果数(默认值:10)。
使用示例:
const response = await client.callTool({
name: "search_google_documents",
arguments: {
query: "検索キーワード",
maxResults: 5
}
});程序使用示例
从 TypeScript 或 JavaScript 程序使用 MCP 客户端的示例:
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
async function main() {
// MCPクライアントの作成
const client = new Client({
name: "google-docs-client",
version: "1.0.0"
});
// Google Docs MCPサーバーへの接続
const transport = new StdioClientTransport({
command: "npm",
args: ["run", "mcp"]
});
await client.connect(transport);
// サーバー情報の取得
const info = await client.getServerInfo();
console.log("利用可能なツール:", info.tools);
// ドキュメントの検索
const searchResult = await client.callTool({
name: "search_google_documents",
arguments: {
query: "会議資料",
maxResults: 5
}
});
console.log("検索結果:", searchResult);
// 接続を閉じる
await client.disconnect();
}
main().catch(console.error);故障排除
如果 Cursor 发生连接错误
完全重启 Cursor。
请确保
.cursor/mcp.json设置正确。手动启动 MCP 服务器并检查其是否正常工作:
npm run dev验证在运行此命令时是否看到消息“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 费用。