search_subreddits
Find Reddit communities by name or description using targeted search queries to discover relevant subreddits for specific topics or interests.
Instructions
Rechercher des subreddits par nom ou description
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | La requête de recherche | |
| limit | No | Nombre de résultats à retourner |
Implementation Reference
- src/tools/search-tools.ts:297-350 (handler)The main handler function for the 'search_subreddits' tool. It destructures query and optional limit from params, retrieves the Reddit client, searches for subreddits, formats them using formatSubredditInfo, generates markdown summaries, and returns a structured text content response.export async function searchSubreddits(params: { query: string; limit?: number; }) { const { query, limit = 25 } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Searching subreddits for "${query}"`); const subreddits = await client.searchSubreddits(query, limit); const formattedSubreddits = subreddits.map(formatSubredditInfo); const subredditSummaries = formattedSubreddits .map( (subreddit, index) => ` ### ${index + 1}. r/${subreddit.name} - Titre: ${subreddit.title} - Abonnés: ${subreddit.stats.subscribers.toLocaleString()} - Description: ${subreddit.description.short.substring(0, 150)}${subreddit.description.short.length > 150 ? "..." : ""} - Lien: ${subreddit.links.subreddit} ` ) .join("\n"); return { content: [ { type: "text", text: ` # Résultats de recherche de subreddits **Requête:** "${query}" **Résultats trouvés:** ${subreddits.length} ${subredditSummaries} `, }, ], }; } catch (error) { console.error(`[Error] Error searching subreddits: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to search subreddits: ${error}` ); } }
- src/index.ts:320-338 (schema)The input schema and metadata for the 'search_subreddits' tool as registered in the ListTools response, defining the expected parameters: query (string, required) and limit (integer, optional default 25).{ name: "search_subreddits", description: "Rechercher des subreddits par nom ou description", inputSchema: { type: "object", properties: { query: { type: "string", description: "La requête de recherche", }, limit: { type: "integer", description: "Nombre de résultats à retourner", default: 25, }, }, required: ["query"], }, },
- src/index.ts:498-501 (registration)The dispatch registration in the CallToolRequest handler's switch statement that routes calls to 'search_subreddits' to the tools.searchSubreddits function with typed parameters.case "search_subreddits": return await tools.searchSubreddits( toolParams as { query: string; limit?: number } );
- src/client/reddit-client.ts:522-552 (helper)Supporting helper method in RedditClient class that authenticates, makes API call to Reddit's /subreddits/search.json endpoint, parses the response, and returns an array of formatted RedditSubreddit objects. Called by the tool handler.async searchSubreddits(query: string, limit: number = 25): Promise<RedditSubreddit[]> { await this.authenticate(); try { const response = await this.api.get("/subreddits/search.json", { params: { q: query, limit, type: "sr", }, }); return response.data.data.children.map((child: any) => { const subreddit = child.data; return { displayName: subreddit.display_name, title: subreddit.title, description: subreddit.description || "", publicDescription: subreddit.public_description || "", subscribers: subreddit.subscribers, activeUserCount: subreddit.active_user_count, createdUtc: subreddit.created_utc, over18: subreddit.over18, subredditType: subreddit.subreddit_type, url: subreddit.url, }; }); } catch (error) { console.error(`[Error] Failed to search subreddits:`, error); throw new Error(`Failed to search subreddits: ${error}`); } }