fetch_reddit_hot_threads
Retrieve trending posts from a specified subreddit using predefined parameters to fetch up to a custom number of threads.
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 |
|---|---|---|---|
| limit | No | ||
| subreddit | Yes |
Implementation Reference
- src/mcp_reddit/reddit_fetcher.py:19-50 (handler)The main handler for the 'fetch_reddit_hot_threads' tool. Decorated with @mcp.tool() for registration, fetches hot threads from the specified subreddit using redditwarp Client, formats post information (title, score, comments, author, type, content, link) using helpers, handles exceptions.@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 _get_post_type to determine Reddit post type (link, text, gallery, unknown) based on submission class.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 _get_content to extract post content or permalink based on post type.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
- src/mcp_reddit/reddit_fetcher.py:8-8 (registration)Initializes the FastMCP server instance named 'Reddit MCP' where the tool is registered via decorator.mcp = FastMCP("Reddit MCP")