Discord通知MCP服务器

一个基于Cloudflare Workers的MCP服务器,用于在Claude Code完成任务时向Discord发送通知。

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

使用说明

MCP服务器不一致通知

当Claude Code完成任务时,向Discord发送通知的MCP(模型上下文协议)服务器。使用Cloudflare代理SDK使用TypeScript构建,并可部署在Cloudflare Workers上。

特性

  • 🤖 MCP工具:提供 send_discord_message 用于发送通知的工具
  • 🚀 Cloudflare员工:使用全局分发运行无服务器
  • 🔐 安全:使用Cloudflare机密作为凭据
  • 🌐 双重运输:支持SSE和Streamable HTTP,以实现最大兼容性
  • 💾 耐用物品:McpAgent要求的状态管理
  • 💬 丰富的格式:支持嵌入、TTS消息和提及控件

建筑

此服务器使用Cloudflare的代理SDK实现MCP规范:

  • GET/sse:MCP通信的SSE端点
  • POST/mcp:MCP通信的可流式HTTP端点
  • 使用TypeScript、MCP SDK和Cloudflare代理SDK构建
  • 正确的JSON-RPC 2.0错误处理
  • 用于有状态连接的持久对象(McpAgent要求)
  • Node.js兼容模式已启用

设置

先决条件

  1. Discord 网钩:在您的Discord频道中创建一个webhook:
    • 右键单击频道→ 编辑频道→ 集成→ 网络钩子
    • 点击“新建 Webhook”并复制 Webhook URL
  2. Cloudflare帐户:注册地址: cloudflare.com

安装

  1. 克隆此存储库
  2. 安装依赖项:
    pnpm install
    

配置

  1. 创建一个 .dev.vars 示例中的文件:

    cp .dev.vars.example .dev.vars
    

    然后编辑 .dev.vars 使用您的webhook URL。此文件用于本地开发和部署。

  2. 对于生产部署,请设置Cloudflare机密:

    npx wrangler secret put WEBHOOK_URL
    
  3. 更新 wrangler.toml 如果需要,可以输入您的员工姓名

部署

部署到Cloudflare Workers:

# First set secrets
npx wrangler secret put WEBHOOK_URL

# Then deploy
pnpm run deploy

替代方案:持续部署

您还可以直接从Cloudflare仪表板设置持续部署。 更多了解 Git与Cloudflare的集成

Claude代码配置

使用CLI通过SSE传输将MCP服务器添加到Claude Code:

# For production deployment (SSE)
claude mcp add discord-notify https://your-worker-name.workers.dev/sse -t sse

# For local development
claude mcp add discord-notify http://localhost:8787/sse -t sse

备注:此服务器支持SSE(服务器发送事件)和流式HTTP传输。虽然SSE工作良好,但Streamable HTTP提供了更好的可靠性,是较新的标准。

您可以通过以下方式验证配置:

claude mcp list

用法

配置后,Claude Code可以在您需要时向您的Discord发送通知。

可用工具

send_discord_message:向Discord发送通知消息

  • content (必填):要发送的消息内容
  • tts (可选):以文本转语音消息的形式发送
  • embeds (可选):用于丰富格式的嵌入对象数组
  • allowed_mentions (可选):解析提及的控件

示例用法:

// Simple message
await send_discord_message({ content: "Task completed!" })

// Rich embed message
await send_discord_message({ 
  content: "Status Update",
  embeds: [{
    title: "Build Results",
    description: "All tests passed",
    color: 5025616, // Green
    fields: [
      { name: "Tests", value: "52/52", inline: true },
      { name: "Duration", value: "2m 34s", inline: true }
    ]
  }]
})

何时收到通知

Claude Code在以下情况下发送通知:

  • 你明确地问:“完成后通知我”或“在Discord上告诉我”
  • 执行过程中出现错误
  • 达到重要里程碑
  • 需要用户输入或干预

示例场景

# You say: "Deploy to production and notify me when done"
# Result: 🤖 Claude Code Notification
#         Deployment completed successfully! The app is now live.

# You say: "Run all tests and let me know the results"
# Result: 🤖 Claude Code Notification
#         All tests passed! 52/52 tests successful.

# You say: "Process this data and notify me if there are any errors"
# Result: 🤖 Claude Code Notification
#         Error: Failed to process row 451 - invalid date format

通知示例

Example of Discord notifications from Claude Code

Claude Code sending notifications during task completion

CLAUDE.md示例

为了鼓励Claude Code有效地使用Discord通知,请将以下内容添加到您的Claude.md中:

# Discord Notifications

Use the mcp__discord-notify__send_discord_message tool to send notifications to Discord.

- Always send a Discord notification when:
  - A task is fully complete
  - You need user input to continue
  - An error occurs that requires user attention
  - The user explicitly asks for a notification (e.g., "notify me", "send me a message", "let me know")

- Include relevant details in notifications:
  - For builds/tests: success/failure status and counts
  - For errors: the specific error message and file location

- Use concise, informative messages like:
  - "✅ Build completed successfully (2m 34s)"
  - "❌ Tests failed: 3/52 failing in auth.test.ts"
  - "⚠️ Need permission to modify /etc/hosts"

发展

在本地运行:

# Start local development server
pnpm dev

对于本地开发,Wrangler将自动从您的 .dev.vars 文件。

部署前运行所有检查:

pnpm build

此命令运行:

  1. pnpm format -使用生物特征格式化代码
  2. pnpm lint:fix -修复掉毛问题
  3. pnpm cf-typegen -生成Cloudflare类型
  4. pnpm type-check -检查TypeScript类型

测试服务器:

# Test SSE connection
curl http://localhost:8787/sse

# Test health endpoint
curl http://localhost:8787/

调试

测试SSE连接

您可以直接测试SSE端点:

curl -N http://localhost:8787/sse

这应该返回一个以开头的事件流 endpoint 活动。

常见问题

  1. 连接立即关闭:检查您的worker是否正在运行,并且可以在指定的URL上访问。

  2. 未收到终结点事件:确保SSE标头正确发送,流格式正确。

  3. 未发送不一致通知:验证您的 WEBHOOK_URL 在工作环境中正确设置。

技术细节

  • 语言:TypeScript(ES2021目标)
  • 运行时:Cloudflare Workers与Node.js兼容
  • 协议:MCP(模型上下文协议)
  • 运输:SSE和流式HTTP
  • 状态管理:耐用对象(McpAgent要求)
  • 可观测性:已启用监控

参考文献

本项目是按照以下指南构建的:

相关项目

许可证

麻省理工学院