Reddit MCP服务器

一个提供Reddit订阅功能并包含缓存、速率限制、全面错误处理和健壮数据解析等增强功能的模型上下文协议(MCP)服务器。

  • by zicochaos
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 zicochaos/reddit-mcp

使用说明

Reddit MCP服务器

一种模型上下文协议(MCP)服务器,提供Reddit提要功能,具有增强的功能,包括缓存、速率限制、全面的错误处理和强大的数据解析。

Claude Desktop快速入门

  1. 安装 紫外线:

    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  2. 添加到Claude桌面配置:

    {
      "mcpServers": {
        "reddit": {
          "command": "/path/to/uv",
          "args": [
            "run",
            "--no-project",
            "--with",
            "mcp>=0.1.0",
            "--with",
            "httpx>=0.25.0",
            "--with",
            "typing-extensions>=4.8.0",
            "/path/to/reddit-mcp/reddit_mcp_server.py"
          ]
        }
      }
    }
    
  3. 重新启动Claude Desktop并开始使用Reddit工具!

特性

  • 子涡流进料检索:从任何子版块获取最新帖子
  • 用户源检索:获取Reddit用户的帖子和评论
  • 高级过滤:按热门、新、热门、有争议的时间范围排序
  • 搜索功能:在子版块和整个Reddit上搜索
  • 评论检索:通过嵌套回复获取特定帖子的评论
  • 缓存:内置具有TTL的LRU缓存,以减少API调用
  • 速率限制:令牌桶速率限制,自动重试
  • 类型安全:完整的类型提示和数据验证
  • 错误处理:全面的错误处理和详细的消息
  • 稳健解析:处理Reddit API异常,包括字符串浮动值
  • 连接池:高效的HTTP连接管理
  • 分页支持:浏览大型结果集

安装

选项1:传统安装

# Clone the repository
git clone <repository-url>
cd reddit-mcp

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

选项2:使用uv(不需要虚拟环境)

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Clone the repository
git clone <repository-url>
cd reddit-mcp

# Run directly with uv (see Configuration section for Claude Desktop setup)
uv run --no-project --with mcp>=0.1.0 --with httpx>=0.25.0 --with typing-extensions>=4.8.0 reddit_mcp_server.py

配置

服务器可以通过环境变量进行配置:

# Cache settings
REDDIT_CACHE_TTL=300  # Cache TTL in seconds (default: 300)
REDDIT_CACHE_SIZE=100  # Maximum cache entries (default: 100)

# Rate limiting
REDDIT_RATE_LIMIT_CALLS=60  # Calls per minute (default: 60)
REDDIT_RATE_LIMIT_WINDOW=60  # Window in seconds (default: 60)

# Request settings
REDDIT_REQUEST_TIMEOUT=30  # Request timeout in seconds (default: 30)
REDDIT_MAX_RETRIES=3  # Maximum retry attempts (default: 3)

# Reddit API settings
REDDIT_USER_AGENT="YourApp/1.0"  # Custom user agent (optional)

用法

启动服务器

# Option 1: With Python (requires virtual environment)
python reddit_mcp_server.py

# Option 2: With uv (no virtual environment needed)
uv run --no-project --with mcp>=0.1.0 --with httpx>=0.25.0 --with typing-extensions>=4.8.0 reddit_mcp_server.py

有关Claude Desktop集成,请参阅 安装指南 用于配置示例。

有关LLM使用示例和最佳实践,请参阅:

可用工具

1.获取subreddit_feed

通过可选的筛选从子版块或Reddit首页获取帖子。

{
  "subreddit": "python",  // Optional: leave empty for frontpage
  "sort": "hot",
  "time_filter": "week",
  "limit": 25,
  "after": "t3_abc123"  // Optional: for pagination
}

2.用户订阅

获取Reddit用户的帖子和评论。

{
  "username": "spez",
  "content_type": "all",
  "sort": "new",
  "limit": 25
}

3.搜索_添加

在Reddit上搜索帖子。

{
  "query": "machine learning",
  "subreddit": "all",
  "sort": "relevance",
  "time_filter": "month",
  "limit": 25
}

4.get_post_comments

获取特定帖子的详细帖子内容和评论。

{
  "post_id": "t3_abc123",
  "sort": "best",
  "limit": 100,
  "depth": 2  // Optional: depth of nested replies to retrieve
}

5.获取子信息

获取有关subreddit的详细信息。

{
  "subreddit": "python"
}

数据模型

发布对象

{
  "id": "t3_abc123",
  "title": "Post Title",
  "description": "Post content",
  "link": "https://reddit.com/...",
  "author": {
    "username": "author_name",
    "id": "t2_user123"
  },
  "subreddit": {
    "name": "subreddit_name",
    "id": "t5_sub123",
    "subscribers": 1000000
  },
  "stats": {
    "score": 1234,
    "upvotes": 1300,
    "downvotes": 66,
    "upvote_ratio": 0.95,
    "comments": 456,
    "crossposts": 3,
    "awards": 2
  },
  "metadata": {
    "created_utc": 1234567890,
    "edited": false,
    "is_video": false,
    "is_self": true,
    "over_18": false,
    "spoiler": false,
    "stickied": false,
    "locked": false,
    "archived": false
  }
}

评论对象

{
  "id": "t1_def456",
  "body": "Comment text",
  "author": {
    "username": "commenter",
    "id": "t2_user456"
  },
  "post": {
    "id": "t3_abc123",
    "title": "Post Title"
  },
  "stats": {
    "score": 123,
    "upvotes": 130,
    "downvotes": 7
  },
  "metadata": {
    "created_utc": 1234567890,
    "edited": false,
    "stickied": false
  },
  "replies": []  # Nested replies if depth > 0
}

错误处理

服务器为常见场景提供详细的错误消息:

  • 无效输入:特定字段信息的验证错误
  • 速率限制:使用指数回退自动重试
  • 网络错误:超时和连接错误处理
  • API错误:带有状态代码的Reddit API错误响应
  • 数据错误:妥善处理丢失或格式错误的数据
  • 字符串浮点分析:处理Reddit API以字符串形式返回的浮动(例如,“88.0”)
  • 404错误:优雅地处理不存在的子版块或用户
  • 403错误:清除私有/隔离子版块的消息

LLM使用

此MCP服务器旨在与大型语言模型(如Claude、GPT-4和其他支持模型上下文协议的AI助手)无缝协作。

快速示例

User: "What's trending on Reddit right now?"
User: "Show me the top posts from r/technology this week"
User: "Find discussions about climate change"
User: "What is r/python about?"

常见用例

  1. 内容发现

    • 浏览Reddit或特定子版块的热门话题
    • 查找关于任何主题的最佳帖子
    • 发现新社区
  2. 研究与分析

    • 分析围绕产品、事件或主题的情绪
    • 比较不同社区的讨论
    • 跟踪对话如何随时间演变
  3. 社区见解

    • 获取有关subreddit的详细信息
    • 了解社区规则和文化
    • 寻找活跃的讨论和专家意见

LLM交互示例

User: "I want to learn about machine learning. What are the best Reddit resources?"

LLM: "I'll help you find the best machine learning resources on Reddit. Let me search for relevant communities and posts."

[Uses get_subreddit_info for "MachineLearning"]
[Uses search_reddit for "beginner guide machine learning"]
[Uses get_subreddit_feed for top posts from relevant subreddits]

LLM: "Here are the best Reddit resources for learning machine learning:

**Communities:**
- r/MachineLearning (2.8M members) - Main hub for ML news and research
- r/learnmachinelearning (890K members) - Focused on learning and beginners
- r/MLQuestions (125K members) - Q&A for ML problems

**Top Resources:**
1. 'Complete Beginner's Guide to ML' - comprehensive guide with 5.6K upvotes
2. 'Free ML Course Collection' - curated list of courses
3. 'From Zero to ML Engineer Roadmap' - step-by-step learning path

Would you like me to get more specific posts or explore any of these communities in detail?"

法学硕士最佳实践

  1. 从宽开始,然后缩小范围 -在浏览特定帖子之前,请使用搜索或首页
  2. 先查看社区信息 -了解subreddit文化有助于解读内容
  3. 使用适当的排序 -“顶级”代表最佳内容,“新”代表最新内容,“有争议”代表辩论
  4. 优雅地处理错误 -一些子版块可能是私有的或NSFW的
  5. 遵守费率限制 -服务器会自动处理此问题,但会通知用户延迟

有关更详细的示例和集成指南,请参阅:

发展

项目结构

reddit-mcp/
├── reddit_mcp_server.py    # Main MCP server implementation
├── models.py               # Data models with safe int parsing
├── client.py               # HTTP client with retry logic
├── cache.py                # LRU caching with TTL support
├── rate_limiter.py         # Token bucket rate limiting
├── config.py               # Configuration management
├── utils.py                # Utility functions
├── requirements.txt        # Python dependencies
├── docs/                   # Documentation
│   ├── API_REFERENCE.md    # Complete API documentation
│   ├── CONFIGURATION.md    # Configuration guide
│   ├── SETUP_GUIDE.md      # Installation guide
│   ├── TROUBLESHOOTING.md  # Common issues and solutions
│   ├── USAGE_EXAMPLES.md   # Code examples
│   ├── LLM_USAGE_GUIDE.md  # LLM integration guide
│   ├── RESPONSE_EXAMPLES.md # Response format examples
│   └── CONTRIBUTING.md     # Contribution guidelines
├── CHANGELOG.md            # Version history
├── LICENSE                 # MIT License
└── README.md              # This file

贡献

看 贡献.md 详细的贡献指南。

许可证

MIT许可证-有关详细信息,请参阅许可证文件

仓库