日历管理服务

该服务允许用户通过Claude与Google Calendar交互,实现事件列表查看、会议创建、空闲时间查找等功能。

  • by v-3
  • TypeScript
  • 本地部署
  • 72 星标
  • 更新于
在 GitHub 查看 v-3/google-calendar

使用说明

谷歌日历MCP服务器

此MCP服务器允许Claude与您的Google日历交互,启用列出事件、创建会议和查找空闲时间等功能。

先决条件

  • Node.js(v16或更高版本)
  • 克劳德桌面应用程序
  • 谷歌云项目
  • 已启用Google日历API
  • OAuth 2.0凭据

安装说明

1.创建谷歌云项目

  1. 转到 谷歌云控制台
  2. 创建新项目或选择现有项目
  3. 启用Google日历API:
    • 转到“API和服务”>“库”
    • 搜索“Google日历API”
    • 点击“启用”

2.配置OAuth同意屏幕

  1. 转到“API和服务”>“OAuth同意屏幕”
  2. 选择“外部”用户类型(除非您有Google Workspace组织)
  3. 填写所需信息:
    • 应用程序名称
    • 用户支持电子邮件
    • 开发人员联系信息
  4. 添加以下范围:
    • https://www.googleapis.com/auth/calendar
    • https://www.googleapis.com/auth/calendar.events
  5. 将您的电子邮件地址添加为测试用户

3.创建OAuth 2.0凭据

  1. 转到“API和服务”>“凭据”
  2. 点击“创建凭据”>“OAuth客户端ID”
  3. 选择“桌面应用程序”作为应用程序类型
  4. 命名您的客户(例如,“MCP日历客户端”)
  5. 点击“创建”
  6. 下载客户端配置文件(您需要客户端ID和客户端密码)

4.获取刷新令牌

  1. 创建一个名为的新文件 getToken.js:
const { google } = require('googleapis');
const http = require('http');
const url = require('url');

// Replace these with your OAuth 2.0 credentials
const CLIENT_ID = 'your-client-id';
const CLIENT_SECRET = 'your-client-secret';
const REDIRECT_URI = 'http://localhost:3000/oauth2callback';

// Configure OAuth2 client
const oauth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLIENT_SECRET,
  REDIRECT_URI
);

// Define scopes
const scopes = [
  'https://www.googleapis.com/auth/calendar',
  'https://www.googleapis.com/auth/calendar.events'
];

async function getRefreshToken() {
  return new Promise((resolve, reject) => {
    try {
      // Create server to handle OAuth callback
      const server = http.createServer(async (req, res) => {
        try {
          const queryParams = url.parse(req.url, true).query;
          
          if (queryParams.code) {
            // Get tokens from code
            const { tokens } = await oauth2Client.getToken(queryParams.code);
            console.log('\n=================');
            console.log('Refresh Token:', tokens.refresh_token);
            console.log('=================\n');
            console.log('Save this refresh token in your configuration!');
            
            // Send success response
            res.end('Authentication successful! You can close this window.');
            
            // Close server
            server.close();
            resolve(tokens);
          }
        } catch (error) {
          console.error('Error getting tokens:', error);
          res.end('Authentication failed! Please check console for errors.');
          reject(error);
        }
      }).listen(3000, () => {
        // Generate auth url
        const authUrl = oauth2Client.generateAuthUrl({
          access_type: 'offline',
          scope: scopes,
          prompt: 'consent'  // Force consent screen to ensure refresh token
        });

        console.log('1. Copy this URL and paste it in your browser:');
        console.log('\n', authUrl, '\n');
        console.log('2. Follow the Google authentication process');
        console.log('3. Wait for the refresh token to appear here');
      });

    } catch (error) {
      console.error('Server creation error:', error);
      reject(error);
    }
  });
}

// Run the token retrieval
getRefreshToken().catch(console.error);
  1. 安装所需的依赖关系:
npm install googleapis
  1. 使用您的OAuth凭据更新脚本:

    • 替换 your-client-id 使用您的实际客户ID
    • 替换 your-client-secret 使用您的实际客户机密
  2. 运行脚本:

node getToken.js
  1. 按照控制台中的说明进行操作:
    • 复制提供的URL
    • 将其粘贴到浏览器中
    • 完成Google身份验证过程
    • 复制控制台中显示的刷新令牌

5.配置克劳德桌面

  1. 打开您的Claude Desktop配置文件:

对于MacOS:

code ~/Library/Application\ Support/Claude/claude_desktop_config.json

对于Windows:

code %AppData%\Claude\claude_desktop_config.json
  1. 添加或更新配置:
{
    "mcpServers": {
        "google-calendar": {
            "command": "node",
            "args": [
                "/ABSOLUTE/PATH/TO/YOUR/build/index.js"
            ],
            "env": {
                "GOOGLE_CLIENT_ID": "your_client_id_here",
                "GOOGLE_CLIENT_SECRET": "your_client_secret_here",
                "GOOGLE_REDIRECT_URI": "http://localhost",
                "GOOGLE_REFRESH_TOKEN": "your_refresh_token_here"
            }
        }
    }
}
  1. 保存文件并重新启动Claude Desktop

初始项目设置

  1. 为您的项目创建一个新目录:
mkdir google-calendar-mcp
cd google-calendar-mcp
  1. 初始化一个新的npm项目:
npm init -y
  1. 安装依赖项:
npm install @modelcontextprotocol/sdk googleapis google-auth-library zod
npm install -D @types/node typescript
  1. 创建一个tsconfig.json文件:
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}
  1. 更新package.json:
{
  "type": "module",
  "scripts": {
    "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\""
  }
}
  1. 创建源目录:
mkdir src
  1. 为本地开发创建.env文件(不要提交此文件):
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost
GOOGLE_REFRESH_TOKEN=your_refresh_token_here

建立和运行

  1. 构建服务器:
npm run build
  1. 当您打开Claude Desktop时,服务器将自动启动

可用工具

服务器提供以下工具:

  1. list_events:列出指定时间范围内的日历事件
  2. create_event:创建新的日历事件
  3. update_event:更新现有日历事件
  4. delete_event:删除日历事件
  5. find_free_time:在日历中查找可用的时间段

Claude中的示例用法

设置后,您可以使用以下命令:

  • “显示我下周的日历事件”
  • “安排明天下午2点与[email_id]会面1小时”
  • “今天下午找一个30分钟的免费时段”
  • “将我下午3点的会议更新为下午4点”
  • “取消ID为[event_ID]的会议”

故障排除

常见问题

  1. Claude中未出现的工具:

    • 检查克劳德桌面日志: tail -f ~/Library/Logs/Claude/mcp*.log
    • 验证所有环境变量是否设置正确
    • 确保index.js的路径是绝对正确的
  2. 身份验证错误:

    • 验证您的OAuth凭据是否正确
    • 检查刷新令牌是否有效
    • 确保启用了所需的作用域
  3. 服务器连接问题:

    • 检查服务器是否已成功构建
    • 验证build/index.js上的文件权限(应该是755)
    • 尝试直接运行服务器: node /path/to/build/index.js

查看日志

要查看服务器日志,请执行以下操作:

# For MacOS/Linux:
tail -n 20 -f ~/Library/Logs/Claude/mcp*.log

# For Windows:
Get-Content -Path "$env:AppData\Claude\Logs\mcp*.log" -Wait -Tail 20

环境变量

如果您遇到环境变量错误,请验证每个错误:

  1. GOOGLE_CLIENT_ID:应该以“123456789-…”开头
  2. GOOGLE_REDIRECT_URI:应该是“http://localhost"
  3. GOOGLE_REFRESH_TOKEN:一个不会过期的长字符串

安全考虑

  • 确保您的OAuth凭据安全
  • 不将凭据提交到版本控制
  • 对敏感数据使用环境变量
  • 定期轮换刷新令牌
  • 监控Google云控制台中API的使用情况

许可证

MIT许可证-有关详细信息,请参阅许可证文件。

支持

如果您遇到任何问题:

  1. 检查上面的故障排除部分
  2. 查看克劳德桌面日志
  3. 在GitHub上打开一个问题
  4. 联系维护人员