Slack AI交互服务器

Slack MCP服务器是一个基于FastAPI和PostgreSQL构建的服务,使AI助手(如Claude Desktop、Cursor)能够与Slack进行交互,实现频道列表、消息发送和历史记录获取等功能。

  • by Sreenav14
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Sreenav14/Slack_mcp_server

使用说明

Slack MCP服务器

A. 模型上下文协议(MCP) 服务器,使AI助手(如Claude Desktop、Cursor)能够与Slack交互。使用FastAPI、MCP SDK和PostgreSQL构建。

🎯 这有什么作用

将您的Slack工作区变成AI可访问的工具:

  • 列出频道: list_channels(limit=20, include_private=false)
  • 发送消息: send_message(channel_id="C123", text="Hello!", thread_ts="...")
  • 获取历史记录: fetch_history(channel_id="C123", limit=10)

🏗️ 建筑

┌─────────────────────────────────────────────────────────────────────────┐
│                          COMPLETE MCP ECOSYSTEM                         │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  ┌─────────────────┐                    ┌─────────────────────────┐    │
│  │   MCP Client    │                    │    MCP Server            │    │
│  │  (Cursor/Claude)│                    │    (FastAPI + MCP SDK)   │    │
│  └─────────┬───────┘                    └─────────┬───────────────┘    │
│            │                                       │                    │
│            │ JSON-RPC over stdio                   │ HTTP/SSE           │
│            │ (via mcp_bridge.py)                   │                    │
│            ▼                                       ▼                    │
│  ┌─────────────────┐                    ┌─────────────────────────┐    │
│  │  MCP Bridge     │ ──────────────────►│   /mcp/sse              │    │
│  │  (Local)        │   HTTP POST         │   /mcp/messages         │    │
│  └─────────────────┘                    └─────────┬───────────────┘    │
│                                                  │                    │
│                                                  ▼                    │
│  ┌─────────────────┐                    ┌─────────────────────────┐    │
│  │  Tool Execution │ ──────────────────►│    Slack API             │    │
│  │  (server.py)    │   REST API          │                         │    │
│  └─────────────────┘                    └─────────────────────────┘    │
│                                                                          │
│  ┌─────────────────┐                    ┌─────────────────────────┐    │
│  │ Authentication  │                    │    Database              │    │
│  │ JWT + OAuth     │                    │    PostgreSQL            │    │
│  └─────────────────┘                    └─────────────────────────┘    │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘

📦 特性

  • ✅ 多用户:每个用户都连接自己的Slack工作区
  • ✅ 安全:用于Slack的JWT身份验证+OAuth 2.0
  • ✅ 现代MCP:使用官方MCP SDK和SSE传输
  • ✅ 生产就绪:FastAPI、PostgreSQL、正确的错误处理
  • ✅ 本机桥:将本地MCP客户端连接到远程服务器

🚀 快速开始

1.克隆和安装

git clone <your-repo>
cd slack-mcp
pip install -r requirements.txt

2.环境设置

创建 .env 文件:

# Database
DATABASE_URL=postgresql://user:password@localhost:5432/slack_mcp

# Slack OAuth (from https://api.slack.com/apps)
SLACK_CLIENT_ID=your_slack_client_id
SLACK_CLIENT_SECRET=your_slack_client_secret
SLACK_REDIRECT_URI=https://your-server.com/oauth/slack/callback

# App Security
APP_SECRET_KEY=your_secure_random_secret_key_here
JWT_EXP_MINUTES=43200

# Bridge Configuration (for local clients)
SLACK_MCP_TOKEN=your_session_token_here
SLACK_MCP_URL=https://your-server.com

3.数据库设置

# Create tables
alembic upgrade head

4.运行服务器

uvicorn app.main:app --reload

访问 http://localhost:8000/docs API文档。

🔐 用户流程

步骤1:创建帐户

# Signup
curl -X POST http://localhost:8000/auth/signup \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Response: {"user_id": 1, "session_token": "eyJ..."}

第二步:松弛连接

# Get OAuth URL
curl "http://localhost:8000/oauth/slack/start?session_token=your_token"

# Visit the URL in browser → Authorize app → Redirect back

步骤3:使用MCP工具

# Run bridge locally
python mcp_bridge.py

# Configure your MCP client (Cursor/Claude) to use:
# Command: python
# Arguments: ["path/to/mcp_bridge.py"]

🛠️ MCP工具

list_channels

列出连接的工作区中的Slack频道。

参数:

  • limit (数字,可选):要返回的最大通道数(默认值:20)
  • include_private (布尔值,可选):包括专用通道

例子:

{
  "name": "list_channels",
  "arguments": {"limit": 10, "include_private": true}
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "Found 3 channels:\n\n• #general (ID: C1234567890) - 25 members\n• #random (ID: C0987654321) - 15 members\n• #private (ID: C1111111111) - 5 members"
    }
  ]
}

send_message

向Slack频道发送消息。

参数:

  • channel_id (字符串,必填):松弛通道ID
  • text (字符串,必填):消息内容
  • thread_ts (字符串,可选):回复线程

例子:

{
  "name": "send_message",
  "arguments": {
    "channel_id": "C1234567890",
    "text": "Hello from AI!",
    "thread_ts": "1234567890.123456"
  }
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "✓ Message sent successfully to channel C1234567890"
    }
  ]
}

fetch_history

从频道获取消息历史记录。

参数:

  • channel_id (字符串,必填):松弛通道ID
  • limit (数字,可选):要获取的消息(默认值:10,最大值:100)

例子:

{
  "name": "fetch_history",
  "arguments": {
    "channel_id": "C1234567890",
    "limit": 5
  }
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "Last 5 messages from C1234567890:\n\n[U123]: Hello team!\n[U456]: How's the project?\n[U123]: Going well!\n[U789]: Great to hear\n[U456]: Awesome!"
    }
  ]
}

📁 项目结构

slack-mcp/
├── alembic/                    # Database migrations
├── app/
│   ├── auth/                   # Authentication
│   │   ├── jwt.py             # JWT token handling
│   │   ├── passwords.py       # Password hashing
│   │   └── routes.py          # /auth/signup, /auth/login
│   ├── mcp/                   # MCP server
│   │   ├── __init__.py
│   │   └── server.py          # MCP SDK implementation
│   ├── oauth/                 # Slack OAuth
│   │   └── slack.py           # OAuth flow
│   ├── slack/                 # Slack API client
│   │   └── client.py          # SlackClient wrapper
│   ├── config.py              # Environment config
│   ├── db.py                  # Database connection
│   ├── main.py                # FastAPI app + MCP endpoints
│   └── models.py              # SQLAlchemy models
├── mcp_bridge.py              # Local MCP bridge
├── requirements.txt           # Python dependencies
└── README.md                  # This file

🔧 API终点

认证

  • POST /auth/signup -创建帐户
  • POST /auth/login -登录
  • GET /auth/me -获取用户信息

Slack OAuth

  • GET /oauth/slack/start -启动OAuth流
  • GET /oauth/slack/callback -OAuth回调

MCP(苏格兰和南方能源公司运输)

  • GET /mcp/sse -SSE连接端点
  • POST /mcp/messages -发送MCP请求

效用

  • GET /health -健康检查
  • GET / -服务器信息
  • GET /docs -Swagger用户界面

🔒 安全

  • JWT身份验证:会话令牌将在30天后过期
  • OAuth 2.0:安全访问Slack工作区
  • CSRF保护:状态参数防止OAuth攻击
  • 密码散列:bcrypt用于安全密码存储
  • 用户隔离:每个用户只访问自己的Slack数据

🚀 部署

环境变量

# Production settings
DATABASE_URL=postgresql://prod:password@host:5432/db
SLACK_CLIENT_ID=your_prod_client_id
SLACK_CLIENT_SECRET=your_prod_client_secret
SLACK_REDIRECT_URI=https://yourdomain.com/oauth/slack/callback
APP_SECRET_KEY=your_prod_secret_key
JWT_EXP_MINUTES=43200

Docker(可选)

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
RUN alembic upgrade head

EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

🧪 测试

测试MCP服务器

# Start server
uvicorn app.main:app --reload

# Test health endpoint
curl http://localhost:8000/health

# Test MCP SSE connection (needs session_token)
curl "http://localhost:8000/mcp/sse?session_token=your_token"

测试桥梁

# Set environment variables
export SLACK_MCP_TOKEN=your_token
export SLACK_MCP_URL=http://localhost:8000

# Run bridge
python mcp_bridge.py

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 如果适用,添加测试
  5. 提交拉取请求

📄 许可证

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

🙏 致谢


🎯 这一切是如何协同工作的

  1. 用户注册 → Gets session_token
  2. 用户连接Slack → OAuth流程→ 商店 bot_access_token
  3. AI客户端连接 → 通过桥梁→ SSE运输→ MCP服务器
  4. AI调用工具 → MCP协议→ 您的服务器→ 松弛API→ 响应

结果:人工智能助手现在可以安全地读/写Slack消息! 🚀