Reddit MCP 服务器是一个模型上下文协议(MCP)服务器,它通过专门为深入研究和分析设计的三层架构,为大语言模型(LLMs)提供全面访问 Reddit 内容的能力。该服务器基于 FastMCP 和 PRAW 构建,可实现高效部署。
client_id
(在“个人使用脚本”下方)和 client_secret
git clone
cd reddit-mcp-poc
pip install uv
uv sync
在项目根目录下创建一个 .env
文件:
REDDIT_CLIENT_ID=your_client_id_here
REDDIT_CLIENT_SECRET=your_client_secret_here
REDDIT_USER_AGENT=RedditMCP/1.0 by u/your_username
uv run src/server.py
fastmcp dev src/server.py
服务器启动后,即可接受 MCP 连接。
本服务器采用独特的三层架构,引导大语言模型对 Reddit 进行全面研究:
discover_reddit_resources
)get_operation_requirements
)execute_reddit_operation
)为获得最佳效果,请遵循以下利用三层架构的工作流程:
# 1. 发现 - 查找相关社区
discover_reddit_resources(
topic="machine learning ethics",
discovery_depth="comprehensive"
)
# 2. 需求 - 获取参数指导(可选)
get_operation_requirements("fetch_multiple", context="ML ethics discussion")
# 3. 执行 - 从多个社区获取内容
execute_reddit_operation("fetch_multiple", {
"subreddit_names": ["MachineLearning", "artificial", "singularity", "ethics"],
"limit_per_subreddit": 8
})
# 4. 深入挖掘 - 获取有前景帖子的评论
execute_reddit_operation("fetch_comments", {
"submission_id": "abc123",
"comment_limit": 100
})
为何此流程有效:
# 推荐:完整的研究工作流程
# 步骤 1: 发现社区
result = discover_reddit_resources(
topic="sustainable technology",
discovery_depth="comprehensive"
)
# 返回:8 - 15 个相关子版块 + 推荐操作
# 步骤 2: 获取操作要求(可选)
schema = get_operation_requirements("fetch_multiple")
# 返回:参数模式、建议和常见错误
# 步骤 3: 使用发现的社区执行操作
posts = execute_reddit_operation("fetch_multiple", {
"subreddit_names": result["relevant_communities"]["subreddits"][:8],
"listing_type": "hot",
"limit_per_subreddit": 6
})
# 步骤 4: 深入挖掘有前景的讨论
comments = execute_reddit_operation("fetch_comments", {
"submission_id": "interesting_post_id",
"comment_limit": 100
})
# 在整个 Reddit 上搜索
execute_reddit_operation("search_all", {
"query": "artificial intelligence ethics",
"sort": "top",
"time_filter": "week",
"limit": 15
})
# 在特定子版块中搜索
execute_reddit_operation("search_subreddit", {
"subreddit_name": "MachineLearning",
"query": "transformer architecture",
"limit": 20
})
# 从已知子版块批量获取内容(效率提高 70%)
execute_reddit_operation("fetch_multiple", {
"subreddit_names": ["artificial", "singularity", "Futurology"],
"listing_type": "hot",
"limit_per_subreddit": 8
})
服务器通过 execute_reddit_operation
提供以下操作来访问 Reddit:
操作 | 描述 | 适用场景 |
---|---|---|
search_all |
在整个 Reddit 上进行搜索 | 广泛主题探索 |
search_subreddit |
在特定子版块中进行搜索 | 针对性社区搜索 |
fetch_posts |
获取子版块的最新帖子 | 了解当前趋势和活动 |
fetch_multiple |
⚡ 从多个子版块批量获取内容 | 多社区研究 |
fetch_comments |
获取带有完整讨论的帖子 | 深入分析对话 |
工具 | 用途 | 使用时机 |
---|---|---|
discover_reddit_resources |
查找相关社区和操作 | 始终从这里开始 |
get_operation_requirements |
获取详细的参数模式 | 复杂操作之前 |
execute_reddit_operation |
执行任何 Reddit 操作 | 获取要求之后 |
服务器提供三种 MCP 资源,用于访问常用数据:
返回 25 个最热门子版块的列表,包含订阅者数量和描述。
获取特定子版块的详细信息,包括:
返回 MCP 服务器的全面信息,包括:
reddit-mcp-poc/
├── src/
│ ├── server.py # 具有三层架构的主 MCP 服务器
│ ├── config.py # Reddit 客户端配置
│ ├── models.py # Pydantic 数据模型
│ ├── resources.py # MCP 资源实现
│ └── tools/ # 工具实现
│ ├── search.py # 搜索功能(支持永久链接)
│ ├── posts.py # 子版块帖子获取
│ ├── comments.py # 评论获取
│ └── discover.py # 子版块发现
├── tests/
│ └── test_tools.py # 单元测试
├── pyproject.toml # 项目依赖
├── .env # 你的 API 凭证
└── README.md # 本文件
服务器能够优雅地处理常见的 Reddit API 错误:
此 MVP 实现存在一些有意设置的限制:
基于三层架构的基础,后续计划如下:
get_operation_requirements
,提供更丰富的上下文感知建议本项目采用 MIT 许可证。
欢迎贡献代码!请随时提交拉取请求。
问题 | 解决方案 |
---|---|
"Reddit API credentials not found" | 确保 .env 文件存在且包含有效凭证 |
速率限制错误 | 等待几分钟,PRAW 会自动处理 |
"Subreddit not found" | 验证子版块名称(无需 r/ 前缀) |
无搜索结果 | 尝试更广泛的搜索词或不同的时间过滤器 |
导入错误 | 运行 uv sync 安装所有依赖项 |