get_post_comments
Retrieve comments from a Reddit post by providing the post ID. Sort comments by best, top, new, controversial, or old to analyze discussions.
Instructions
Get comments from a Reddit post
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| postId | Yes | Reddit post ID | |
| sort | No | Sort order for comments | best |
Implementation Reference
- src/reddit-client.ts:139-148 (handler)Core handler function that fetches comments from Reddit API for a given post, extracts them recursively, and returns a flat list of RedditComment objects.async getPostComments(postId: string, sort: 'best' | 'top' | 'new' | 'controversial' | 'old' = 'best'): Promise<RedditComment[]> { const data = await this.makeRequest(`/comments/${postId}?sort=${sort}`); const comments: RedditComment[] = []; if (data[1] && data[1].data && data[1].data.children) { this.extractComments(data[1].data.children, comments); } return comments; }
- src/index.ts:322-333 (handler)MCP CallToolRequest handler case that validates input using Zod schema and delegates execution to RedditClient.getPostComments, formatting response as JSON text.case 'get_post_comments': { const args = GetPostCommentsSchema.parse(request.params.arguments); const comments = await redditClient.getPostComments(args.postId, args.sort); return { content: [ { type: 'text', text: JSON.stringify(comments, null, 2), }, ], }; }
- src/index.ts:48-51 (schema)Zod schema defining and validating the tool's input parameters: postId (required string) and sort (enum with default 'best'). Used for parsing in handler.const GetPostCommentsSchema = z.object({ postId: z.string().min(1, "Post ID is required"), sort: z.enum(['best', 'top', 'new', 'controversial', 'old']).default('best'), });
- src/index.ts:130-149 (registration)Tool registration in ListTools response, specifying name, description, and JSON inputSchema advertised to MCP clients.{ name: 'get_post_comments', description: 'Get comments from a Reddit post', inputSchema: { type: 'object', properties: { postId: { type: 'string', description: 'Reddit post ID', }, sort: { type: 'string', enum: ['best', 'top', 'new', 'controversial', 'old'], description: 'Sort order for comments', default: 'best', }, }, required: ['postId'], }, },
- src/reddit-client.ts:190-198 (helper)Recursive helper function to traverse Reddit's nested comment tree and collect all top-level and reply comments into a flat array.private extractComments(children: any[], comments: RedditComment[]): void { for (const child of children) { if (child.kind === 't1' && child.data) { comments.push(this.mapComment(child.data)); if (child.data.replies && child.data.replies.data && child.data.replies.data.children) { this.extractComments(child.data.replies.data.children, comments); } } }