sanuatmasai/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 Model Context Protocol (MCP) server that provides Discord bot functionality through tool-based interactions.
Discord MCP Bot š¤
A Model Context Protocol (MCP) server that provides Discord bot functionality through tool-based interactions. Enables programmatic Discord channel management, message operations, and content moderation via MCP tools.
Features
- Send Messages: Post content to Discord channels programmatically
- Message Retrieval: Fetch recent message history from channels
- Channel Information: Get channel details and metadata
- Message Search: Find messages containing specific keywords
- Content Moderation: Delete recent messages for cleanup
- Async Architecture: Non-blocking operations with queue-based message handling
Project Structure
āāā main.py # MCP server with tool definitions
āāā discord_bot.py # Discord bot backend and core functions
āāā .env # Environment variables (create this file)
āāā README.md # This file
Requirements
pip install discord.py python-dotenv fastmcp asyncio
Setup
1. Discord Bot Token
Create a .env
file in the project root:
DISCORD_BOT_TOKEN=your_discord_bot_token_here
2. Discord Bot Permissions
Your Discord bot needs these permissions:
- Read Messages
- Send Messages
- Read Message History
- Manage Messages (for deletion)
- View Channels
3. Get Channel IDs
Enable Developer Mode in Discord and right-click channels to copy their IDs.
Usage
Start the Server
python main.py
MCP Tools Available
send_message(channel_id, content)
Send a message to a Discord channel
await send_message("123456789", "Hello Discord!")
get_messages(channel_id, limit=10)
Retrieve recent messages from a channel
messages = await get_messages("123456789", 5)
get_channel_info_tool(channel_id)
Get channel name and topic information
info = await get_channel_info_tool("123456789")
search_messages_tool(channel_id, keyword, limit=50)
Search for messages containing specific keywords
results = await search_messages_tool("123456789", "python", 20)
moderate_content(channel_id, limit=5)
Delete recent messages for content moderation
result = await moderate_content("123456789", 3)
Architecture
Discord Bot Backend (discord_bot.py
)
- Handles Discord API connections and authentication
- Implements async message queue for reliable delivery
- Provides core Discord operations as reusable functions
- Manages bot lifecycle and connection state
MCP Server (main.py
)
- Exposes Discord functionality as MCP tools
- Handles tool registration and server lifecycle
- Bridges between MCP protocol and Discord operations
- Runs both bot and MCP server concurrently
Configuration
Bot Settings
- Intents: Message content, guilds, members
- Encoding: UTF-8 for Windows console compatibility
- Queue: Async message queue prevents rate limiting
Error Handling
- Graceful fallbacks for missing channels
- Comprehensive exception handling
- User-friendly error messages with emojis
Security Considerations
- Token Security: Store Discord token in
.env
file (never commit to git) - Permissions: Grant minimal required Discord permissions
- Channel Access: Verify bot has access to target channels
- Rate Limiting: Built-in queue system prevents API abuse
Troubleshooting
Common Issues
Bot Not Connecting:
- Verify Discord token in
.env
file - Check bot permissions in Discord server
- Ensure bot is invited to the server
Channel Not Found:
- Verify channel ID is correct
- Check bot has access to the channel
- Ensure channel exists and bot can view it
Permission Errors:
- Grant necessary permissions in Discord server settings
- Check role hierarchy and permissions inheritance
Debug Tips
- Check console output for connection status
- Use
get_channel_info_tool
to test channel access - Start with simple
send_message
to verify basic functionality
Development
Adding New Tools
Add new MCP tools in main.py
:
@mcp.tool
async def your_new_tool(parameter: str) -> str:
return await your_discord_function(parameter)
Extending Bot Functions
Add new Discord operations in discord_bot.py
:
async def your_new_function(params):
await bot_ready.wait()
# Your Discord API calls here
return result
License
This project is for educational and development purposes. Ensure compliance with Discord's Terms of Service when using.