使用说明
FastMCP Discord集成服务器
安全、可扩展 模型上下文协议(MCP)后端服务器 建造于 快速 API 和 FastMCP,使AI模型能够通过一套全面的工具与Discord进行交互。
🌟 特性
- Discord Bot集成:发送消息、检索历史记录、搜索内容和调节频道
- MCP协议支持:与MCP Inspector和MCP客户端完全兼容
- 安全认证:API基于密钥的身份验证,具有基于角色的权限
- 多租户技术:每个客户端支持多个Discord机器人
- 实时调试:与MCP Inspector集成,用于请求监控
- 综合录井:带审计跟踪的结构化日志记录
- 速率限制:内置防滥用保护
- FastAPI集成:RESTful API端点与MCP工具
🛠️ 可用的MCP工具
| 工具 | 描述 | 所需权限 |
|---|---|---|
send_message |
向Discord频道发送消息 | 发送消息 |
get_messages |
从频道检索最近的消息 | 读取消息历史记录 |
get_channel_info |
获取详细频道信息 | 查看频道 |
search_messages |
按关键字搜索邮件 | 阅读邮件历史记录 |
delete_message |
删除特定邮件 | 管理邮件 |
ban_user |
禁止用户加入公会 | 禁止会员 |
kick_user |
从公会中踢用户 | 踢会员 |
get_guild_info |
获取帮会详细信息 | 查看频道 |
🚀 快速开始
1.先决条件
- Python 3.8+
- Discord Bot令牌
- 虚拟环境(推荐)
2.安装
# Clone the repository
git clone <repository-url>
cd fastmcp-discord-server
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
3.配置
# Copy the sample environment file
cp env.sample .env
# Edit .env with your configuration
nano .env
所需的环境变量:
DISCORD_BOT_TOKEN:您的Discord机器人令牌API_KEY_SECRET:API密钥生成的秘密JWT_SECRET_KEY:JWT代币的秘密
4.Discord Bot设置
- 首选 Discord开发者门户
- 创建新应用程序
- 转到“Bot”部分并创建一个Bot
- 将机器人令牌复制到您的
.env文件 - 启用以下bot权限:
- 发送消息
- 读取消息历史记录
- 查看频道
- 管理邮件(用于审核)
- 禁止成员(适度)
- Kick会员(用于审核)
5.运行服务器
# Run FastAPI server (recommended for development)
python -m app.main
# Or run MCP server directly
python -m app.main mcp
# Or using uvicorn directly
uvicorn app.main:app --reload --host 127.0.0.1 --port 8000
📡 API使用
认证
所有API调用都需要通过API密钥进行身份验证:
# Generate an API key
curl -X POST "http://localhost:8000/generate-api-key" \
-H "Content-Type: application/json" \
-d '{"user_id": "your_user_id"}'
使用MCP工具
# List available tools
curl -X GET "http://localhost:8000/mcp/tools" \
-H "X-API-Key: your_api_key"
# Call a tool
curl -X POST "http://localhost:8000/mcp/call-tool" \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{
"tool_name": "send_message",
"parameters": {
"channel_id": 123456789,
"content": "Hello from MCP!"
}
}'
健康检查
curl -X GET "http://localhost:8000/health"
🔧 MCP客户端集成
与MCP检查器一起使用
- 安装MCP检查器
- 配置连接到
http://localhost:8000 - 使用API密钥进行身份验证
- 监控实时请求和响应
使用AI模型
配置您的AI模型客户端以使用MCP服务器:
{
"server_url": "http://localhost:8000",
"api_key": "your_api_key",
"tools": ["send_message", "get_messages", "search_messages"]
}
🏗️ 项目结构
fastmcp_backend/
├── app/
│ ├── __init__.py
│ ├── main.py # Main FastAPI + MCP server
│ ├── config.py # Configuration management
│ ├── auth/
│ │ ├── __init__.py
│ │ └── middleware.py # Authentication middleware
│ ├── discord/
│ │ ├── __init__.py
│ │ └── bot.py # Discord bot integration
│ ├── mcp/
│ │ ├── __init__.py
│ │ └── tools.py # MCP tools implementation
│ ├── models/
│ │ └── __init__.py
│ ├── schemas/
│ │ └── __init__.py
│ ├── db/
│ │ └── __init__.py
│ └── tests/
│ └── __init__.py
├── requirements.txt
├── env.sample
└── README.md
🔐 安全功能
- API密钥验证:基于HMAC-的API安全密钥
- 速率限制:可配置的请求限制
- CORS 保护:可配置的允许来源
- 输入验证:Pydantic模式验证
- 审计日志:完成动作记录
- 权限检查:基于角色的访问控制
🧪 测试
# Run tests
pytest app/tests/
# Run with coverage
pytest --cov=app app/tests/
# Run specific test
pytest app/tests/test_discord.py
📊 监控
日志
日志的结构包括:
- 请求/响应详细信息
- 用户操作
- 误差跟踪
- 性能指标
健康监测
这 /health 端点提供:
- 服务器状态
- Discord机器人状态
- 系统度量
- 时间戳信息
🚀 部署
发展
python -m app.main
生产
# Using gunicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker
# Using Docker (create Dockerfile)
docker build -t fastmcp-discord .
docker run -p 8000:8000 fastmcp-discord
📚 例子
发送信息
import httpx
async def send_discord_message():
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:8000/mcp/call-tool",
headers={"X-API-Key": "your_api_key"},
json={
"tool_name": "send_message",
"parameters": {
"channel_id": 123456789,
"content": "Hello from FastMCP!"
}
}
)
return response.json()
搜索邮件
async def search_messages():
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:8000/mcp/call-tool",
headers={"X-API-Key": "your_api_key"},
json={
"tool_name": "search_messages",
"parameters": {
"channel_id": 123456789,
"query": "important",
"limit": 10
}
}
)
return response.json()
🤝 贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加测试
- 提交拉取请求
📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
🆘 支持
🔄 更新日志
v1.0.0
- 初始版本
- 完全支持MCP协议
- Discord机器人集成
- 身份验证系统
- 综合录井
- 速率限制
- 健康监测