Skip to main content
Glama
liuyang1520

Reddit MCP Server

by liuyang1520

get_post_comments

Retrieve comments from a Reddit post by providing the post ID. Sort comments by best, top, new, controversial, or old to analyze discussions.

Instructions

Get comments from a Reddit post

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
postIdYesReddit post ID
sortNoSort order for commentsbest

Implementation Reference

  • Core handler function that fetches comments from Reddit API for a given post, extracts them recursively, and returns a flat list of RedditComment objects.
    async getPostComments(postId: string, sort: 'best' | 'top' | 'new' | 'controversial' | 'old' = 'best'): Promise<RedditComment[]> {
      const data = await this.makeRequest(`/comments/${postId}?sort=${sort}`);
      const comments: RedditComment[] = [];
      
      if (data[1] && data[1].data && data[1].data.children) {
        this.extractComments(data[1].data.children, comments);
      }
      
      return comments;
    }
  • MCP CallToolRequest handler case that validates input using Zod schema and delegates execution to RedditClient.getPostComments, formatting response as JSON text.
    case 'get_post_comments': {
      const args = GetPostCommentsSchema.parse(request.params.arguments);
      const comments = await redditClient.getPostComments(args.postId, args.sort);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(comments, null, 2),
          },
        ],
      };
    }
  • Zod schema defining and validating the tool's input parameters: postId (required string) and sort (enum with default 'best'). Used for parsing in handler.
    const GetPostCommentsSchema = z.object({
      postId: z.string().min(1, "Post ID is required"),
      sort: z.enum(['best', 'top', 'new', 'controversial', 'old']).default('best'),
    });
  • src/index.ts:130-149 (registration)
    Tool registration in ListTools response, specifying name, description, and JSON inputSchema advertised to MCP clients.
    {
      name: 'get_post_comments',
      description: 'Get comments from a Reddit post',
      inputSchema: {
        type: 'object',
        properties: {
          postId: {
            type: 'string',
            description: 'Reddit post ID',
          },
          sort: {
            type: 'string',
            enum: ['best', 'top', 'new', 'controversial', 'old'],
            description: 'Sort order for comments',
            default: 'best',
          },
        },
        required: ['postId'],
      },
    },
  • Recursive helper function to traverse Reddit's nested comment tree and collect all top-level and reply comments into a flat array.
    private extractComments(children: any[], comments: RedditComment[]): void {
      for (const child of children) {
        if (child.kind === 't1' && child.data) {
          comments.push(this.mapComment(child.data));
          if (child.data.replies && child.data.replies.data && child.data.replies.data.children) {
            this.extractComments(child.data.replies.data.children, comments);
          }
        }
      }

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

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