get_post_comments
Fetch and structure comments and replies for any Reddit post using its post_id. Optionally sort results by 'best,' 'top,' or 'new' for organized viewing.
Instructions
💬 Get comments for a Reddit post 🎯 What it does: Fetches comments and replies for any Reddit post 📝 Required: post_id (Reddit post ID, found in post URLs) ⚙️ Optional: sort ('best', 'top', 'new') 💡 Examples: • Get comments: {"post_id": "1n1nlse"} • Best comments: {"post_id": "1n1nlse", "sort": "best"} • New comments: {"post_id": "1n1nlse", "sort": "new"} 🔍 Output: Formatted comment tree with author, score, timestamp, and nested replies
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| post_id | Yes | Reddit post ID to get comments for | |
| sort | No | Sort order (default: best) | best |
Implementation Reference
- src/index.ts:615-652 (handler)MCP tool handler for 'get_post_comments': extracts params, applies smart defaults, calls redditAPI.getPostComments, parses and validates response, formats comment tree with formatDataList and formatRedditComment, returns MCP-formatted text response.createToolHandler(async (params: z.infer<typeof SimplePostCommentsSchema>) => { const { post_id, sort } = params; // 🧠 Smart defaults for missing parameters const smartDefaults = getSmartDefaults(params, 'comments'); const finalParams = { ...smartDefaults, post_id, sort: sort || smartDefaults.sort }; const result = await redditAPI.getPostComments(post_id, finalParams.limit, finalParams.sort); if (!result.success) { return createErrorResponse("Error getting post comments", result.error); } const data = result.data; if (!data || !Array.isArray(data) || data.length === 0) { return createErrorResponse("No comments found for this post"); } // The first element contains the post, the second contains comments const commentsData = data[1]; if (!commentsData || !commentsData.data || !commentsData.data.children) { return createErrorResponse("No comments found for this post"); } const comments = commentsData.data.children.map((child: any) => child.data); if (comments.length === 0) { return createSuccessResponse("No comments found for this post"); } const summary = `💬 Found ${comments.length} comments for post ${post_id} (sorted by ${sort})`; // ✅ DRY: Sử dụng formatDataList helper const commentDetails = formatDataList(comments, formatRedditComment, COMMENT_PREVIEW_LIMIT, "comments"); const resultText = `${summary}\n\n${commentDetails}`; return createSuccessResponse(resultText); })
- src/types/index.ts:118-121 (schema)Zod input schema defining required 'post_id' string and optional 'sort' enum (best, top, new) with defaults and descriptions.export const SimplePostCommentsSchema = z.object({ post_id: z.string().describe("Reddit post ID to get comments for"), sort: z.enum(["best", "top", "new"]).default("best").describe("Sort order (default: best)") });
- src/index.ts:603-653 (registration)MCP server.tool registration for 'get_post_comments' including name, detailed description, input schema reference, and wrapped handler function.server.tool( "get_post_comments", "💬 Get comments for a Reddit post\n" + "🎯 What it does: Fetches comments and replies for any Reddit post\n" + "📝 Required: post_id (Reddit post ID, found in post URLs)\n" + "⚙️ Optional: sort ('best', 'top', 'new')\n" + "💡 Examples:\n" + " • Get comments: {\"post_id\": \"1n1nlse\"}\n" + " • Best comments: {\"post_id\": \"1n1nlse\", \"sort\": \"best\"}\n" + " • New comments: {\"post_id\": \"1n1nlse\", \"sort\": \"new\"}\n" + "🔍 Output: Formatted comment tree with author, score, timestamp, and nested replies", SimplePostCommentsSchema.shape, createToolHandler(async (params: z.infer<typeof SimplePostCommentsSchema>) => { const { post_id, sort } = params; // 🧠 Smart defaults for missing parameters const smartDefaults = getSmartDefaults(params, 'comments'); const finalParams = { ...smartDefaults, post_id, sort: sort || smartDefaults.sort }; const result = await redditAPI.getPostComments(post_id, finalParams.limit, finalParams.sort); if (!result.success) { return createErrorResponse("Error getting post comments", result.error); } const data = result.data; if (!data || !Array.isArray(data) || data.length === 0) { return createErrorResponse("No comments found for this post"); } // The first element contains the post, the second contains comments const commentsData = data[1]; if (!commentsData || !commentsData.data || !commentsData.data.children) { return createErrorResponse("No comments found for this post"); } const comments = commentsData.data.children.map((child: any) => child.data); if (comments.length === 0) { return createSuccessResponse("No comments found for this post"); } const summary = `💬 Found ${comments.length} comments for post ${post_id} (sorted by ${sort})`; // ✅ DRY: Sử dụng formatDataList helper const commentDetails = formatDataList(comments, formatRedditComment, COMMENT_PREVIEW_LIMIT, "comments"); const resultText = `${summary}\n\n${commentDetails}`; return createSuccessResponse(resultText); }) );
- src/services/reddit-api.ts:395-406 (helper)Supporting method in RedditAPIService: constructs and executes authenticated GET request to Reddit API endpoint `/comments/{postId}.json` with limit/sort params, handles OAuth token, rate limits, errors, returns structured ApiCallResult.async getPostComments( postId: string, limit: number = 25, sort: "best" | "top" | "new" | "controversial" | "old" | "qa" = "best", ): Promise<ApiCallResult> { const params: Record<string, any> = { limit, sort }; return this.makeRequest<Array<{ data: { children: Array<{ data: RedditComment }> } }>>( `/comments/${postId}.json`, params, ); }