get_user_comments
Retrieve a user's Reddit comment history with options to sort by new, top, or hot posts and specify the number of comments to fetch.
Instructions
Obtenir l'historique des commentaires d'un utilisateur
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Nombre de commentaires à récupérer | |
| sort | No | Méthode de tri (new, top, hot) | new |
| username | Yes | Le nom d'utilisateur Reddit |
Implementation Reference
- src/tools/user-tools.ts:120-181 (handler)Main MCP tool handler function that fetches user comments using the Reddit client and formats the response as structured text content for the MCP protocol.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 (registration)Tool registration in the MCP server's ListTools response, including the tool name, description, and input schema definition.{ 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 (handler)Dispatch handler in the MCP server's CallToolRequestHandler switch statement that invokes the specific tool implementation.case "get_user_comments": return await tools.getUserComments( toolParams as { username: string; sort?: string; limit?: number } );
- src/client/reddit-client.ts:592-622 (helper)Low-level helper in RedditClient that performs the actual API call to fetch user comments from Reddit.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}`); } }