Skip to main content
Glama
Hawstein

MCP Server Reddit

by Hawstein

get_post_comments

Retrieve comments from Reddit posts by providing a post ID, with configurable limits for comment quantity.

Instructions

Get comments from a post

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
post_idYesID of the post
limitNoNumber of comments to return (default: 10)

Implementation Reference

  • The core handler function that fetches the comment tree from Reddit using redditwarp.Client and constructs a flat list of top-level Comment objects using the _build_comment_tree helper.
    def get_post_comments(self, post_id: str, limit: int = 10) -> list[Comment]:
        """Get comments from a post"""
        comments = []
        tree_node = self.client.p.comment_tree.fetch(post_id, sort='top', limit=limit)
        for node in tree_node.children:
            comment = self._build_comment_tree(node)
            if comment:
                comments.append(comment)
        return comments
  • The input schema definition for the get_post_comments tool, registered in the list_tools handler.
    Tool(
        name=RedditTools.GET_POST_COMMENTS.value,
        description="Get comments from a post",
        inputSchema={
            "type": "object",
            "properties": {
                "post_id": {
                    "type": "string",
                    "description": "ID of the post",
                },
                "limit": {
                    "type": "integer",
                    "description": "Number of comments to return (default: 10)",
                    "default": 10,
                    "minimum": 1,
                    "maximum": 100
                }
            },
            "required": ["post_id"]
        }
    ),
  • The dispatch case in the call_tool handler that validates arguments and invokes the get_post_comments method.
    case RedditTools.GET_POST_COMMENTS.value:
        post_id = arguments.get("post_id")
        if not post_id:
            raise ValueError("Missing required argument: post_id")
        limit = arguments.get("limit", 10)
        result = reddit_server.get_post_comments(post_id, limit)
  • Recursive helper function to build nested Comment objects from the redditwarp comment tree node, limiting depth to prevent excessive recursion.
    def _build_comment_tree(self, node, depth: int = 3) -> Comment | None:
        """Helper method to recursively build comment tree"""
        if depth <= 0 or not node:
            return None
    
        comment = node.value
        replies = []
        for child in node.children:
            child_comment = self._build_comment_tree(child, depth - 1)
            if child_comment:
                replies.append(child_comment)
    
        return Comment(
            id=comment.id36,
            author=comment.author_display_name or '[deleted]',
            body=comment.body,
            score=comment.score,
            replies=replies
        )
  • Pydantic model defining the structure of a Comment, used as return type for get_post_comments.
    class Comment(BaseModel):
        id: str
        author: str
        body: str
        score: int
        replies: list['Comment'] = []

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/Hawstein/mcp-server-reddit'

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