Discord消息管理工具

一个通过MCP协议提供Discord消息读取和发送功能的工具,支持服务器、频道和私信管理。

  • by tensakulabs
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 tensakulabs/discord-mcp

使用说明

不和谐mcp

Discord selfbot MCP服务器——从Claude Code或OpenClaw读取和发送Discord消息。

⚠️ 自拍警告: 这将使用您的Discord用户令牌,这违反了Discord的ToS。您的帐户可能会被禁止。使用风险自负。

它做什么

通过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 即使克劳德不跑步,它也能正常工作。

设置

1.安装和配置

npx @tensakulabs/discord-mcp setup

这将:

  1. 演示如何从桌面应用程序中提取Discord令牌
  2. 将其安全地保存到您的操作系统密钥链(macOS:使用内置 security CLI——不需要本机模块)
  3. 通过launchd(macOS)或systemd(Linux)启动后台守护进程
  4. 在Claude的配置中自动注册MCP服务器

2.令牌提取(设置过程中显示的步骤)

打开Discord桌面应用程序→ Press Ctrl+Shift+I (或 Cmd+选项+I 在Mac上)→ 网络 tab → 在Discord中发送任何消息→ 按以下条件筛选请求 messages → 单击任何请求→ 标头 tab → 找到 Authorization 头球→ 复制其值。

3.重启克劳德

重新启动克劳德代码。您将看到Discord工具可用。

验证它是否正常工作

npx @tensakulabs/discord-mcp status
# ✅ Connected as: yourname#0

手动MCP配置(如果自动配置失败)

增添 ~/.claude/settings.json:

{
  "mcpServers": {
    "discord": {
      "command": "npx",
      "args": ["-y", "@tensakulabs/discord-mcp"]
    }
  }
}

与Claude一起使用

注册后,Claude可以直接使用Discord工具:

"What did I miss in the #general channel of the Tensaku server?"
→ Claude calls discord_list_guilds, discord_list_channels, discord_get_unread

"Reply to Alex saying I'll be there at 5pm"
→ Claude calls discord_send_message with replyToMessageId

钩子

当Discord事件发生时,触发shell命令或HTTP webhooks。在中配置 ~/.config/discord-mcp/config.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 有人在您所在的服务器中使用@
on_message 任何非机器人消息(谨慎使用——频繁触发)

钩子配置字段

字段 值 描述
type "command" "http"
enabled true false
command string Shell命令(类型:command)
url string POST到的端点(类型:http)

模板变量

可选 command 字符串和HTTP POST正文:

变量 值
{author} 消息发送者的用户名
{content} 消息文本
{channel} 频道ID
{guild} 公会/服务器ID(或 "dm" 对于DM)
{is_dm} true 或 false

HTTP挂钩负载

{
  "author": "username",
  "content": "message text",
  "channel": "channel-id",
  "guild": "guild-id",
  "is_dm": false
}

OpenClaw集成

添加到OpenClaw的MCP配置中:

{
  "mcp": {
    "servers": [{
      "name": "discord",
      "transport": "stdio",
      "command": "npx",
      "args": ["-y", "@tensakulabs/discord-mcp"]
    }]
  }
}

安全

  • 存储在OS钥匙链中的令牌(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 server entry — registers 6 tools
│   ├── cli.ts            setup + status commands
│   ├── daemon.ts         Discord Gateway WebSocket → SQLite ingestion + hooks
│   ├── auth.ts           keychain token storage (macOS security CLI / keytar / encrypted file)
│   ├── hooks.ts          hook runner — shell commands and HTTP webhooks
│   ├── config.ts         config loader (~/.config/discord-mcp/config.json)
│   ├── db.ts             SQLite schema + queries
│   ├── ratelimit.ts      429 backoff + Discord headers
│   ├── state.ts          per-channel last-seen state
│   ├── purge.ts          scheduled message retention cleanup
│   └── tools/
│       ├── list_guilds.ts
│       ├── list_channels.ts
│       ├── get_messages.ts
│       ├── get_dms.ts
│       ├── send_message.ts
│       └── get_unread.ts

所有Discord API调用都通过 rateLimitedFetch --429自动后退。

守护进程作为launchd服务运行(com.discord-mcp.daemon)在macOS上,连接到 wss://gateway.discord.gg 并将所有消息存储在本地,以便快速进行未读查询。