Google Docs MCP Server

Google Docs MCP Server

lkm1developer

开发者工具
访问服务器

README

Google Docs MCP 服务器

一个强大的模型上下文协议 (MCP) 服务器实现,用于无缝的 Google Docs API 集成,使 AI 助手能够创建、读取、更新和管理 Google 文档。

功能

  • 创建具有自定义标题和内容的新 Google 文档
  • 检索文档内容和元数据
  • 使用新内容更新现有文档
  • 列出所有可访问的文档
  • 删除文档
  • 将文档导出为不同的格式(PDF、纯文本等)
  • 与特定用户共享文档
  • 按标题或内容搜索文档
  • 验证连接和凭据

前提条件

  • Node.js 18 或更高版本
  • 启用了 Google Docs API 的 Google Cloud 项目
  • 身份验证凭据(API 密钥、服务帐户或 OAuth2)

安装

  1. 克隆此存储库:

    git clone https://github.com/lkm1developer/google-docs-mcp-server.git
    cd google-docs-mcp-server
    
  2. 安装依赖项:

    npm install
    
  3. 创建一个包含您的 Google Cloud 凭据的 .env 文件:

    # 必需
    GOOGLE_CLOUD_PROJECT_ID=your-project-id
    
    # 选择一种身份验证方法:
    
    # 选项 1A:使用文件路径的服务帐户(推荐用于生产环境)
    GOOGLE_APPLICATION_CREDENTIALS=path/to/your-service-account-key.json
    
    # 选项 1B:直接使用 JSON 内容的服务帐户
    # 适用于无法创建文件的环境
    GOOGLE_APPLICATION_CREDENTIALS_JSON={"type":"service_account","project_id":"...","private_key":"...","client_email":"..."}
    
    # 选项 2:API 密钥(开发环境更简单)
    GOOGLE_API_KEY=your-api-key
    
    # 选项 3:OAuth2(用户特定操作需要)
    client_id=your-oauth-client-id
    client_secret=your-oauth-client-secret
    refresh_token=your-oauth-refresh-token
    

身份验证设置

服务帐户身份验证

  1. 转到 Google Cloud Console
  2. 创建一个新项目或选择一个现有项目
  3. 启用 Google Docs API 和 Google Drive API
  4. 转到“IAM & 管理”>“服务帐户”
  5. 创建一个新的服务帐户
  6. 授予它必要的角色(例如,“Docs API User”、“Drive API User”)
  7. 为服务帐户创建并下载 JSON 密钥
  8. 在您的 .env 文件中设置此 JSON 文件的路径

OAuth2 身份验证

对于需要用户同意的操作(例如创建/编辑文档):

  1. 转到 Google Cloud Console
  2. 创建一个新项目或选择一个现有项目
  3. 启用 Google Docs API 和 Google Drive API
  4. 转到“API 和服务”>“凭据”
  5. 创建 OAuth 客户端 ID 凭据(Web 应用程序类型)
  6. 添加授权的重定向 URI(例如,http://localhost:3000/oauth2callback)
  7. 记下您的客户端 ID 和客户端密钥
  8. 使用以下脚本获取刷新令牌:
// 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

构建和运行

  1. 构建项目:

    npm run build
    
  2. 测试您的连接:

    npx tsx src/test-connection.ts
    
  3. 运行服务器:

    npm start
    

    或者使用特定的凭据:

    npm start -- --GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json --GOOGLE_CLOUD_PROJECT_ID=your-project-id
    
    # 或者直接使用 JSON 内容:
    npm start -- --GOOGLE_APPLICATION_CREDENTIALS_JSON='{"type":"service_account","project_id":"..."}' --GOOGLE_CLOUD_PROJECT_ID=your-project-id
    
  4. 运行带有身份验证的 SSE 服务器:

    npx mcp-proxy-auth node dist/index.js
    

在 SSE 服务器中实现身份验证

SSE 服务器使用 mcp-proxy-auth 包进行身份验证。 要实现身份验证:

  1. 安装软件包:

    npm install mcp-proxy-auth
    
  2. AUTH_SERVER_URL 环境变量设置为指向您的 API 密钥验证端点:

    export AUTH_SERVER_URL=https://your-auth-server.com/verify
    
  3. 运行带有身份验证的 SSE 服务器:

    npx mcp-proxy-auth node dist/index.js
    
  4. SSE URL 将在以下位置可用:

    localhost:8080/sse?apiKey=apikey
    

    apikey 替换为您的实际 API 密钥以进行身份验证。

mcp-proxy-auth 包充当代理,它:

  • 拦截对您的 SSE 服务器的请求
  • 根据您的身份验证服务器验证 API 密钥
  • 仅允许经过身份验证的请求到达您的 SSE 端点

Docker 支持

您还可以使用 Docker 运行服务器:

  1. 构建 Docker 镜像:

    docker build -t google-docs-mcp-server .
    
  2. 运行容器:

    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 文档 titlecontent(可选)
google_docs_get 按 ID 获取 Google 文档 documentId
google_docs_update 使用新内容更新 Google 文档 documentIdcontentreplaceAll(可选)
google_docs_list 列出经过身份验证的用户可以访问的 Google 文档 pageSize(可选),pageToken(可选)
google_docs_delete 删除 Google 文档 documentId
google_docs_export 将 Google 文档导出为不同的格式 documentIdmimeType(可选)
google_docs_share 与特定用户共享 Google 文档 documentIdemailAddressrole(可选)
google_docs_search 按标题或内容搜索 Google 文档 querypageSize(可选),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

推荐服务器

Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
MCP Package Docs Server

MCP Package Docs Server

促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。

精选
本地
TypeScript
Claude Code MCP

Claude Code MCP

一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。

精选
本地
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。

精选
本地
JavaScript
mermaid-mcp-server

mermaid-mcp-server

一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。

精选
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

精选
TypeScript
Linear MCP Server

Linear MCP Server

一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

精选
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Curri MCP Server

Curri MCP Server

通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。

官方
本地
JavaScript