fetch_reddit_post_content
Extract detailed content and comment threads from a specific Reddit post, including customizable comment limits and depth, to simplify content analysis and review.
Instructions
Fetch detailed content of a specific post
Args: post_id: Reddit post ID comment_limit: Number of top level comments to fetch comment_depth: Maximum depth of comment tree to traverse
Returns: Human readable string containing post content and comments tree
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| comment_depth | No | ||
| comment_limit | No | ||
| post_id | Yes |
Implementation Reference
- src/mcp_reddit/reddit_fetcher.py:67-102 (handler)The core handler function for the 'fetch_reddit_post_content' tool, decorated with @mcp.tool() for registration. Fetches Reddit post submission details, formats content based on post type, retrieves top comments up to specified limit and depth, and formats them into a readable tree structure using helper functions.@mcp.tool() async def fetch_reddit_post_content(post_id: str, comment_limit: int = 20, comment_depth: int = 3) -> str: """ Fetch detailed content of a specific post Args: post_id: Reddit post ID comment_limit: Number of top level comments to fetch comment_depth: Maximum depth of comment tree to traverse Returns: Human readable string containing post content and comments tree """ try: submission = await client.p.submission.fetch(post_id) content = ( f"Title: {submission.title}\n" f"Score: {submission.score}\n" f"Author: {submission.author_display_name or '[deleted]'}\n" f"Type: {_get_post_type(submission)}\n" f"Content: {_get_content(submission)}\n" ) comments = await client.p.comment_tree.fetch(post_id, sort='top', limit=comment_limit, depth=comment_depth) if comments.children: content += "\nComments:\n" for comment in comments.children: content += "\n" + _format_comment_tree(comment) else: content += "\nNo comments found." return content except Exception as e: return f"An error occurred: {str(e)}"
- Recursive helper function to format the comment tree with indentation, used in fetch_reddit_post_content to structure comments.def _format_comment_tree(comment_node, depth: int = 0) -> str: """Helper method to recursively format comment tree with proper indentation""" comment = comment_node.value indent = "-- " * depth content = ( f"{indent}* Author: {comment.author_display_name or '[deleted]'}\n" f"{indent} Score: {comment.score}\n" f"{indent} {comment.body}\n" ) for child in comment_node.children: content += "\n" + _format_comment_tree(child, depth + 1) return content
- Helper function to determine and return the type of the Reddit post (link, text, gallery, or unknown).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 string from the submission based on its type (permalink for links, body for text, gallery link for galleries).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