get_comments_by_submission
Extract and analyze comments from a Reddit submission using the submission ID, optionally replacing MoreComments with detailed replies for comprehensive insights.
Instructions
Retrieve comments from a specific submission.
Args:
submission_id: ID of the submission to get comments from
replace_more: Whether to replace MoreComments objects with actual comments
Returns:
List of comments with their replies
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| replace_more | No | ||
| submission_id | Yes |
Implementation Reference
- The main handler function implementing the tool logic to fetch comments from a Reddit submission, process them recursively, and return structured data.@validate_call(validate_return=True) def get_comments_by_submission( submission_id: str, replace_more: bool = True ) -> List[CommentResult]: """ Retrieve comments from a specific submission. Args: submission_id: ID of the submission to get comments from replace_more: Whether to replace MoreComments objects with actual comments Returns: List of comments with their replies """ client = RedditClient.get_instance() submission = client.reddit.submission(submission_id) if replace_more: submission.comments.replace_more() return [ result for comment in submission.comments.list() if (result := comment_to_model(comment)) is not None ]
- Pydantic BaseModel defining the schema for comment data, including nested replies, used for validation and typing.class CommentResult(BaseModel): """Reddit comment details""" id: str = Field(description="Unique identifier of the comment") body: str = Field(description="Text content of the comment") author: str | None = Field(description="Username of the author, or None if deleted") created_utc: str = Field(description="UTC timestamp when comment was created") is_submitter: bool = Field( description="Whether the comment author is the submission author" ) score: int = Field(description="Number of upvotes minus downvotes") replies: List["CommentResult"] = Field( description="List of reply comments", default_factory=list ) CommentResult.model_rebuild() # Required for self-referential models
- src/reddit_mcp/tools/__init__.py:16-24 (registration)Registry list exporting the get_comments_by_submission tool function for use by the MCP server.# Registry of all available tools tools = [ get_submission, get_subreddit, get_comments_by_submission, get_comment_by_id, search_posts, search_subreddits, ]
- src/reddit_mcp/server.py:11-13 (registration)MCP server code that iterates over the tools list and registers each tool with the FastMCP server instance.for tool in tools: logger.info(f"Registering tool: {tool.__name__}") mcp.tool()(tool)
- Recursive helper utility to convert PRAW Reddit comment objects into CommentResult models, handling nested replies and skipping placeholders.def comment_to_model(comment) -> CommentResult: """Convert PRAW comment object to CommentResult model.""" # Skip MoreComments objects if isinstance(comment, MoreComments): return None return CommentResult( id=comment.id, body=comment.body, author=None if comment.author is None else comment.author.name, created_utc=format_utc_timestamp(comment.created_utc), is_submitter=comment.is_submitter, score=comment.score, replies=[ result for reply in comment.replies if (result := comment_to_model(reply)) is not None ], )