get_post_details
Fetch Reddit posts with comments using URL or post ID, including options for comment limits, sorting, and link extraction to analyze discussions.
Instructions
Fetch a Reddit post with its comments. Requires EITHER url OR post_id. IMPORTANT: When using post_id alone, an extra API call is made to fetch the subreddit first (2 calls total). For better efficiency, always provide the subreddit parameter when known (1 call total).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| comment_depth | No | Levels of nested replies to include (1-10, default: 3) | |
| comment_limit | No | Maximum comments to fetch (1-500, default: 20) | |
| comment_sort | No | Comment ordering: "best" (algorithm-ranked), "top" (highest score), "new", "controversial", "qa" (Q&A style). Default: best | |
| extract_links | No | Extract all URLs mentioned in post and comments (default: false) | |
| max_top_comments | No | Number of top comments to return (1-50, default: 5) | |
| post_id | No | Reddit post ID (e.g., "abc123"). Can be used alone or with subreddit for better performance | |
| subreddit | No | Optional subreddit name when using post_id. Providing it avoids an extra API call (e.g., "science") | |
| url | No | Full Reddit post URL (e.g., "https://reddit.com/r/science/comments/abc123/...") |
Input Schema (JSON Schema)
{
"properties": {
"comment_depth": {
"description": "Levels of nested replies to include (1-10, default: 3)",
"type": "number"
},
"comment_limit": {
"description": "Maximum comments to fetch (1-500, default: 20)",
"type": "number"
},
"comment_sort": {
"description": "Comment ordering: \"best\" (algorithm-ranked), \"top\" (highest score), \"new\", \"controversial\", \"qa\" (Q&A style). Default: best",
"enum": [
"best",
"top",
"new",
"controversial",
"qa"
],
"type": "string"
},
"extract_links": {
"description": "Extract all URLs mentioned in post and comments (default: false)",
"type": "boolean"
},
"max_top_comments": {
"description": "Number of top comments to return (1-50, default: 5)",
"type": "number"
},
"post_id": {
"description": "Reddit post ID (e.g., \"abc123\"). Can be used alone or with subreddit for better performance",
"type": "string"
},
"subreddit": {
"description": "Optional subreddit name when using post_id. Providing it avoids an extra API call (e.g., \"science\")",
"type": "string"
},
"url": {
"description": "Full Reddit post URL (e.g., \"https://reddit.com/r/science/comments/abc123/...\")",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/tools/index.ts:208-283 (handler)The main handler function in RedditTools class that implements get_post_details tool logic. Extracts post identifier from URL or parameters, fetches post and comments via RedditAPI, cleans and structures the data, processes top comments, optionally extracts links from comments, and returns formatted result.async getPostDetails(params: z.infer<typeof getPostDetailsSchema>) { let postIdentifier: string; if (params.url) { // Extract from URL - returns format "subreddit_postid" postIdentifier = this.extractPostIdFromUrl(params.url); } else if (params.post_id) { // If subreddit provided, use it; otherwise getPost will fetch it postIdentifier = params.subreddit ? `${params.subreddit}_${params.post_id}` : params.post_id; } else { throw new Error('Provide either url OR post_id'); } const [postListing, commentsListing] = await this.api.getPost(postIdentifier, { limit: params.comment_limit, sort: params.comment_sort, depth: params.comment_depth, }); const post = postListing.data.children[0].data; // Extract essential post fields const cleanPost = { id: post.id, title: post.title, author: post.author, score: post.score, upvote_ratio: post.upvote_ratio, num_comments: post.num_comments, created_utc: post.created_utc, url: post.url, permalink: `https://reddit.com${post.permalink}`, subreddit: post.subreddit, is_video: post.is_video, is_text_post: post.is_self, content: post.selftext?.substring(0, 1000), // More text for post details nsfw: post.over_18, link_flair_text: post.link_flair_text, stickied: post.stickied, locked: post.locked, }; // Process comments const comments = commentsListing.data.children .filter(child => child.kind === 't1') // Only comments .map(child => child.data); let result: any = { post: cleanPost, total_comments: comments.length, top_comments: comments.slice(0, params.max_top_comments || 5).map(c => ({ id: c.id, author: c.author, score: c.score, body: (c.body || '').substring(0, 500), created_utc: c.created_utc, depth: c.depth, is_op: c.is_submitter, permalink: `https://reddit.com${c.permalink}`, })), }; // Extract links if requested if (params.extract_links) { const links = new Set<string>(); comments.forEach(c => { const urls = (c.body || '').match(/https?:\/\/[^\s]+/g) || []; urls.forEach(url => links.add(url)); }); result.extracted_links = Array.from(links); } return result; }
- src/tools/index.ts:30-39 (schema)Zod schema defining input validation for get_post_details tool, specifying optional post_id, subreddit, url, and various comment fetching parameters with defaults and descriptions.export const getPostDetailsSchema = z.object({ post_id: z.string().optional().describe('Reddit post ID (e.g., "1abc2d3")'), subreddit: z.string().optional().describe('Subreddit name (optional with post_id, but more efficient if provided)'), url: z.string().optional().describe('Full Reddit URL (alternative to post_id)'), comment_limit: z.number().min(1).max(500).optional().default(20).describe('Default 20, range (1-500). Change ONLY IF user asks.'), comment_sort: z.enum(['best', 'top', 'new', 'controversial', 'qa']).optional().default('best'), comment_depth: z.number().min(1).max(10).optional().default(3).describe('Default 3, range (1-10). Override ONLY IF user specifies.'), extract_links: z.boolean().optional().default(false), max_top_comments: z.number().min(1).max(20).optional().default(5).describe('Default 5, range (1-20). Change ONLY IF user requests.'), });
- src/mcp-server.ts:119-123 (registration)MCP tool registration definition for get_post_details, including name, detailed description, input schema conversion from Zod, and read-only hint. Added to the toolDefinitions array used by the MCP server.name: 'get_post_details', description: 'Fetch a Reddit post with its comments. Requires EITHER url OR post_id. IMPORTANT: When using post_id alone, an extra API call is made to fetch the subreddit first (2 calls total). For better efficiency, always provide the subreddit parameter when known (1 call total).', inputSchema: zodToJsonSchema(getPostDetailsSchema) as any, readOnlyHint: true },
- src/mcp-server.ts:161-165 (registration)Dispatch handler in the tools/call request handler that routes calls to get_post_details by parsing arguments with the schema and invoking the tools.getPostDetails method.case 'get_post_details': result = await tools.getPostDetails( getPostDetailsSchema.parse(args) ); break;
- src/tools/index.ts:476-482 (helper)Private helper method used by getPostDetails to parse a full Reddit post URL and extract the subreddit_postid identifier format required by the RedditAPI.private extractPostIdFromUrl(url: string): string { const match = url.match(/\/r\/(\w+)\/comments\/(\w+)/); if (match) { return `${match[1]}_${match[2]}`; } throw new Error('Invalid Reddit post URL'); }