Slack_mcp_server

Sreenav14/Slack_mcp_server

3.2

If you are the rightful owner of Slack_mcp_server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

The Slack MCP Server is a specialized server designed to facilitate communication and integration between Slack and various applications using the Model Context Protocol (MCP).

Slack MCP Server

A Model Context Protocol (MCP) server that enables AI assistants (like Claude Desktop, Cursor) to interact with Slack. Built with FastAPI, MCP SDK, and PostgreSQL.

🎯 What This Does

Turns your Slack workspace into AI-accessible tools:

  • List channels: list_channels(limit=20, include_private=false)
  • Send messages: send_message(channel_id="C123", text="Hello!", thread_ts="...")
  • Fetch history: fetch_history(channel_id="C123", limit=10)

🏗️ Architecture

┌─────────────────────────────────────────────────────────────────────────┐
│                          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            │    │
│  └─────────────────┘                    └─────────────────────────┘    │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘

📦 Features

  • Multi-tenant: Each user connects their own Slack workspace
  • Secure: JWT authentication + OAuth 2.0 for Slack
  • Modern MCP: Uses official MCP SDK with SSE transport
  • Production ready: FastAPI, PostgreSQL, proper error handling
  • Local bridge: Connects local MCP clients to remote server

🚀 Quick Start

1. Clone & Install

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

2. Environment Setup

Create a .env file:

# 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. Database Setup

# Create tables
alembic upgrade head

4. Run Server

uvicorn app.main:app --reload

Visit http://localhost:8000/docs for API documentation.

🔐 User Flow

Step 1: Account Creation

# 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..."}

Step 2: Slack Connection

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

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

Step 3: Use MCP Tools

# Run bridge locally
python mcp_bridge.py

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

🛠️ MCP Tools

list_channels

Lists Slack channels in the connected workspace.

Parameters:

  • limit (number, optional): Max channels to return (default: 20)
  • include_private (boolean, optional): Include private channels

Example:

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

Response:

{
  "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

Sends a message to a Slack channel.

Parameters:

  • channel_id (string, required): Slack channel ID
  • text (string, required): Message content
  • thread_ts (string, optional): Reply to thread

Example:

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

Response:

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

fetch_history

Fetches message history from a channel.

Parameters:

  • channel_id (string, required): Slack channel ID
  • limit (number, optional): Messages to fetch (default: 10, max: 100)

Example:

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

Response:

{
  "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!"
    }
  ]
}

📁 Project Structure

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 Endpoints

Authentication

  • POST /auth/signup - Create account
  • POST /auth/login - Login
  • GET /auth/me - Get user info

Slack OAuth

  • GET /oauth/slack/start - Start OAuth flow
  • GET /oauth/slack/callback - OAuth callback

MCP (SSE Transport)

  • GET /mcp/sse - SSE connection endpoint
  • POST /mcp/messages - Send MCP requests

Utility

  • GET /health - Health check
  • GET / - Server info
  • GET /docs - Swagger UI

🔒 Security

  • JWT Authentication: Session tokens expire in 30 days
  • OAuth 2.0: Secure Slack workspace access
  • CSRF Protection: State parameters prevent OAuth attacks
  • Password Hashing: bcrypt for secure password storage
  • User Isolation: Each user only accesses their own Slack data

🚀 Deployment

Environment Variables

# 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 (Optional)

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"]

🧪 Testing

Test the MCP Server

# 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"

Test the Bridge

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

# Run bridge
python mcp_bridge.py

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

📄 License

MIT License - see LICENSE file for details.

🙏 Acknowledgments


🎯 How It All Works Together

  1. User signs up → Gets session_token
  2. User connects Slack → OAuth flow → Stores bot_access_token
  3. AI client connects → Via bridge → SSE transport → MCP server
  4. AI calls tools → MCP protocol → Your server → Slack API → Response

Result: AI assistants can now read/write Slack messages securely! 🚀