search_posts
Find Reddit posts in specific subreddits using search queries, with options to sort by relevance, newness, popularity, or top content and control result quantity.
Instructions
Rechercher des posts dans un subreddit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| subreddit | Yes | Le nom du subreddit dans lequel rechercher | |
| query | Yes | La requête de recherche | |
| sort | No | Méthode de tri (relevance, new, hot, top) | relevance |
| limit | No | Nombre de résultats à retourner |
Implementation Reference
- src/tools/search-tools.ts:238-295 (handler)The main handler function for the 'search_posts' tool. It extracts parameters, retrieves the Reddit client, performs the search via client.searchPosts, formats the results using formatPostInfo, generates markdown summaries, and returns structured content.export async function searchPosts(params: { subreddit: string; query: string; sort?: string; limit?: number; }) { const { subreddit, query, sort = "relevance", limit = 25 } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Searching posts in r/${subreddit} for "${query}"`); const posts = await client.searchPosts(subreddit, query, sort, limit); const formattedPosts = posts.map(formatPostInfo); const postSummaries = formattedPosts .map( (post, index) => ` ### ${index + 1}. ${post.title} - Auteur: u/${post.author} - Score: ${post.stats.score.toLocaleString()} (${(post.stats.upvoteRatio * 100).toFixed(1)}% positifs) - Commentaires: ${post.stats.comments.toLocaleString()} - Posté: ${post.metadata.posted} - Lien: ${post.links.shortLink} ` ) .join("\n"); return { content: [ { type: "text", text: ` # Résultats de recherche dans r/${subreddit} **Requête:** "${query}" **Tri:** ${sort} **Résultats trouvés:** ${posts.length} ${postSummaries} `, }, ], }; } catch (error) { console.error(`[Error] Error searching posts: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to search posts: ${error}` ); } }
- src/index.ts:292-318 (schema)The input schema definition and tool description for 'search_posts' provided to the MCP ListTools request handler.name: "search_posts", description: "Rechercher des posts dans un subreddit", inputSchema: { type: "object", properties: { subreddit: { type: "string", description: "Le nom du subreddit dans lequel rechercher", }, query: { type: "string", description: "La requête de recherche", }, sort: { type: "string", description: "Méthode de tri (relevance, new, hot, top)", enum: ["relevance", "new", "hot", "top"], default: "relevance", }, limit: { type: "integer", description: "Nombre de résultats à retourner", default: 25, }, }, required: ["subreddit", "query"], },
- src/index.ts:493-496 (registration)The switch case in the CallToolRequestHandler that registers and dispatches calls to the searchPosts handler function from tools.case "search_posts": return await tools.searchPosts( toolParams as { subreddit: string; query: string; sort?: string; limit?: number } );
- src/client/reddit-client.ts:482-520 (helper)The RedditClient helper method that performs the actual Reddit API search request and maps the response to RedditPost objects, used by the tool handler.async searchPosts(subreddit: string, query: string, sort: string = "relevance", limit: number = 25): Promise<RedditPost[]> { await this.authenticate(); try { const response = await this.api.get(`/r/${subreddit}/search.json`, { params: { q: query, restrict_sr: true, sort, limit, type: "link" } }); 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 search posts in ${subreddit}:`, error); throw new Error(`Failed to search posts in ${subreddit}`); } }