search_subreddits
Find relevant Reddit communities by searching subreddit names and descriptions to discover active discussion groups on specific topics.
Instructions
Rechercher des subreddits par nom ou description
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Nombre de résultats à retourner | |
| query | Yes | La requête de recherche |
Implementation Reference
- src/tools/search-tools.ts:297-349 (handler)The primary handler function for the 'search_subreddits' tool. It takes query and optional limit parameters, fetches subreddits using the Reddit client, formats the results, and returns a structured text response with summaries.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:321-338 (schema)The input schema and metadata definition for the 'search_subreddits' tool, registered in the ListTools response, specifying required 'query' parameter and optional 'limit'.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 switch case registration that maps incoming 'search_subreddits' tool calls to the implementation in tools.searchSubreddits.case "search_subreddits": return await tools.searchSubreddits( toolParams as { query: string; limit?: number } );
- src/client/reddit-client.ts:522-552 (helper)The underlying RedditClient method that performs the actual API call to search subreddits and returns raw RedditSubreddit data, used 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}`); } }