get_user_posts
Retrieve a Reddit user's post history to analyze their contributions, with options to sort by new, top, or hot posts and control the number of results returned.
Instructions
Obtenir l'historique des posts 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 posts à récupérer |
Implementation Reference
- src/tools/user-tools.ts:56-118 (handler)The main handler function for the 'get_user_posts' MCP tool. It fetches the user's posts using the Reddit client, formats them into a structured Markdown response with details like title, subreddit, score, comments, date, and link.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)The input schema and metadata for the 'get_user_posts' tool, defined in the listTools response.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)The dispatch handler in the CallToolRequestSchema that routes 'get_user_posts' calls to the tools.getUserPosts 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)The RedditClient method that fetches raw user posts from the Reddit API, called by the main handler.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}`); }