search_posts
Search for posts in specific Reddit communities using keywords and filters to find relevant discussions and content.
Instructions
Rechercher des posts dans un subreddit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Nombre de résultats à retourner | |
| query | Yes | La requête de recherche | |
| sort | No | Méthode de tri (relevance, new, hot, top) | relevance |
| subreddit | Yes | Le nom du subreddit dans lequel rechercher |
Implementation Reference
- src/tools/search-tools.ts:238-295 (handler)The primary handler function for the 'search_posts' tool. It receives parameters, fetches posts using the Reddit client, formats them, and returns structured MCP 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-319 (schema)Defines the tool schema including name, description, input parameters, and validation for the search_posts tool in the ListTools response.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)Dispatches the tool call to the searchPosts handler function in the MCP server's CallToolRequestHandler.case "search_posts": return await tools.searchPosts( toolParams as { subreddit: string; query: string; sort?: string; limit?: number } );
- src/client/reddit-client.ts:482-519 (helper)The RedditClient method that performs the actual API search request to Reddit and returns raw post data, called 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}`); }