get_user_comments
Retrieve a Reddit user's comment history to analyze their activity and contributions across the platform.
Instructions
Obtenir l'historique des commentaires d'un utilisateur
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| username | Yes | Le nom d'utilisateur Reddit | |
| sort | No | Méthode de tri (new, top, hot) | new |
| limit | No | Nombre de commentaires à récupérer |
Implementation Reference
- src/tools/user-tools.ts:120-181 (handler)Main tool handler: fetches user comments from Reddit client, formats them into a structured text response with details like post, score, date, link, and truncated content.export async function getUserComments(params: { username: string; sort?: string; limit?: number }) { const { username, sort = "new", limit = 25 } = params; const client = getRedditClient(); if (!client) { throw new McpError( ErrorCode.InternalError, "Reddit client not initialized" ); } try { console.log(`[Tool] Getting comments for u/${username}`); const comments = await client.getUserComments(username, sort, limit); if (comments.length === 0) { return { content: [ { type: "text", text: `# Commentaires de u/${username}\n\nAucun commentaire trouvé pour cet utilisateur.`, }, ], }; } const commentsText = comments.map((comment, index) => { const postedDate = new Date(comment.createdUtc * 1000).toLocaleString('fr-FR'); const scoreText = comment.score >= 0 ? `+${comment.score}` : `${comment.score}`; const controversialText = comment.controversiality > 0 ? ' (Controversé)' : ''; return ` ### ${index + 1}. Commentaire dans r/${comment.subreddit} - **Post**: ${comment.submissionTitle} - **Score**: ${scoreText}${controversialText} - **Posté**: ${postedDate} - **Lien**: ${comment.permalink} **Contenu**: ${comment.body.substring(0, 300)}${comment.body.length > 300 ? '...' : ''} `; }).join('\n'); return { content: [ { type: "text", text: `# Commentaires récents de u/${username} **Tri**: ${sort} **Nombre de commentaires**: ${comments.length} ${commentsText}`, }, ], }; } catch (error) { console.error(`[Error] Error getting user comments: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch user comments: ${error}` ); } }
- src/index.ts:364-388 (schema)Input schema definition for the get_user_comments tool, specifying parameters: username (required), sort (enum: new/top/hot, default new), limit (default 25).{ name: "get_user_comments", description: "Obtenir l'historique des commentaires d'un utilisateur", inputSchema: { type: "object", properties: { username: { type: "string", description: "Le nom d'utilisateur Reddit", }, sort: { type: "string", description: "Méthode de tri (new, top, hot)", enum: ["new", "top", "hot"], default: "new", }, limit: { type: "integer", description: "Nombre de commentaires à récupérer", default: 25, }, }, required: ["username"], }, },
- src/index.ts:508-511 (registration)Tool call dispatching: handles CallToolRequest for get_user_comments by invoking tools.getUserComments with typed parameters.case "get_user_comments": return await tools.getUserComments( toolParams as { username: string; sort?: string; limit?: number } );
- src/client/reddit-client.ts:592-622 (helper)RedditClient helper method: authenticates and fetches user's comments via Reddit API endpoint /user/{username}/comments.json, maps response to RedditComment objects.async getUserComments(username: string, sort: string = "new", limit: number = 25): Promise<RedditComment[]> { await this.authenticate(); try { const response = await this.api.get(`/user/${username}/comments.json`, { params: { sort, limit, }, }); return response.data.data.children.map((child: any) => { const comment = child.data; return { id: comment.id, author: comment.author, body: comment.body, score: comment.score, controversiality: comment.controversiality, subreddit: comment.subreddit, submissionTitle: comment.link_title || "Unknown Post", createdUtc: comment.created_utc, edited: !!comment.edited, isSubmitter: comment.is_submitter, permalink: `https://reddit.com${comment.permalink}`, }; }); } catch (error) { console.error(`[Error] Failed to get user comments for ${username}:`, error); throw new Error(`Failed to get user comments for ${username}`); } }