fetch_reddit_hot_threads
Retrieve trending posts from any subreddit to monitor popular discussions and content.
Instructions
Fetch hot threads from a subreddit
Args: subreddit: Name of the subreddit limit: Number of posts to fetch (default: 10)
Returns: Human readable string containing list of post information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| subreddit | Yes | ||
| limit | No |
Implementation Reference
- src/mcp_reddit/reddit_fetcher.py:19-50 (handler)The main handler function for the 'fetch_reddit_hot_threads' tool. It fetches hot posts from a specified subreddit using the redditwarp client, formats post information including title, score, comments, author, type, content, and link, and returns a formatted string. Registered via @mcp.tool() decorator.@mcp.tool() async def fetch_reddit_hot_threads(subreddit: str, limit: int = 10) -> str: """ Fetch hot threads from a subreddit Args: subreddit: Name of the subreddit limit: Number of posts to fetch (default: 10) Returns: Human readable string containing list of post information """ try: posts = [] async for submission in client.p.subreddit.pull.hot(subreddit, limit): post_info = ( f"Title: {submission.title}\n" f"Score: {submission.score}\n" f"Comments: {submission.comment_count}\n" f"Author: {submission.author_display_name or '[deleted]'}\n" f"Type: {_get_post_type(submission)}\n" f"Content: {_get_content(submission)}\n" f"Link: https://reddit.com{submission.permalink}\n" f"---" ) posts.append(post_info) return "\n\n".join(posts) except Exception as e: logging.error(f"An error occurred: {str(e)}") return f"An error occurred: {str(e)}"
- Helper function to determine the type of a Reddit submission (link, text, gallery, or unknown). Used in post_info formatting.def _get_post_type(submission) -> str: """Helper method to determine post type""" if isinstance(submission, LinkPost): return 'link' elif isinstance(submission, TextPost): return 'text' elif isinstance(submission, GalleryPost): return 'gallery' return 'unknown'
- Helper function to extract appropriate content or link from a Reddit submission based on its type. Used in post_info formatting.def _get_content(submission) -> Optional[str]: """Helper method to extract post content based on type""" if isinstance(submission, LinkPost): return submission.permalink elif isinstance(submission, TextPost): return submission.body elif isinstance(submission, GalleryPost): return str(submission.gallery_link) return None