Skip to main content
Glama
karanb192

Reddit Buddy MCP

by karanb192

user_analysis

Analyze Reddit user activity patterns, posting history, karma statistics, and top subreddits to understand behavior and engagement trends.

Instructions

Analyze a Reddit user's posting history, karma, and activity patterns. Returns posts, comments, and statistics.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
comments_limitNoNumber of recent comments to include (0-100, default: 10)
posts_limitNoNumber of recent posts to include (0-100, default: 10)
time_rangeNoPeriod to analyze: "day", "week", "month", "year", "all". Note: "all" returns newest content, others return top-scored content from that period
top_subreddits_limitNoNumber of most-active subreddits to list (1-50, default: 10)
usernameYesReddit username without u/ prefix (e.g., "spez", not "u/spez")

Implementation Reference

  • The core handler function implementing the user_analysis tool logic. Fetches user profile, posts, and comments from Reddit API, handles time-based filtering with fallbacks, and processes the data into a summary using ContentProcessor.processUserSummary.
    async userAnalysis(params: z.infer<typeof userAnalysisSchema>) {
      // Try to get posts within the specified time range first
      // If time_range is 'all', use 'new' sort; otherwise use 'top' to respect time filtering
      const sort = params.time_range === 'all' ? 'new' : 'top';
      
      let posts = null;
      let comments = null;
      let usedFallback = false;
      
      const user = await this.api.getUser(params.username);
      
      // Fetch posts
      if (params.posts_limit > 0) {
        posts = await this.api.getUserPosts(params.username, 'submitted', {
          limit: params.posts_limit,
          sort,
          time: params.time_range,
        });
        
        // If no results with time filter, fallback to getting recent posts
        if (posts.data.children.length === 0 && params.time_range !== 'all') {
          usedFallback = true;
          posts = await this.api.getUserPosts(params.username, 'submitted', {
            limit: params.posts_limit,
            sort: 'new',
            time: 'all',
          });
        }
      }
      
      // Fetch comments
      if (params.comments_limit > 0) {
        comments = await this.api.getUserPosts(params.username, 'comments', {
          limit: params.comments_limit,
          sort,
          time: params.time_range,
        });
        
        // If no results with time filter, fallback to getting recent comments
        if (comments.data.children.length === 0 && params.time_range !== 'all') {
          usedFallback = true;
          comments = await this.api.getUserPosts(params.username, 'comments', {
            limit: params.comments_limit,
            sort: 'new',
            time: 'all',
          });
        }
      }
    
      // Process summary
      let summary: any;
      if (posts) {
        summary = ContentProcessor.processUserSummary(user, posts, {
          maxTopSubreddits: params.top_subreddits_limit,
          comments: comments || undefined
        });
      } else {
        // Fallback when no posts - but still include comments if available
        summary = {
          username: user.name,
          accountAge: 'Unknown',
          karma: {
            link: user.link_karma,
            comment: user.comment_karma,
            total: user.link_karma + user.comment_karma,
          },
        };
    
        // Add comments even when there are no posts
        if (comments && comments.data.children.length > 0) {
          summary.recentComments = comments.data.children.map(child => {
            const comment = child.data as any;
            return {
              id: comment.id,
              body: comment.body?.substring(0, 200) + (comment.body?.length > 200 ? '...' : ''),
              score: comment.score,
              subreddit: comment.subreddit || 'unknown',
              postTitle: comment.link_title,
              created: new Date(comment.created_utc * 1000),
              url: `https://reddit.com${comment.permalink}`,
            };
          });
        }
      }
    
      // Add note if we used fallback data
      if (usedFallback && params.time_range !== 'all') {
        const timeRangeLabel = {
          'hour': 'hour',
          'day': 'day',
          'week': 'week', 
          'month': 'month',
          'year': 'year'
        }[params.time_range];
        
        summary.timeRangeNote = `No posts found in the last ${timeRangeLabel}, showing all recent posts instead`;
      }
    
      return summary;
    }
  • Zod schema defining the input parameters for the user_analysis tool, including username and optional limits for posts, comments, time range, and top subreddits.
    export const userAnalysisSchema = z.object({
      username: z.string().describe('Reddit username'),
      posts_limit: z.number().min(0).max(100).optional().default(10).describe('Default 10, range (0-100). Change ONLY IF user specifies.'),
      comments_limit: z.number().min(0).max(100).optional().default(10).describe('Default 10, range (0-100). Override ONLY IF user asks.'),
      time_range: z.enum(['day', 'week', 'month', 'year', 'all']).optional().default('month').describe('Time range for posts/comments (default: month). Note: When set to values other than "all", posts are sorted by top scores within that period. When set to "all", posts are sorted by newest'),
      top_subreddits_limit: z.number().min(1).max(50).optional().default(10).describe('Default 10, range (1-50). Change ONLY IF user requests.'),
    });
  • Registration of the user_analysis tool in the MCP server's toolDefinitions array, specifying name, description, input schema, and read-only hint.
      name: 'user_analysis',
      description: 'Analyze a Reddit user\'s posting history, karma, and activity patterns. Returns posts, comments, and statistics.',
      inputSchema: zodToJsonSchema(userAnalysisSchema) as any,
      readOnlyHint: true
    },
  • Dispatch logic in the MCP tool call handler that invokes the userAnalysis method with parsed arguments when 'user_analysis' is called.
    case 'user_analysis':
      result = await tools.userAnalysis(
        userAnalysisSchema.parse(args)
      );
      break;

Latest Blog Posts

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/karanb192/reddit-buddy-mcp'

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