get_user_posts
Retrieve a Reddit user's post history with sorting options to analyze their content contributions and activity patterns on the platform.
Instructions
Obtenir l'historique des posts d'un utilisateur
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Nombre de posts à 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:56-118 (handler)Main handler function for the 'get_user_posts' tool. Extracts parameters, fetches posts using Reddit client, formats them into a markdown response, and handles errors.export async function getUserPosts(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 posts for u/${username}`); const posts = await client.getUserPosts(username, sort, limit); if (posts.length === 0) { return { content: [ { type: "text", text: `# Posts de u/${username}\n\nAucun post trouvé pour cet utilisateur.`, }, ], }; } const postsText = posts.map((post, index) => { const postedDate = new Date(post.createdUtc * 1000).toLocaleString('fr-FR'); const scoreText = post.score >= 0 ? `+${post.score}` : `${post.score}`; return ` ### ${index + 1}. ${post.title} - **Subreddit**: r/${post.subreddit} - **Score**: ${scoreText} (${Math.round(post.upvoteRatio * 100)}% positifs) - **Commentaires**: ${post.numComments} - **Type**: ${post.isSelf ? 'Post texte' : 'Lien'} - **Posté**: ${postedDate} ${post.linkFlairText ? `- **Flair**: ${post.linkFlairText}` : ''} - **Lien**: https://reddit.com${post.permalink} ${post.selftext && post.selftext.length > 0 ? `\n**Contenu**: ${post.selftext.substring(0, 200)}${post.selftext.length > 200 ? '...' : ''}` : ''} `; }).join('\n'); return { content: [ { type: "text", text: `# Posts récents de u/${username} **Tri**: ${sort} **Nombre de posts**: ${posts.length} ${postsText}`, }, ], }; } catch (error) { console.error(`[Error] Error getting user posts: ${error}`); throw new McpError( ErrorCode.InternalError, `Failed to fetch user posts: ${error}` ); } }
- src/index.ts:340-362 (schema)Input schema definition for the get_user_posts tool, including parameters, types, descriptions, defaults, and required fields.name: "get_user_posts", description: "Obtenir l'historique des posts 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 posts à récupérer", default: 25, }, }, required: ["username"], },
- src/index.ts:503-506 (registration)Registration in the MCP server request handler: switch case that dispatches CallToolRequest to the getUserPosts tool function.case "get_user_posts": return await tools.getUserPosts( toolParams as { username: string; sort?: string; limit?: number } );
- src/client/reddit-client.ts:555-589 (helper)Helper method in RedditClient class that makes the actual API call to fetch user's submitted posts from Reddit.async getUserPosts(username: string, sort: string = "new", limit: number = 25): Promise<RedditPost[]> { await this.authenticate(); try { const response = await this.api.get(`/user/${username}/submitted.json`, { params: { sort, limit, }, }); return response.data.data.children.map((child: any) => { const post = child.data; return { id: post.id, title: post.title, author: post.author, subreddit: post.subreddit, selftext: post.selftext, url: post.url, score: post.score, upvoteRatio: post.upvote_ratio, numComments: post.num_comments, createdUtc: post.created_utc, over18: post.over_18, spoiler: post.spoiler, edited: !!post.edited, isSelf: post.is_self, linkFlairText: post.link_flair_text, permalink: post.permalink, }; }); } catch (error) { console.error(`[Error] Failed to get user posts for ${username}:`, error); throw new Error(`Failed to get user posts for ${username}`); }