mdmukhtarmohsin/Discord-MCP-Server
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.
A comprehensive Model Context Protocol (MCP) server for Discord integration, providing secure access to Discord functionality through a standardized API interface.
send_message
Send a message to a Discord channel.
get_messages
Retrieve message history from a channel.
get_channel_info
Get information about a Discord channel.
search_messages
Search for messages with various filters.
moderate_content
Perform moderation actions.
Discord MCP Server
A comprehensive Model Context Protocol (MCP) server for Discord integration, providing secure access to Discord functionality through a standardized API interface.
๐ Features
- FastMCP Integration: Built on top of FastMCP for efficient MCP server implementation
- Discord Bot: Full-featured Discord bot with message handling and moderation capabilities
- Authentication & Authorization: JWT-based authentication with API key management
- Rate Limiting: Redis-based rate limiting with configurable limits
- Multi-tenancy: Support for multiple users with permission-based access control
- Audit Logging: Comprehensive logging of all actions and API calls
- Moderation Tools: Built-in content moderation and user management
- Secure: Hash-based API key storage and encrypted JWT tokens
๐ Requirements
- Python 3.9+
- Redis server
- Discord Bot Token (from Discord Developer Portal)
- Virtual environment (recommended)
๐ ๏ธ Installation
1. Clone and Setup Environment
git clone <repository-url>
cd Discord-MCP-Server
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
2. Install Dependencies
pip install -r requirements.txt
3. Configure Environment
Run the interactive setup script:
python3 scripts/setup_env.py
Or manually create a .env
file based on .env.example
:
cp .env.example .env
# Edit .env with your configuration
4. Start Redis Server
Make sure Redis is running on your system:
# Ubuntu/Debian
sudo systemctl start redis
# macOS with Homebrew
brew services start redis
# Or run directly
redis-server
5. Run the Server
python3 scripts/run_server.py
For development with debug mode:
python3 scripts/run_server.py --debug
โ๏ธ Configuration
Environment Variables
Variable | Description | Default |
---|---|---|
DISCORD_BOT_TOKEN | Discord bot token from Developer Portal | Required |
JWT_SECRET_KEY | Secret key for JWT token signing | Required |
REDIS_URL | Redis connection URL | redis://localhost:6379/0 |
MCP_SERVER_NAME | Name of the MCP server | discord-mcp-server |
MCP_SERVER_HOST | Server host address | 127.0.0.1 |
MCP_SERVER_PORT | Server port | 8000 |
LOG_LEVEL | Logging level | INFO |
LOG_FORMAT | Log format (json/console) | console |
CORS_ENABLED | Enable CORS | false |
ALLOWED_ORIGINS | Comma-separated allowed origins | `` |
RATE_LIMIT_REQUESTS | Requests per window | 100 |
RATE_LIMIT_WINDOW | Rate limit window in seconds | 60 |
Discord Bot Setup
- Go to the Discord Developer Portal
- Create a new application
- Go to the "Bot" section
- Create a bot and copy the token
- Enable necessary intents:
- Message Content Intent
- Server Members Intent
- Guilds Intent
- Invite the bot to your server with appropriate permissions
๐ง API Usage
MCP Tools
The server provides the following MCP tools:
send_message
Send a message to a Discord channel.
{
"tool": "send_message",
"arguments": {
"channel_id": "123456789",
"content": "Hello, world!",
"embed": {
"title": "Optional Embed",
"description": "Embed content"
}
}
}
get_messages
Retrieve message history from a channel.
{
"tool": "get_messages",
"arguments": {
"channel_id": "123456789",
"limit": 50,
"before": "2023-12-01T00:00:00Z",
"after": "2023-11-01T00:00:00Z"
}
}
get_channel_info
Get information about a Discord channel.
{
"tool": "get_channel_info",
"arguments": {
"channel_id": "123456789"
}
}
search_messages
Search for messages with various filters.
{
"tool": "search_messages",
"arguments": {
"query": "search term",
"channel_id": "123456789",
"author_id": "987654321",
"limit": 100
}
}
moderate_content
Perform moderation actions.
{
"tool": "moderate_content",
"arguments": {
"action": "delete_message",
"target_id": "message_id",
"guild_id": "guild_id",
"reason": "Spam content"
}
}
REST API Endpoints
GET /
- Server status and informationGET /health
- Health check endpointGET /mcp/tools
- List available MCP toolsGET /mcp/resources/discord://status
- Discord bot status
๐ Authentication
The server uses JWT-based authentication with API keys. Each API key has:
- User ID: Unique identifier for the user
- Permissions: Read-only, Read-write, or Moderation access
- Guild/Channel Restrictions: Optional access limitations
- Expiration Date: Automatic key expiration
- Rate Limiting: Per-key rate limits
Permission Levels
- READ_ONLY: Can retrieve messages and channel information
- READ_WRITE: Can send messages and retrieve data
- MODERATE: Can perform moderation actions (delete, ban, etc.)
๐งช Testing
Run the test suite:
# Install test dependencies
pip install pytest pytest-asyncio
# Run all tests
python3 -m pytest tests/
# Run specific test file
python3 -m pytest tests/test_auth.py -v
# Run with coverage
python3 -m pytest --cov=src tests/
๐ Monitoring and Logging
The server provides structured logging with configurable formats:
- Console Format: Human-readable colored output
- JSON Format: Machine-readable for log aggregation
Key log events include:
- Authentication attempts
- API calls and responses
- Discord actions (messages, moderation)
- Rate limiting events
- Errors and exceptions
๐จ Security Considerations
- API Keys: Stored as SHA-256 hashes in Redis
- JWT Tokens: Signed with strong secret keys
- Rate Limiting: Prevents abuse and DoS attacks
- Input Validation: All inputs are validated and sanitized
- Permission Checks: Fine-grained access control
- Audit Logging: Complete audit trail of all actions
๐ง Development
Project Structure
Discord-MCP-Server/
โโโ src/
โ โโโ auth/ # Authentication and authorization
โ โโโ bot/ # Discord bot implementation
โ โโโ mcp/ # MCP server and tools
โ โโโ models/ # Data models and schemas
โ โโโ utils/ # Utility functions
โ โโโ main.py # Application entry point
โโโ tests/ # Test suite
โโโ scripts/ # Utility scripts
โโโ requirements.txt # Python dependencies
โโโ .env.example # Environment configuration template
โโโ README.md # This file
Adding New MCP Tools
- Define the tool function in
src/mcp/server.py
- Use the
@mcp_server.tool()
decorator - Add appropriate error handling and logging
- Update the API documentation
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
๐ Support
For support and questions:
- Check the documentation and examples
- Review the test cases for usage patterns
- Open an issue on GitHub
- Join our Discord community
๐ Related Projects
- FastMCP - Fast MCP server implementation
- Discord.py - Discord API wrapper
- Model Context Protocol - MCP specification
Made with โค๏ธ for the Discord and MCP communities