get_post_comments
Retrieve comments from a Reddit post by specifying the post ID and desired comment limit. This tool simplifies accessing and analyzing post discussions on Reddit.
Instructions
Get comments from a post
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of comments to return (default: 10) | |
| post_id | Yes | ID of the post |
Input Schema (JSON Schema)
{
"properties": {
"limit": {
"default": 10,
"description": "Number of comments to return (default: 10)",
"maximum": 100,
"minimum": 1,
"type": "integer"
},
"post_id": {
"description": "ID of the post",
"type": "string"
}
},
"required": [
"post_id"
],
"type": "object"
}
Implementation Reference
- src/mcp_server_reddit/server.py:183-191 (handler)Core handler function that fetches the top comments tree using redditwarp and constructs a flat list of top-level Comment objects using the helper _build_comment_tree.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
- Pydantic model defining the output structure for comments, including nested replies.class Comment(BaseModel): id: str author: str body: str score: int replies: list['Comment'] = []
- src/mcp_server_reddit/server.py:350-370 (registration)Registers the tool with the MCP server, providing name, description, and JSON input schema for validation.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"] } ),
- src/mcp_server_reddit/server.py:427-432 (registration)In the call_tool handler, matches the tool name and dispatches to the RedditServer.get_post_comments method with arguments.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 used by the handler to construct nested Comment objects from the redditwarp comment tree nodes, limited to depth 3.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 )