Google日历MCP服务器

一个用于Google日历集成并支持OAuth 2.0认证的模型上下文协议(MCP)服务器。

  • by EmilyThaHuman
  • 本地部署
  • 更新于
在 GitHub 查看 EmilyThaHuman/google-calendar-mcp-server

使用说明

谷歌日历MCP服务器

用于Google日历与OAuth 2.0身份验证集成的模型上下文协议(MCP)服务器。

特性

  • OAuth 2.0身份验证:通过自动令牌刷新保护Google OAuth流
  • MCP协议支持:全面实施模型上下文协议
  • 谷歌日历操作:
    • 列出和搜索事件
    • 创建、更新和删除事件
    • 列出日历
    • 获取忙/闲信息
  • 会话管理:具有自动令牌刷新功能的持久会话
  • TypeScript:完全使用TypeScript类型,以获得更好的开发体验

先决条件

  • Node.js 18+
  • 启用Google日历API的Google云项目
  • OAuth 2.0凭据(客户端ID和客户端密钥)

设置

1.谷歌云配置

  1. 首选 谷歌云控制台
  2. 创建新项目或选择现有项目
  3. 启用Google日历API
  4. 创建OAuth 2.0凭据:
    • 应用程序类型:Web应用程序
    • 授权重定向URI: http://localhost:3002/oauth/callback
  5. 复制您的客户端ID和客户端密码

2.安装

# Clone or navigate to the project directory
cd google-calendar-mcp-server

# Install dependencies
npm install

3.配置

创建一个 .env 根目录中的文件:

cp env.example .env

编辑 .env 根据您的配置:

# Server Configuration
PORT=3002
NODE_ENV=development

# Google OAuth Configuration
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:3002/oauth/callback

# Frontend URL (where to redirect after OAuth)
FRONTEND_URL=http://localhost:5173

# MCP Server Configuration
MCP_SERVER_NAME=google-calendar-mcp-server
MCP_SERVER_VERSION=1.0.0

用法

发展模式

npm run dev

生产模式

# Build
npm run build

# Start
npm start

API终点

健康检查

GET /health

OAuth流

1.启动授权

GET /oauth/authorize?userId={userId}&state={state}

答复:

{
  "authorizationUrl": "https://accounts.google.com/o/oauth2/v2/auth?...",
  "state": "your-state-value"
}

2.OAuth回调(自动处理)

GET /oauth/callback?code={code}&state={state}

3.刷新令牌

POST /oauth/refresh
Content-Type: application/json

{
  "userId": "user123",
  "refreshToken": "refresh_token_here"
}

4.断开连接

POST /oauth/disconnect
Content-Type: application/json

{
  "userId": "user123"
}

MCP端点

POST /mcp
GET /mcp
DELETE /mcp

MCP端点遵循工具执行的模型上下文协议规范。

MCP工具

1.日历列表_事件

列出并搜索谷歌日历事件。

输入:

{
  "userId": "user123",
  "calendarId": "primary",
  "timeMin": "2024-01-01T00:00:00Z",
  "timeMax": "2024-12-31T23:59:59Z",
  "maxResults": 50,
  "singleEvents": true,
  "orderBy": "startTime",
  "showDeleted": false,
  "q": "meeting"
}

输出:

{
  "events": [
    {
      "id": "event123",
      "summary": "Team Meeting",
      "description": "Weekly team sync",
      "location": "Conference Room A",
      "start": {
        "dateTime": "2024-01-15T10:00:00-08:00",
        "timeZone": "America/Los_Angeles"
      },
      "end": {
        "dateTime": "2024-01-15T11:00:00-08:00",
        "timeZone": "America/Los_Angeles"
      },
      "attendees": [
        {
          "email": "user@example.com",
          "displayName": "User Name",
          "responseStatus": "accepted"
        }
      ],
      "status": "confirmed",
      "htmlLink": "https://calendar.google.com/..."
    }
  ],
  "count": 1
}

2.日历_创建_事件

在Google日历中创建新事件。

输入:

{
  "userId": "user123",
  "calendarId": "primary",
  "summary": "Project Review",
  "description": "Q4 project review meeting",
  "location": "Building 1, Room 301",
  "start": {
    "dateTime": "2024-01-20T14:00:00-08:00",
    "timeZone": "America/Los_Angeles"
  },
  "end": {
    "dateTime": "2024-01-20T15:00:00-08:00",
    "timeZone": "America/Los_Angeles"
  },
  "attendees": [
    { "email": "colleague@example.com" }
  ],
  "reminders": {
    "useDefault": false,
    "overrides": [
      { "method": "email", "minutes": 1440 },
      { "method": "popup", "minutes": 10 }
    ]
  }
}

输出:

{
  "success": true,
  "event": {
    "id": "event456",
    "summary": "Project Review",
    "htmlLink": "https://calendar.google.com/...",
    "start": {
      "dateTime": "2024-01-20T14:00:00-08:00"
    },
    "end": {
      "dateTime": "2024-01-20T15:00:00-08:00"
    }
  }
}

3.日历更新事件

更新Google日历中的现有事件。

输入:

{
  "userId": "user123",
  "calendarId": "primary",
  "eventId": "event456",
  "summary": "Updated: Project Review",
  "location": "Building 2, Room 405"
}

输出:

{
  "success": true,
  "event": {
    "id": "event456",
    "summary": "Updated: Project Review",
    "htmlLink": "https://calendar.google.com/..."
  }
}

4.日历_事件

从Google日历中删除事件。

输入:

{
  "userId": "user123",
  "calendarId": "primary",
  "eventId": "event456"
}

输出:

{
  "success": true,
  "message": "Event event456 deleted successfully"
}

5.日历列表

列出已验证用户的所有日历。

输入:

{
  "userId": "user123"
}

输出:

{
  "calendars": [
    {
      "id": "primary",
      "summary": "My Calendar",
      "description": "Personal calendar",
      "timeZone": "America/Los_Angeles",
      "primary": true
    },
    {
      "id": "work@example.com",
      "summary": "Work Calendar",
      "timeZone": "America/Los_Angeles",
      "primary": false
    }
  ],
  "count": 2
}

6.日历_get_fibebusy

获取日历的忙/闲信息。

输入:

{
  "userId": "user123",
  "timeMin": "2024-01-15T00:00:00Z",
  "timeMax": "2024-01-15T23:59:59Z",
  "calendarIds": ["primary", "work@example.com"]
}

输出:

{
  "calendars": {
    "primary": {
      "busy": [
        {
          "start": "2024-01-15T10:00:00-08:00",
          "end": "2024-01-15T11:00:00-08:00"
        },
        {
          "start": "2024-01-15T14:00:00-08:00",
          "end": "2024-01-15T15:30:00-08:00"
        }
      ]
    }
  }
}

与ZeroTwoApi集成

要将此谷歌日历MCP服务器与您的ZeroTwoApi后端集成:

1.更新前端OAuth配置

在 ZeroTwo/src/services/oauthProviders.js,更新Google日历配置:

google_calendar: {
  provider: "google_calendar",
  clientId: import.meta.env.VITE_GOOGLE_CLIENT_ID,
  flowType: OAuthFlowType.GOOGLE_GIS,
  scopes: [
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.googleapis.com/auth/calendar.events",
  ],
  backendExchangeEndpoint: "/api/auth/google-calendar/callback",
  backendRefreshEndpoint: "/api/auth/google-calendar/refresh",
  mcpEnabled: true,
  mcpEndpoint: "http://localhost:3002/mcp",
  mcpSseEndpoint: "http://localhost:3002/sse",
},

2.更新后端OAuth路由

后端OAuth路由 ZeroTwoApi/routes/auth/google-calendar.js 应该重定向到此MCP服务器,而不是直接处理令牌。

3.删除旧刀具路线

从以下位置删除旧刀具路线 ZeroTwoApi/routes/ai/tools/google-calendar.js 因为所有日历操作现在都由此MCP服务器处理。

4.创建MCP集成

OAuth身份验证后,在数据库中创建MCP集成:

INSERT INTO mcp_integrations (
  user_id,
  name,
  provider,
  endpoint,
  mcp_type,
  connection_status,
  auth_type,
  description
) VALUES (
  'user_id_here',
  'Google Calendar',
  'google_calendar',
  'http://localhost:3002/mcp',
  'remote_mcp',
  'connected',
  'oauth',
  'Google Calendar integration via MCP'
);

建筑

┌─────────────────┐
│   Frontend      │
│   (ZeroTwo)     │
└────────┬────────┘
         │
         │ OAuth Flow
         ▼
┌─────────────────┐      ┌──────────────────┐
│  Calendar MCP   │◄────►│  Google OAuth    │
│  Server         │      │  & Calendar API  │
└────────┬────────┘      └──────────────────┘
         │
         │ MCP Protocol
         ▼
┌─────────────────┐
│   Backend       │
│   (ZeroTwoApi)  │
└─────────────────┘

发展

项目结构

google-calendar-mcp-server/
├── src/
│   ├── auth/
│   │   └── oauth-manager.ts    # OAuth token management
│   ├── config/
│   │   └── index.ts            # Configuration management
│   ├── calendar/
│   │   └── client.ts           # Calendar API wrapper
│   ├── mcp/
│   │   ├── server.ts           # MCP server implementation
│   │   └── tools.ts            # MCP tool definitions
│   ├── types/
│   │   └── index.ts            # TypeScript type definitions
│   ├── utils/
│   │   └── logger.ts           # Logging utility
│   └── index.ts                # Entry point
├── package.json
├── tsconfig.json
├── env.example
└── README.md

脚本

  • npm run dev -使用热重新加载启动开发服务器
  • npm run build -将TypeScript构建为JavaScript
  • npm start -启动生产服务器
  • npm run lint -运行ESLint
  • npm run format -使用Prettier格式化代码

安全考虑

  1. OAuth令牌:令牌存储在内存中。对于生产,请使用安全的数据库或加密存储。
  2. 跨域资源共享:为生产适当配置CORS源。
  3. 超文本传输安全协议:在生产环境中使用HTTPS进行安全通信。
  4. 环境变量:从不承诺 .env 文件夹。在生产中使用安全的秘密管理。
  5. 速率限制:对生产部署实施速率限制。

故障排除

OAuth错误

  • “缺少必需的环境变量”:检查您的 .env 文件包含所有必需的变量
  • “交换授权码失败”:在Google Cloud控制台中验证您的重定向URI是否完全匹配
  • “令牌已过期”:如果刷新令牌可用,服务器会自动刷新令牌

MCP连接问题

  • “没有有效的会话ID”:确保MCP客户端发送正确的会话标头
  • “需要身份验证”:用户需要先完成OAuth流程

许可证

麻省理工学院

贡献

欢迎投稿!请随时提交拉取请求。


由ZeroTwo提供技术支持

此Google日历MCP连接器是 ZeroTwo人工智能平台 --一体化的人工智能工作区,让您通过GPT-5、Claude和Gemini安排会议、管理事件和自动化日历。

🌐 ZeroTwo——一个应用程序中的所有AI模型 使用人工智能安排、重新安排和管理谷歌日历事件——所有这些都在一个应用程序中。
✨ 零两个功能 人工智能日程安排、文档分析、网络搜索和MCP驱动的日历自动化。
🤖 人工智能模型——GPT-5、克劳德和双子座 使用世界上最好的人工智能来计划你的一天,查找可用性,并设置提醒。
🔌 ZeroTwo连接器和集成 将Google日历、Gmail、Google联系人、Outlook等连接到您的AI工作流程。
💰 零二定价 一个替代ChatGPT Plus、Claude Pro和Gemini Advanced的订阅。
📝 ZeroTwo博客 人工智能生产力提示、日程安排指南和ZeroTwo产品更新。
🚀 免费试用ZeroTwo 让AI管理你的日程安排——今天就免费开始吧。

专为ZeroTwo打造 --使用此Google日历MCP服务器 ZeroTwo的AI连接器系统 通过人工智能助手中的自然语言创建事件、检查可用性和管理日历。