Discord-Mcp-server

Tathagat017/Discord-Mcp-server

3.2

If you are the rightful owner of Discord-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 henry@mcphub.com.

FastMCP Discord Integration Server is a secure, scalable backend server built with FastAPI and FastMCP, enabling AI models to interact with Discord through a comprehensive set of tools.

Tools
  1. send_message

    Send messages to Discord channels

  2. get_messages

    Retrieve recent messages from channels

  3. get_channel_info

    Get detailed channel information

  4. search_messages

    Search messages by keyword

  5. delete_message

    Delete specific messages

  6. ban_user

    Ban users from guilds

  7. kick_user

    Kick users from guilds

  8. get_guild_info

    Get detailed guild information

FastMCP Discord Integration Server

A secure, scalable Model Context Protocol (MCP) backend server built with FastAPI and FastMCP, enabling AI models to interact with Discord through a comprehensive set of tools.

๐ŸŒŸ Features

  • Discord Bot Integration: Send messages, retrieve history, search content, and moderate channels
  • MCP Protocol Support: Full compatibility with MCP Inspector and MCP clients
  • Secure Authentication: API key-based authentication with role-based permissions
  • Multi-tenancy: Support for multiple Discord bots per client
  • Real-time Debugging: Integration with MCP Inspector for request monitoring
  • Comprehensive Logging: Structured logging with audit trail
  • Rate Limiting: Built-in protection against abuse
  • FastAPI Integration: RESTful API endpoints alongside MCP tools

๐Ÿ› ๏ธ Available MCP Tools

ToolDescriptionPermissions Required
send_messageSend messages to Discord channelsSend Messages
get_messagesRetrieve recent messages from channelsRead Message History
get_channel_infoGet detailed channel informationView Channels
search_messagesSearch messages by keywordRead Message History
delete_messageDelete specific messagesManage Messages
ban_userBan users from guildsBan Members
kick_userKick users from guildsKick Members
get_guild_infoGet detailed guild informationView Channels

๐Ÿš€ Quick Start

1. Prerequisites

  • Python 3.8+
  • Discord Bot Token
  • Virtual environment (recommended)

2. Installation

# Clone the repository
git clone <repository-url>
cd fastmcp-discord-server

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

3. Configuration

# Copy the sample environment file
cp env.sample .env

# Edit .env with your configuration
nano .env

Required Environment Variables:

  • DISCORD_BOT_TOKEN: Your Discord bot token
  • API_KEY_SECRET: Secret for API key generation
  • JWT_SECRET_KEY: Secret for JWT tokens

4. Discord Bot Setup

  1. Go to Discord Developer Portal
  2. Create a new application
  3. Go to "Bot" section and create a bot
  4. Copy the bot token to your .env file
  5. Enable the following bot permissions:
    • Send Messages
    • Read Message History
    • View Channels
    • Manage Messages (for moderation)
    • Ban Members (for moderation)
    • Kick Members (for moderation)

5. Running the Server

# Run FastAPI server (recommended for development)
python -m app.main

# Or run MCP server directly
python -m app.main mcp

# Or using uvicorn directly
uvicorn app.main:app --reload --host 127.0.0.1 --port 8000

๐Ÿ“ก API Usage

Authentication

All API calls require authentication via API key:

# Generate an API key
curl -X POST "http://localhost:8000/generate-api-key" \
  -H "Content-Type: application/json" \
  -d '{"user_id": "your_user_id"}'

Using MCP Tools

# List available tools
curl -X GET "http://localhost:8000/mcp/tools" \
  -H "X-API-Key: your_api_key"

# Call a tool
curl -X POST "http://localhost:8000/mcp/call-tool" \
  -H "X-API-Key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "send_message",
    "parameters": {
      "channel_id": 123456789,
      "content": "Hello from MCP!"
    }
  }'

Health Check

curl -X GET "http://localhost:8000/health"

๐Ÿ”ง MCP Client Integration

Using with MCP Inspector

  1. Install MCP Inspector
  2. Configure connection to http://localhost:8000
  3. Use API key for authentication
  4. Monitor real-time requests and responses

Using with AI Models

Configure your AI model client to use the MCP server:

{
  "server_url": "http://localhost:8000",
  "api_key": "your_api_key",
  "tools": ["send_message", "get_messages", "search_messages"]
}

๐Ÿ—๏ธ Project Structure

fastmcp_backend/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ main.py              # Main FastAPI + MCP server
โ”‚   โ”œโ”€โ”€ config.py            # Configuration management
โ”‚   โ”œโ”€โ”€ auth/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ middleware.py    # Authentication middleware
โ”‚   โ”œโ”€โ”€ discord/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ bot.py          # Discord bot integration
โ”‚   โ”œโ”€โ”€ mcp/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ tools.py        # MCP tools implementation
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ schemas/
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ db/
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ tests/
โ”‚       โ””โ”€โ”€ __init__.py
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ env.sample
โ””โ”€โ”€ README.md

๐Ÿ” Security Features

  • API Key Authentication: Secure HMAC-based API keys
  • Rate Limiting: Configurable request limits
  • CORS Protection: Configurable allowed origins
  • Input Validation: Pydantic schema validation
  • Audit Logging: Complete action logging
  • Permission Checks: Role-based access control

๐Ÿงช Testing

# Run tests
pytest app/tests/

# Run with coverage
pytest --cov=app app/tests/

# Run specific test
pytest app/tests/test_discord.py

๐Ÿ“Š Monitoring

Logs

Logs are structured and include:

  • Request/response details
  • User actions
  • Error tracking
  • Performance metrics

Health Monitoring

The /health endpoint provides:

  • Server status
  • Discord bot status
  • System metrics
  • Timestamp information

๐Ÿš€ Deployment

Development

python -m app.main

Production

# Using gunicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker

# Using Docker (create Dockerfile)
docker build -t fastmcp-discord .
docker run -p 8000:8000 fastmcp-discord

๐Ÿ“š Examples

Send a Message

import httpx

async def send_discord_message():
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "http://localhost:8000/mcp/call-tool",
            headers={"X-API-Key": "your_api_key"},
            json={
                "tool_name": "send_message",
                "parameters": {
                    "channel_id": 123456789,
                    "content": "Hello from FastMCP!"
                }
            }
        )
        return response.json()

Search Messages

async def search_messages():
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "http://localhost:8000/mcp/call-tool",
            headers={"X-API-Key": "your_api_key"},
            json={
                "tool_name": "search_messages",
                "parameters": {
                    "channel_id": 123456789,
                    "query": "important",
                    "limit": 10
                }
            }
        )
        return response.json()

๐Ÿค Contributing

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

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ†˜ Support

๐Ÿ”„ Changelog

v1.0.0

  • Initial release
  • Full MCP protocol support
  • Discord bot integration
  • Authentication system
  • Comprehensive logging
  • Rate limiting
  • Health monitoring