Skip to main content
Glama

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
NameRequiredDescriptionDefault
replace_moreNo
submission_idYes

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
  • 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,
    ]
  • 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
            ],
        )
Install Server

Other Tools

Related Tools

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

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