Skip to main content
Glama
adhikasp
by adhikasp

fetch_reddit_post_content

Retrieve detailed Reddit post content including comments by providing a post ID, enabling analysis and review of discussions.

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
NameRequiredDescriptionDefault
post_idYes
comment_limitNo
comment_depthNo

Implementation Reference

  • The main asynchronous handler function for the 'fetch_reddit_post_content' tool. It fetches the Reddit post using redditwarp Client, extracts post details, retrieves top comments up to specified limit and depth, formats them using helper functions, and returns a formatted string. Registered via @mcp.tool() decorator.
    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, including author, score, and body for each comment and its children.
    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 the type of the Reddit submission (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 the appropriate content string from the submission based on its 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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/adhikasp/mcp-reddit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server