W4D3-Discord-MCP-Server-Bot

Misal-Ambasta/W4D3-Discord-MCP-Server-Bot

3.2

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

  1. Clone the repository:

    git clone <repository-url>
    cd W4D3-Discord-MCP-Server-Bot
    
  2. Install dependencies:

    npm install
    
  3. Set up environment variables: Create a .env file in the root directory:

    DISCORD_BOT_TOKEN=your_discord_bot_token_here
    
  4. Build the project:

    npm run build
    

šŸ¤– Discord Bot Setup

  1. 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
  2. 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
  3. 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 ID
  • content (string): Message content to send
  • apiKey (string): API key for authentication

2. get_messages

Retrieve message history from a Discord channel.

Parameters:

  • channelId (string): Discord channel ID
  • limit (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 ID
  • apiKey (string): API key for authentication

4. search_messages

Search for messages in a Discord channel.

Parameters:

  • channelId (string): Discord channel ID
  • query (string): Search query
  • limit (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 ID
  • messageId (string): Message ID to moderate
  • action (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 JavaScript
  • npm run watch - Watch for changes and recompile
  • npm start - Start the compiled server
  • npm 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

  1. Bot not responding: Check if the bot token is correct and the bot is online
  2. Permission errors: Ensure the bot has the required permissions in Discord
  3. Authentication failures: Verify API keys are correctly configured
  4. 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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

šŸ“š Resources