search_posts
Search for Reddit posts using keywords, filter by subreddit, sort by relevance or time, and retrieve specific results to find relevant discussions and content.
Instructions
Search for Reddit posts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of results to retrieve (1-100) | |
| query | Yes | Search query | |
| sort | No | Sort order for search results | relevance |
| subreddit | No | Optional: restrict search to specific subreddit | |
| time | No | Time period to filter results (works best with sort=top) |
Implementation Reference
- src/index.ts:387-398 (handler)MCP tool handler for 'search_posts': parses arguments using SearchPostsSchema and calls redditClient.searchPosts to fetch and return search results as JSON.case 'search_posts': { const args = SearchPostsSchema.parse(request.params.arguments); const posts = await redditClient.searchPosts(args.query, args.subreddit, args.sort, args.time, args.limit); return { content: [ { type: 'text', text: JSON.stringify(posts, null, 2), }, ], }; }
- src/index.ts:73-79 (schema)Zod schema defining input validation for the search_posts tool parameters.const SearchPostsSchema = z.object({ query: z.string().min(1, "Search query is required"), subreddit: z.string().optional(), sort: z.enum(['relevance', 'hot', 'top', 'new', 'comments']).default('relevance'), time: z.enum(['hour', 'day', 'week', 'month', 'year', 'all']).optional(), limit: z.number().min(1).max(100).default(25), });
- src/index.ts:232-267 (registration)Tool registration in ListTools response, including name, description, and JSON inputSchema for 'search_posts'.{ name: 'search_posts', description: 'Search for Reddit posts', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', }, subreddit: { type: 'string', description: 'Optional: restrict search to specific subreddit', }, sort: { type: 'string', enum: ['relevance', 'hot', 'top', 'new', 'comments'], description: 'Sort order for search results', default: 'relevance', }, time: { type: 'string', enum: ['hour', 'day', 'week', 'month', 'year', 'all'], description: 'Time period to filter results (works best with sort=top)', }, limit: { type: 'number', description: 'Number of results to retrieve (1-100)', minimum: 1, maximum: 100, default: 25, }, }, required: ['query'], }, },
- src/reddit-client.ts:170-182 (helper)Core implementation of post search using Reddit API search endpoint, mapping results to RedditPost interface.async searchPosts(query: string, subreddit?: string, sort: 'relevance' | 'hot' | 'top' | 'new' | 'comments' = 'relevance', time?: 'hour' | 'day' | 'week' | 'month' | 'year' | 'all', limit: number = 25): Promise<RedditPost[]> { const searchPath = subreddit ? `/r/${subreddit}/search` : '/search'; const params = new URLSearchParams({ q: query, sort, limit: limit.toString(), type: 'link', ...(subreddit && { restrict_sr: 'true' }), ...(time && { t: time }) }); const data = await this.makeRequest(`${searchPath}?${params}`); return data.data.children.map((child: any) => this.mapPost(child.data));