get_top_posts
Retrieve top-performing posts from any subreddit by specifying time period and quantity for content analysis or trend monitoring.
Instructions
Get top posts from a subreddit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| subreddit | Yes | Name of the subreddit | |
| time_filter | No | Time period to filter posts (e.g. 'day', 'week', 'month', 'year', 'all') | week |
| limit | No | Number of posts to fetch |
Implementation Reference
- src/tools/post-tools.ts:75-129 (handler)Main handler function implementing the get_top_posts tool logic: fetches top posts via RedditClient, formats them with formatPostInfo, generates summaries, and returns structured markdown content.
export async function getTopPosts(params: { subreddit: string; time_filter?: string; limit?: number; }) { const { subreddit, time_filter = "week", limit = 10 } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting top posts from r/${subreddit}`); const posts = await client.getTopPosts(subreddit, time_filter, limit); const formattedPosts = posts.map(formatPostInfo); const postSummaries = formattedPosts .map( (post, index) => ` ### ${index + 1}. ${post.title} - Author: u/${post.author} - Score: ${post.stats.score.toLocaleString()} (${( post.stats.upvoteRatio * 100 ).toFixed(1)}% upvoted) - Comments: ${post.stats.comments.toLocaleString()} - Posted: ${post.metadata.posted} - Link: ${post.links.shortLink} ` ) .join("\n"); return { content: [ { type: "text", text: ` # Top Posts from r/${subreddit} (${time_filter}) ${postSummaries} `, }, ], }; } catch (error) { console.error(`[Error] Error getting top posts: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch top posts: ${error}` ); } } - src/index.ts:118-141 (schema)Input schema definition for the get_top_posts tool, specifying required 'subreddit' parameter and optional 'time_filter' (enum) and 'limit'.
name: "get_top_posts", description: "Get top posts from a subreddit", inputSchema: { type: "object", properties: { subreddit: { type: "string", description: "Name of the subreddit", }, time_filter: { type: "string", description: "Time period to filter posts (e.g. 'day', 'week', 'month', 'year', 'all')", enum: ["day", "week", "month", "year", "all"], default: "week", }, limit: { type: "integer", description: "Number of posts to fetch", default: 10, }, }, required: ["subreddit"], }, - src/index.ts:434-441 (registration)Registration of the get_top_posts handler in the MCP server's CallToolRequestSchema switch statement, dispatching to tools.getTopPosts.
case "get_top_posts": return await tools.getTopPosts( toolParams as { subreddit: string; time_filter?: string; limit?: number; } ); - src/client/reddit-client.ts:164-207 (helper)Supporting helper method in RedditClient class that makes the actual API call to fetch top posts from Reddit's OAuth API and parses the response into RedditPost objects.
async getTopPosts( subreddit: string, timeFilter: string = "week", limit: number = 10 ): Promise<RedditPost[]> { await this.authenticate(); try { const endpoint = subreddit ? `/r/${subreddit}/top.json` : "/top.json"; const response = await this.api.get(endpoint, { params: { t: timeFilter, limit, }, }); return response.data.data.children.map((child: any) => { const post = child.data; return { id: post.id, title: post.title, author: post.author, subreddit: post.subreddit, selftext: post.selftext, url: post.url, score: post.score, upvoteRatio: post.upvote_ratio, numComments: post.num_comments, createdUtc: post.created_utc, over18: post.over_18, spoiler: post.spoiler, edited: !!post.edited, isSelf: post.is_self, linkFlairText: post.link_flair_text, permalink: post.permalink, }; }); } catch (error) { console.error( `[Error] Failed to get top posts for ${subreddit || "home"}:`, error ); throw new Error(`Failed to get top posts for ${subreddit || "home"}`); } }