Teams MCP 服务器

一个用于Microsoft Teams集成的模型上下文协议(MCP)服务器,支持OAuth 2.0认证。

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

使用说明

Microsoft Teams MCP服务器

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

特性

  • OAuth 2.0身份验证:通过自动令牌刷新保护Microsoft OAuth流
  • MCP协议支持:全面实施模型上下文协议
  • 团队运营:
    • 搜索聊天和频道消息
    • 按路径获取特定消息
    • 获取聊天成员
    • 获取用户资料
  • 会话管理:具有自动令牌刷新功能的持久会话
  • TypeScript:完全使用TypeScript类型,以获得更好的开发体验
  • Cloudflare员工:可部署到Cloudflare Workers以获得全球边缘性能

先决条件

  • Node.js 18+
  • Microsoft Azure应用程序注册
  • OAuth 2.0凭据(客户端ID和客户端密钥)

设置

1.Microsoft Azure配置

  1. 首选 Azure门户
  2. 导航至 Azure Active Directory > 应用程序注册
  3. 点击 新注册
  4. 配置您的应用程序:
    • 姓名: Teams MCP Server
    • 支持的帐户类型:根据您的需求进行选择
    • 重定向URI: Web - http://localhost:3003/oauth/callback
  5. 创建后,请注意 应用程序(客户端)ID 和 目录(租户)ID
  6. 首选 证书和秘密 > 新客户机密
  7. 复制客户端机密值
  8. 首选 API权限 > 添加权限 > Microsoft Graph
  9. 添加以下内容 委托权限:
    • Chat.Read
    • ChannelMessage.Read.All
    • User.Read
  10. 点击 授予管理员同意 (如果您有管理员权限)

2.安装

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

# Install dependencies
npm install

3.配置

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

cp env.example .env

编辑 .env 根据您的配置:

# Server Configuration
PORT=3003
NODE_ENV=development

# Microsoft OAuth Configuration
MICROSOFT_CLIENT_ID=your_microsoft_client_id_here
MICROSOFT_CLIENT_SECRET=your_microsoft_client_secret_here
MICROSOFT_REDIRECT_URI=http://localhost:3003/oauth/callback
MICROSOFT_TENANT_ID=common

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

# MCP Server Configuration
MCP_SERVER_NAME=teams-mcp-server
MCP_SERVER_VERSION=1.0.0

租户ID选项:

  • common -多租户(个人和工作/学校账户)
  • organizations -仅限工作/学校账户
  • consumers -仅限个人Microsoft帐户
  • {tenant-id} -特定租户/组织

用法

发展模式

npm run dev

生产模式

# Build
npm run build

# Start
npm start

Cloudflare员工部署

# Create KV namespace for sessions
npm run kv:create

# Update wrangler.toml with your KV namespace ID

# Set secrets
wrangler secret put MICROSOFT_CLIENT_ID
wrangler secret put MICROSOFT_CLIENT_SECRET
wrangler secret put MICROSOFT_REDIRECT_URI
wrangler secret put MICROSOFT_TENANT_ID
wrangler secret put FRONTEND_URL

# Deploy
npm run deploy

API终点

健康检查

GET /health

OAuth流

1.启动授权

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

答复:

{
  "authorizationUrl": "https://login.microsoftonline.com/...",
  "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.团队搜索

搜索Microsoft Teams聊天记录和频道消息。

所需范围: Chat.Read, ChannelMessage.Read.All

输入:

{
  "userId": "user123",
  "query": "meeting notes",
  "chatId": "optional_chat_id",
  "channelId": "optional_channel_id",
  "teamId": "optional_team_id",
  "maxResults": 20,
  "fromDate": "2024-01-01T00:00:00Z",
  "toDate": "2024-12-31T23:59:59Z"
}

输出:

{
  "messages": [
    {
      "id": "msg123",
      "chatId": "chat123",
      "from": {
        "displayName": "John Doe",
        "id": "user456"
      },
      "body": {
        "content": "Here are the meeting notes...",
        "contentType": "text"
      },
      "createdDateTime": "2024-01-15T10:30:00Z",
      "subject": "Meeting Notes",
      "webUrl": "https://teams.microsoft.com/..."
    }
  ],
  "count": 1
}

2.teams_fetch

按路径获取特定的Teams消息。

所需范围: Chat.Read, ChannelMessage.Read.All

输入:

{
  "userId": "user123",
  "path": "chats/{chatId}/messages/{messageId}"
}

或者对于频道消息:

{
  "userId": "user123",
  "path": "teams/{teamId}/channels/{channelId}/messages/{messageId}"
}

输出:

{
  "id": "msg123",
  "chatId": "chat123",
  "from": {
    "displayName": "John Doe",
    "id": "user456"
  },
  "body": {
    "content": "Message content here",
    "contentType": "text"
  },
  "createdDateTime": "2024-01-15T10:30:00Z",
  "webUrl": "https://teams.microsoft.com/..."
}

3.团队成员

列出Microsoft Teams聊天的成员。

所需范围: Chat.Read

输入:

{
  "userId": "user123",
  "chatId": "chat123"
}

输出:

{
  "members": [
    {
      "id": "member123",
      "displayName": "John Doe",
      "userId": "user456",
      "email": "john.doe@company.com",
      "roles": ["owner"]
    }
  ],
  "count": 1
}

4.teams_get_file

返回经过身份验证的Teams用户的个人资料。

所需范围: User.Read

输入:

{
  "userId": "user123"
}

输出:

{
  "id": "user123",
  "displayName": "John Doe",
  "givenName": "John",
  "surname": "Doe",
  "userPrincipalName": "john.doe@company.com",
  "mail": "john.doe@company.com",
  "jobTitle": "Software Engineer",
  "officeLocation": "Building 1",
  "mobilePhone": "+1234567890",
  "businessPhones": ["+1234567890"]
}

建筑

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

发展

项目结构

teams-mcp-server/
├── src/
│   ├── auth/
│   │   └── oauth-manager.ts    # OAuth token management
│   ├── config/
│   │   └── index.ts            # Configuration management
│   ├── teams/
│   │   └── client.ts           # Teams/Graph 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
│   └── worker.ts               # Cloudflare Worker entry
├── package.json
├── tsconfig.json
├── wrangler.toml
├── env.example
└── README.md

脚本

  • npm run dev -使用热重新加载启动开发服务器
  • npm run dev:worker -启动Cloudflare Workers开发服务器
  • npm run build -将TypeScript构建为JavaScript
  • npm run build:worker -为Cloudflare Workers构建(模拟运行)
  • npm start -启动生产服务器
  • npm run deploy -部署到Cloudflare Workers
  • npm run lint -运行ESLint
  • npm run format -使用Prettier格式化代码

与ZeroTwoApi集成

要将此Teams MCP服务器与ZeroTwoApi后端集成:

1.更新前端OAuth配置

在前端OAuth提供者配置中,添加Teams:

teams: {
  provider: "teams",
  clientId: import.meta.env.VITE_MICROSOFT_CLIENT_ID,
  flowType: OAuthFlowType.REDIRECT,
  scopes: [
    "Chat.Read",
    "ChannelMessage.Read.All",
    "User.Read",
  ],
  authEndpoint: "http://localhost:3003/oauth/authorize",
  backendExchangeEndpoint: "/api/auth/teams/callback",
  backendRefreshEndpoint: "/api/auth/teams/refresh",
  mcpEnabled: true,
  mcpEndpoint: "http://localhost:3003/mcp",
},

2.更新MCP客户端配置

在您的MCP客户端中,添加Teams MCP支持:

// For Teams MCP
if (integration.provider === "teams") {
  endpointUrl = "http://localhost:3003/mcp";
  
  // Use OAuth token from profile
  if (integration.oauth_token) {
    transportOptions.headers = {
      Authorization: `Bearer ${integration.oauth_token}`,
      ...integration.headers,
    };
  }
}

3.创建MCP集成

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

INSERT INTO mcp_integrations (
  user_id,
  name,
  provider,
  endpoint,
  mcp_type,
  connection_status,
  auth_type,
  description
) VALUES (
  'user_id_here',
  'Microsoft Teams',
  'teams',
  'http://localhost:3003/mcp',
  'remote_mcp',
  'connected',
  'oauth',
  'Microsoft Teams integration via MCP'
);

Microsoft Graph API

此服务器使用Microsoft Graph API与团队交互:

使用的公共图API端点

  • GET /me -获取用户资料
  • GET /me/chats -列出用户的聊天记录
  • GET /chats/{chat-id}/messages -获取聊天信息
  • GET /chats/{chat-id}/members -获取聊天成员
  • GET /teams/{team-id}/channels/{channel-id}/messages -获取频道消息

安全考虑

  1. OAuth令牌:令牌存储在内存(Node.js)或KV(Cloudflare)中。对于生产,确保适当的加密。
  2. 跨域资源共享:为生产适当配置CORS源。
  3. 超文本传输安全协议:在生产环境中使用HTTPS进行安全通信。
  4. 环境变量:从不承诺 .env 文件夹。在生产中使用安全的秘密管理。
  5. 速率限制:对生产部署实施速率限制。
  6. 范围最小化:只请求用例所需的最小范围。

故障排除

OAuth错误

  • “缺少必需的环境变量”:检查您的 .env 文件包含所有必需的变量
  • “交换授权码失败”:在Azure门户中验证您的重定向URI是否完全匹配
  • “令牌已过期”:如果刷新令牌可用,服务器会自动刷新令牌
  • “权限不足”:确保您已授予所需API权限的管理员同意

MCP连接问题

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

图形API错误

  • “拒绝访问”:检查是否已授予并同意所需的权限
  • “找不到资源”:验证聊天/频道/团队ID是否正确
  • “油门”:Microsoft Graph有速率限制。执行回退和重试逻辑。

速率限制

Microsoft Graph API有速率限制:

  • 每个应用程序的限制:因端点而异
  • 按用户限制:因端点而异

看 Microsoft Graph限制指南 了解详情。

许可证

麻省理工学院

贡献

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


由ZeroTwo提供技术支持

此Microsoft Teams MCP连接器是 ZeroTwo人工智能平台 --通过GPT-5、Claude和Gemini,您可以发送消息、管理渠道并自动化团队协作。

🌐 ZeroTwo——一个应用程序中的所有AI模型 在Microsoft Teams中与GPT-5、Claude和Gemini协作——所有这些都在一个工作区中。
✨ 零两个功能 人工智能消息传递、文档分析、网络搜索和MCP支持的团队协作工具。
🤖 人工智能模型——GPT-5、克劳德和双子座 使用世界上最好的人工智能来起草消息、总结聊天记录和管理团队渠道。
🔌 ZeroTwo连接器和集成 将团队、Outlook、SharePoint、Gmail等连接到您的AI工作流程。
💰 零二定价 一个替代ChatGPT Plus、Claude Pro和Gemini Advanced的订阅。
📝 ZeroTwo博客 AI团队协作提示、Microsoft 365指南和ZeroTwo产品更新。
🚀 免费试用ZeroTwo 让人工智能为您的团队工作流程增压——立即免费开始。

专为ZeroTwo打造 --使用此Microsoft Teams MCP服务器 ZeroTwo的AI连接器系统 通过自然语言发送消息、管理渠道和自动化团队沟通。