search_posts
Find Reddit posts by searching with keywords, filtering by subreddit, sorting by relevance or time, and retrieving specific numbers of results.
Instructions
Search for Reddit posts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| subreddit | No | Optional: restrict search to specific subreddit | |
| sort | No | Sort order for search results | relevance |
| time | No | Time period to filter results (works best with sort=top) | |
| limit | No | Number of results to retrieve (1-100) |
Implementation Reference
- src/index.ts:387-398 (handler)MCP tool handler for 'search_posts': parses arguments using SearchPostsSchema, calls redditClient.searchPosts, and returns JSON stringified results.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 for validating 'search_posts' tool input 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, defining name, description, and JSON inputSchema matching the Zod schema.{ 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-183 (helper)Core implementation of post search logic in RedditClient: constructs Reddit search API endpoint with parameters and fetches/maps results.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)); }