get_comment_by_id
Retrieve detailed information and replies for a specific Reddit comment using its unique ID, enabling focused analysis or response generation.
Instructions
Retrieve a specific comment by ID.
Args:
comment_id: ID of the comment to retrieve
Returns:
Comment details with any replies
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| comment_id | Yes |
Input Schema (JSON Schema)
{
"properties": {
"comment_id": {
"title": "Comment Id",
"type": "string"
}
},
"required": [
"comment_id"
],
"title": "get_comment_by_idArguments",
"type": "object"
}
Implementation Reference
- The handler function that retrieves a specific Reddit comment by its ID using the RedditClient and converts it to a CommentResult model.@validate_call(validate_return=True) def get_comment_by_id(comment_id: str) -> CommentResult: """ Retrieve a specific comment by ID. Args: comment_id: ID of the comment to retrieve Returns: Comment details with any replies """ client = RedditClient.get_instance() return comment_to_model(client.reddit.comment(comment_id))
- Pydantic BaseModel defining the output schema for the comment data, including nested replies.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:17-24 (registration)Registers the get_comment_by_id function in the tools list for MCP tool discovery.tools = [ get_submission, get_subreddit, get_comments_by_submission, get_comment_by_id, search_posts, search_subreddits, ]
- Recursive helper function to convert PRAW comment objects (including replies) to the CommentResult Pydantic model.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 ], )