Google文档管理服务器

一个强大的Google Docs API集成服务器,用于创建、读取、更新和管理Google文档,支持AI助手操作。

  • by lkm1developer
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 lkm1developer/google-docs-mcp-server

使用说明

谷歌文档MCP服务器

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

特性

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

先决条件

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

安装

  1. 克隆此存储库:

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

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

    # Required
    GOOGLE_CLOUD_PROJECT_ID=your-project-id
    
    # Choose one authentication method:
    
    # Option 1A: Service Account with file path (recommended for production)
    GOOGLE_APPLICATION_CREDENTIALS=path/to/your-service-account-key.json
    
    # Option 1B: Service Account with JSON content directly
    # Useful for environments where you can't create files
    GOOGLE_APPLICATION_CREDENTIALS_JSON={"type":"service_account","project_id":"...","private_key":"...","client_email":"..."}
    
    # Option 2: API Key (simpler for development)
    GOOGLE_API_KEY=your-api-key
    
    # Option 3: OAuth2 (required for user-specific operations)
    client_id=your-oauth-client-id
    client_secret=your-oauth-client-secret
    refresh_token=your-oauth-refresh-token
    

身份验证设置

服务帐户身份验证

  1. 转到 谷歌云控制台
  2. 创建新项目或选择现有项目
  3. 启用Google Docs API和Google Drive API
  4. 转到“IAM和管理”>“服务帐户”
  5. 创建新的服务帐户
  6. 授予其必要的角色(例如“Docs API用户”、“Drive API用户”)
  7. 为服务帐户创建并下载JSON密钥
  8. 在您的 .env 文件

OAuth2身份验证

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

  1. 转到 谷歌云控制台
  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
    
    # Or with JSON content directly:
    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代理身份验证 用于身份验证的包。要实现身份验证,请执行以下操作:

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

许可证

麻省理工学院