内容
# discord-mcp
Discord 自我机器人 MCP 服务器 — 从 Claude Code 或 OpenClaw 读取和发送 Discord 消息。
> ⚠️ **自我机器人警告:** 这使用您的 Discord 用户令牌,这违反了 Discord 的服务条款。您的帐户可能会被封禁。请自担风险使用。
## 功能
通过 MCP(模型上下文协议)公开 6 种 Discord 工具:
| 工具 | 描述 |
|------|-------------|
| `discord_list_guilds` | 列出您加入的所有服务器 |
| `discord_list_channels` | 列出服务器中的文字频道 |
| `discord_get_messages` | 从频道中获取最近的消息 |
| `discord_get_dms` | 列出您的开放 DM 对话 |
| `discord_send_message` | 发送消息(频道或 DM) |
| `discord_get_unread` | 获取您尚未看到的消息 |
后台守护进程维护与 Discord 网关的持久 WebSocket 连接,将消息摄入本地 SQLite 数据库,以便 `discord_get_unread` 即使在 Claude 未运行时也能工作。
## 设置
### 1. 安装和配置
```bash
npx @tensakulabs/discord-mcp setup
```
这将:
1. 向您展示如何从桌面应用程序中提取 Discord 令牌
2. 安全地将令牌保存到您的操作系统密钥链(macOS:使用内置的 `security` CLI — 无需本机模块编译;Linux:keytar;备用:AES-256-CBC 加密文件)
3. 通过 launchd(macOS)或 systemd(Linux)启动后台守护进程
4. 在 Claude 的配置中自动注册 MCP 服务器
### 2. 令牌提取(设置期间显示的步骤)
打开 Discord 桌面应用程序 → 按 **Ctrl+Shift+I** (或 Mac 上的 **Cmd+Option+I** )→ **网络** 选项卡 → 在 Discord 中发送任意消息 → 按 `messages` 过滤请求 → 点击任意请求 → **头部** 选项卡 → 查找 `Authorization` 头部 → 复制其值。
### 3. 重启 Claude
重启 Claude Code。您将看到可用的 Discord 工具。
### 验证它是否正常工作
```bash
npx @tensakulabs/discord-mcp status
# ✅ Connected as: yourname#0
```
## 手动 MCP 配置(如果自动配置失败)
添加到 `~/.claude/settings.json`:
```json
{
"mcpServers": {
"discord": {
"command": "npx",
"args": ["-y", "@tensakulabs/discord-mcp"]
}
}
}
```
## 与 Claude 配合使用
注册后,Claude 可以直接使用 Discord 工具:
```
"#general 频道中我错过了什么?"
→ Claude 调用 discord_list_guilds、discord_list_channels、discord_get_unread
"回复 Alex 说我 5 点会到"
→ Claude 调用 discord_send_message with replyToMessageId
```
## 钩子
当 Discord 事件发生时,触发 shell 命令或 HTTP 网络钩子。在 `~/.config/discord-mcp/config.json` 中配置:
```json
{
"hooks": {
"on_mention": [
{
"type": "command",
"enabled": true,
"command": "osascript -e 'display notification \"{content}\" with title \"Mention from {author}\"'"
}
],
"on_everyone": [],
"on_here": [],
"on_message": []
}
}
```
### 钩子类型
| 钩子 | 触发条件 |
|------|-----------|
| `on_mention` | 有人直接 @username 提及您 |
| `on_everyone` | 有人在您加入的服务器中使用 @everyone |
| `on_here` | 有人在您加入的服务器中使用 @here |
| `on_message` | 任何非机器人消息(请谨慎使用 — 触发频率高) |
### 钩子配置字段
| 字段 | 值 | 描述 |
|-------|--------|-------------|
| `type` | `"command"` \| `"http"` | Shell 命令或 HTTP POST |
| `enabled` | `true` \| `false` | 切换而不删除 |
| `command` | 字符串 | Shell 命令(类型:命令) |
| `url` | 字符串 | 要 POST 到的端点(类型:http) |
### 模板变量
可在 `command` 字符串和 HTTP POST 正文中使用:
| 变量 | 值 |
|----------|-------|
| `{author}` | 消息发送者的用户名 |
| `{content}` | 消息文本 |
| `{channel}` | 频道 ID |
| `{guild}` | 服务器/公会 ID(或 `"dm"` 表示 DM) |
| `{is_dm}` | `true` 或 `false` |
### HTTP 钩子有效载荷
```json
{
"author": "username",
"content": "message text",
"channel": "channel-id",
"guild": "guild-id",
"is_dm": false
}
```
## OpenClaw 集成
添加到 OpenClaw 的 MCP 配置:
```json
{
"mcp": {
"servers": [{
"name": "discord",
"transport": "stdio",
"command": "npx",
"args": ["-y", "@tensakulabs/discord-mcp"]
}]
}
}
```
## 安全
- 令牌存储在操作系统密钥链中(macOS:`security` CLI — 无需本机模块编译;Linux:keytar;备用:AES-256-CBC 加密文件)
- 令牌从未写入配置文件或日志
- 本地文件位于 `~/.config/discord-mcp/`:`messages.db`、`config.json`、`token.enc`(仅备用)、`daemon.log`
## 架构
```
discord-mcp/
├── src/
│ ├── index.ts MCP 服务器入口 — 注册 6 种工具
│ ├── cli.ts 设置 + 状态命令
│ ├── daemon.ts Discord 网关 WebSocket → SQLite 摄入 + 钩子
│ ├── auth.ts 密钥链令牌存储(macOS 安全 CLI / keytar / 加密文件)
│ ├── hooks.ts 钩子运行器 — shell 命令和 HTTP 网络钩子
│ ├── config.ts 配置加载器(~/.config/discord-mcp/config.json)
│ ├── db.ts SQLite 模式 + 查询
│ ├── ratelimit.ts 429 回退 + Discord 头部
│ ├── state.ts 每个频道的最后 seen 状态
│ ├── purge.ts 调度消息保留清理
│ └── tools/
│ ├── list_guilds.ts
│ ├── list_channels.ts
│ ├── get_messages.ts
│ ├── get_dms.ts
│ ├── send_message.ts
│ └── get_unread.ts
```
所有 Discord API 调用都通过 `rateLimitedFetch` — 429 自动回退。
守护进程在 macOS 上作为 launchd 服务(`com.discord-mcp.daemon`)运行,连接到 `wss://gateway.discord.gg` 并存储所有消息以供快速未读查询。