Skip to main content
Glama

Reddit MCP Server

search-tools.ts9 kB
import { getRedditClient } from "../client/reddit-client"; import { formatPostInfo, formatCommentInfo, formatSubredditInfo } from "../utils/formatters"; import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js"; export async function getComment(params: { comment_id: string }) { const { comment_id } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting comment ${comment_id}`); const comment = await client.getComment(comment_id); const formattedComment = formatCommentInfo(comment); return { content: [ { type: "text", text: ` # Commentaire Reddit ## Détails du commentaire - Auteur: u/${formattedComment.author} - Score: ${formattedComment.stats.score} - Controverse: ${formattedComment.stats.controversiality} ## Contenu ${formattedComment.content} ## Contexte - Subreddit: r/${formattedComment.context.subreddit} - Thread: ${formattedComment.context.thread} ## Métadonnées - Posté: ${formattedComment.metadata.posted} - Drapeaux: ${formattedComment.metadata.flags.length ? formattedComment.metadata.flags.join(", ") : "Aucun"} ## Lien ${formattedComment.link} ## Analyse du commentaire ${formattedComment.commentAnalysis} `, }, ], }; } catch (error) { console.error(`[Error] Error getting comment: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch comment: ${error}` ); } } export async function getCommentsBySubmission(params: { submission_id: string; limit?: number; }) { const { submission_id, limit = 20 } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting comments for submission ${submission_id}`); const comments = await client.getCommentsBySubmission(submission_id, limit); const formattedComments = comments.map(formatCommentInfo); const commentSummaries = formattedComments .map( (comment, index) => ` ### ${index + 1}. u/${comment.author} - Score: ${comment.stats.score} - Posté: ${comment.metadata.posted} - Contenu: ${comment.content.substring(0, 200)}${comment.content.length > 200 ? "..." : ""} ` ) .join("\n"); return { content: [ { type: "text", text: ` # Commentaires pour la soumission ${submission_id} ${commentSummaries} `, }, ], }; } catch (error) { console.error(`[Error] Error getting comments by submission: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch comments: ${error}` ); } } export async function getSubmission(params: { submission_id: string }) { const { submission_id } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting submission ${submission_id}`); const submission = await client.getSubmission(submission_id); const formattedSubmission = formatPostInfo(submission); return { content: [ { type: "text", text: ` # Soumission Reddit ## Détails de la soumission - Titre: ${formattedSubmission.title} - Type: ${formattedSubmission.type} - Auteur: u/${formattedSubmission.author} - Subreddit: r/${formattedSubmission.subreddit} ## Contenu ${formattedSubmission.content} ## Statistiques - Score: ${formattedSubmission.stats.score.toLocaleString()} - Ratio de votes positifs: ${(formattedSubmission.stats.upvoteRatio * 100).toFixed(1)}% - Commentaires: ${formattedSubmission.stats.comments.toLocaleString()} ## Métadonnées - Posté: ${formattedSubmission.metadata.posted} - Drapeaux: ${formattedSubmission.metadata.flags.length ? formattedSubmission.metadata.flags.join(", ") : "Aucun"} - Flair: ${formattedSubmission.metadata.flair} ## Liens - Post complet: ${formattedSubmission.links.fullPost} - Lien court: ${formattedSubmission.links.shortLink} ## Analyse d'engagement ${formattedSubmission.engagementAnalysis} ## Meilleur moment pour s'engager ${formattedSubmission.bestTimeToEngage} `, }, ], }; } catch (error) { console.error(`[Error] Error getting submission: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch submission: ${error}` ); } } export async function getSubreddit(params: { subreddit_name: string }) { const { subreddit_name } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting subreddit r/${subreddit_name}`); const subreddit = await client.getSubreddit(subreddit_name); const formattedSubreddit = formatSubredditInfo(subreddit); return { content: [ { type: "text", text: ` # r/${formattedSubreddit.name} ## Informations générales - Titre: ${formattedSubreddit.title} - Abonnés: ${formattedSubreddit.stats.subscribers.toLocaleString()} - Utilisateurs actifs: ${formattedSubreddit.stats.activeUsers} ## Description ### Description courte ${formattedSubreddit.description.short} ### Description complète ${formattedSubreddit.description.full} ## Métadonnées - Créé: ${formattedSubreddit.metadata.created} - Drapeaux: ${formattedSubreddit.metadata.flags.length ? formattedSubreddit.metadata.flags.join(", ") : "Aucun"} ## Liens - Subreddit: ${formattedSubreddit.links.subreddit} - Wiki: ${formattedSubreddit.links.wiki} ## Analyse de la communauté ${formattedSubreddit.communityAnalysis} ## Conseils d'engagement ${formattedSubreddit.engagementTips} `, }, ], }; } catch (error) { console.error(`[Error] Error getting subreddit: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch subreddit: ${error}` ); } } 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}` ); } } 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}` ); } }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/samy-clivolt/reddit-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server