Skip to main content
Glama
Discord D&D MCP

Discord D&D MCP

by Closeplanet2
README.md
# Discord D&D MCP

A local stdio MCP server that lets Codex read configured Discord servers and manage messages only in the D&D server. Codex starts `server.py` when it connects. A separate `changelog_bot.py` process logs D&D server message activity to its private `#changelog` channel.

## Setup

1. Create a Discord bot, invite it to each server, and enable **Message Content Intent** in the Discord Developer Portal. In the portal's **OAuth2 → URL Generator**, select the `bot` scope. Give it message write permissions in the D&D server and only **View Channel** and **Read Message History** in other servers.
2. Put `DISCORD_BOT_TOKEN`, comma-separated `DISCORD_GUILD_IDS`, `DISCORD_WRITE_GUILD_ID` (the D&D server), and `DISCORD_CHANGELOG_CHANNEL_ID` (its private changelog) in `.env`. Never commit `.env`.
3. Install Python 3.11+ and run `python -m venv .venv`, then `.\.venv\Scripts\python.exe -m pip install -r requirements.txt`.
4. Register the stdio server with Codex, using `dotenv.exe -f <absolute .env path> run -- <absolute python.exe path> <absolute server.py path>` as the command and arguments. Restart Codex or start a new chat after changing the tool catalog.

The current local Codex configuration already has a `discord_dnd` entry pointing at this project. The MCP Inspector is optional for testing and is not part of the connection.

### Adding another server

Invite the same bot to the new Discord server with read permissions only. Check its effective permissions in each channel: inherited `@everyone` or channel overrides can still grant posting even when the invite requested only read access. Remove **Send Messages**, **Send Messages in Threads**, **Add Reactions**, **Manage Messages**, **Manage Channels**, and **Administrator** outside the D&D server. Add the new server ID to `.env`, while keeping the D&D server as the sole write target and sole changelog destination:

```dotenv
DISCORD_GUILD_IDS=DND_GUILD_ID,OTHER_GUILD_ID
DISCORD_WRITE_GUILD_ID=DND_GUILD_ID
DISCORD_CHANGELOG_CHANNEL_ID=DND_CHANGELOG_CHANNEL_ID
```

Keep the existing `DISCORD_BOT_TOKEN` value. `DISCORD_GUILD_IDS` controls which servers can be read. `DISCORD_WRITE_GUILD_ID` names the only server where MCP message changes are allowed. Restart the listener and Codex's MCP connection after changing `.env`. For more read-only servers, append their IDs to `DISCORD_GUILD_IDS`. No changelog channel is needed in those servers.

## Project skills

Codex can discover the repository skills in `.agents/skills/`: `discord-server-research` for read-only lore and format research, and `discord-dnd-edit` for D&D message changes. Start a new Codex session to load newly added skills. You can also invoke them explicitly as `$discord-server-research` and `$discord-dnd-edit`.

## Changelog listener

The D&D server's private `#changelog` channel should be readable by its administrators and the bot. The listener logs new messages, text edits, attachment changes, pins, deletes, bulk deletes, and reaction changes from D&D channels the bot can access. It ignores activity in all other servers. Entries link to the source message and include up to 500 characters of text. Changelog entries themselves are excluded to prevent a loop. Mentions and link previews are disabled in log posts.

Run the listener separately from the MCP server, and keep it running:

```powershell
cd G:\Closeplanet2\Discord-MCP
.\.venv\Scripts\python.exe changelog_bot.py
```

The listener receives events only while connected. Discord does not supply the old content of an uncached edit or deleted message, so those entries may say the previous text is unavailable. It does not backfill events missed while offline. Give the bot **View Channel**, **Read Message History**, and **Send Messages** in channels you want covered, and enable **Message Content Intent** in the Developer Portal. Run it via Task Scheduler or a service if it must survive a reboot.

## Tools

Read: `discord_status`, `list_guilds`, `message_permissions`, `list_channels`, `list_forum_threads`, `search_messages`, `channel_history`, `get_message`, `list_reactions`, `list_pins`.

Write: `send_message` (including replies and embeds), `edit_message`, `delete_message`, `bulk_delete_messages`, `crosspost_message`, `add_reaction`, `remove_reaction`, `clear_reactions`, `pin_message`, `unpin_message`.

Use `list_guilds` to see configured servers. Pass `guild_id` to `list_channels` and guild-wide `search_messages` when multiple servers are configured; searches limited to a `channel_id` infer its server. Other tools infer the server from the channel ID. Read tools accept channels in any configured server. All write tools reject channels outside `DISCORD_WRITE_GUILD_ID`. Discord also enforces the bot's permissions, so remove write permissions from its role in other servers. Message content can only be edited by its author; deleting other users' messages and clearing their reactions require **Manage Messages**. Posting in threads requires **Send Messages in Threads**. Pins require **Pin Messages**. `message_permissions(channel_id)` reports the bot's effective permissions in a channel.

Mentions and reply pings are disabled by default when sending or editing. Set `allow_mentions=true` only when notifications are intended. Bulk deletion is restricted by Discord to 2–100 messages no older than 14 days. Forum parents hold posts as threads: call `list_forum_threads`, then use a returned thread ID with message tools.

`search_messages` searches accessible messages locally because Discord's guild search endpoint returned 403 for this bot. It is bounded and reports `incomplete=true` when it stops before covering everything; narrow by `channel_id` or increase its scan limits for deeper searches.

## Test

Run `.\.venv\Scripts\python.exe -m unittest discover -s tests -v`. The tests mock all mutating Discord requests and do not post to the server.