Misal-Ambasta/W4D3-Discord-MCP-Server-Bot
If you are the rightful owner of W4D3-Discord-MCP-Server-Bot 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.
The Model Context Protocol (MCP) server is designed to facilitate communication and data exchange between various applications and machine learning models, ensuring seamless integration and efficient processing.
Discord MCP Server Bot
A Model Context Protocol (MCP) server that enables AI models to interact with Discord through secure, authenticated tools. This project provides a complete implementation of Discord bot functionality accessible via the MCP protocol.
š Features
- Complete MCP Server Implementation using the official TypeScript SDK
- Discord Integration with full bot functionality
- Secure Authentication with API key-based access control
- Permission System with granular access control
- Multi-tenancy Support for managing multiple Discord bots
- Audit Logging for all operations
- MCP Inspector Integration for debugging and monitoring
š Prerequisites
- Node.js 18+ and npm
- Discord Bot Token (see setup instructions below)
- TypeScript knowledge (optional, for development)
š ļø Installation
-
Clone the repository:
git clone <repository-url> cd W4D3-Discord-MCP-Server-Bot
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the root directory:DISCORD_BOT_TOKEN=your_discord_bot_token_here
-
Build the project:
npm run build
š¤ Discord Bot Setup
-
Create a Discord Application:
- Go to Discord Developer Portal
- Click "New Application" and give it a name
- Go to the "Bot" section and click "Add Bot"
- Copy the bot token and add it to your
.env
file
-
Configure Bot Permissions:
- In the Discord Developer Portal, go to "OAuth2" > "URL Generator"
- Select "bot" scope
- Select these permissions:
- Send Messages
- Read Message History
- Manage Messages (for moderation)
- View Channels
-
Invite Bot to Server:
- Use the generated URL to invite the bot to your Discord server
- Make sure the bot has the necessary permissions in the channels you want to use
š§ Usage
Starting the MCP Server
npm start
The server will start and listen for MCP requests via stdio transport.
Testing with MCP Inspector
npm run inspector
This will launch the MCP Inspector for debugging and testing your server.
š”ļø Authentication
The server uses API key authentication. Configure your API keys in src/mcp_auth_wrapper.ts
:
export const API_KEYS: Record<string, { permissions: string[], tenant: string }> = {
'your-api-key': {
permissions: ['send_message', 'get_messages', 'get_channel_info', 'search_messages', 'moderate_content'],
tenant: 'your-tenant-id'
},
};
šØ Available Tools
1. send_message
Send a message to a Discord channel.
Parameters:
channelId
(string): Discord channel IDcontent
(string): Message content to sendapiKey
(string): API key for authentication
2. get_messages
Retrieve message history from a Discord channel.
Parameters:
channelId
(string): Discord channel IDlimit
(number, optional): Number of messages to retrieve (default: 10)apiKey
(string): API key for authentication
3. get_channel_info
Get information about a Discord channel.
Parameters:
channelId
(string): Discord channel IDapiKey
(string): API key for authentication
4. search_messages
Search for messages in a Discord channel.
Parameters:
channelId
(string): Discord channel IDquery
(string): Search querylimit
(number, optional): Maximum number of results (default: 10)apiKey
(string): API key for authentication
5. moderate_content
Moderate content by deleting messages.
Parameters:
channelId
(string): Discord channel IDmessageId
(string): Message ID to moderateaction
(string): Moderation action ('delete')apiKey
(string): API key for authentication
šļø Development
Project Structure
src/
āāā index.ts # Main MCP server implementation
āāā state.ts # Shared Discord client state
āāā mcp_auth_wrapper.ts # Authentication and permissions
āāā tools/ # Individual tool implementations (legacy)
āāā send_message.ts
āāā get_messages.ts
āāā get_channel_info.ts
āāā search_messages.ts
āāā moderate_content.ts
Build Commands
npm run build
- Compile TypeScript to JavaScriptnpm run watch
- Watch for changes and recompilenpm start
- Start the compiled servernpm run inspector
- Launch MCP Inspector
Adding New Tools
To add a new tool, register it in src/index.ts
using the mcpServer.tool()
method:
mcpServer.tool(
'your_tool_name',
{
param1: z.string().describe('Parameter description'),
apiKey: z.string().describe('API key for authentication')
},
async ({ param1, apiKey }) => {
// Authentication check
if (!apiKey || !(apiKey in API_KEYS)) {
auditLog('AUTH_FAIL', { reason: 'Invalid or missing API key', tool: 'your_tool_name' });
throw new Error('Unauthorized: Invalid or missing API key');
}
// Your tool logic here
return {
content: [{
type: "text",
text: "Your response here"
}]
};
}
);
š Security Features
- API Key Authentication: All tools require valid API keys
- Permission-based Access Control: Each API key has specific permissions
- Audit Logging: All operations are logged with timestamps
- Input Validation: All parameters are validated using Zod schemas
- Error Handling: Comprehensive error handling with meaningful messages
š Monitoring
The server includes built-in monitoring features:
- Request/Response Logging: All MCP requests and responses are logged
- Connection Tracking: Active connections are monitored
- Audit Trail: All operations are logged for security auditing
š Troubleshooting
Common Issues
- Bot not responding: Check if the bot token is correct and the bot is online
- Permission errors: Ensure the bot has the required permissions in Discord
- Authentication failures: Verify API keys are correctly configured
- Channel not found: Make sure the bot has access to the specified channel
Debugging
Use the MCP Inspector to debug your server:
npm run inspector
Check the console logs for detailed error messages and audit trails.
š License
This project is licensed under the ISC License.
š¤ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request