Skip to main content
Glama

get_comments_by_submission

Retrieve comments from a Reddit submission by providing its ID, enabling analysis of discussion threads and user interactions.

Instructions

Accéder aux commentaires d'une soumission

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
submission_idYesL'ID de la soumission
limitNoNombre de commentaires à récupérer

Implementation Reference

  • The main tool handler function that fetches comments for a submission using the Reddit client, formats them, and returns formatted content blocks.
    export async function getCommentsBySubmission(params: {
      submission_id: string;
      limit?: number;
    }) {
      const { submission_id, limit = 20 } = params;
      const client = getRedditClient();
    
      if (!client) {
        throw new McpError(
          ErrorCode.InternalError,
          "Reddit client not initialized"
        );
      }
    
      try {
        console.log(`[Tool] Getting comments for submission ${submission_id}`);
        const comments = await client.getCommentsBySubmission(submission_id, limit);
        const formattedComments = comments.map(formatCommentInfo);
    
        const commentSummaries = formattedComments
          .map(
            (comment, index) => `
    ### ${index + 1}. u/${comment.author}
    - Score: ${comment.stats.score}
    - Posté: ${comment.metadata.posted}
    - Contenu: ${comment.content.substring(0, 200)}${comment.content.length > 200 ? "..." : ""}
        `
          )
          .join("\n");
    
        return {
          content: [
            {
              type: "text",
              text: `
    # Commentaires pour la soumission ${submission_id}
    
    ${commentSummaries}
              `,
            },
          ],
        };
      } catch (error) {
        console.error(`[Error] Error getting comments by submission: ${error}`);
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to fetch comments: ${error}`
        );
      }
  • Input schema definition for the get_comments_by_submission tool, specifying submission_id as required and limit as optional.
    inputSchema: {
      type: "object",
      properties: {
        submission_id: {
          type: "string",
          description: "L'ID de la soumission",
        },
        limit: {
          type: "integer",
          description: "Nombre de commentaires à récupérer",
          default: 20,
        },
      },
      required: ["submission_id"],
    },
  • src/index.ts:478-481 (registration)
    Tool dispatch/registration in the switch statement for handling CallToolRequest.
    case "get_comments_by_submission":
      return await tools.getCommentsBySubmission(
        toolParams as { submission_id: string; limit?: number }
      );
  • src/index.ts:245-262 (registration)
    Tool registration in the ListTools response, including name, description, and schema.
      name: "get_comments_by_submission",
      description: "Accéder aux commentaires d'une soumission",
      inputSchema: {
        type: "object",
        properties: {
          submission_id: {
            type: "string",
            description: "L'ID de la soumission",
          },
          limit: {
            type: "integer",
            description: "Nombre de commentaires à récupérer",
            default: 20,
          },
        },
        required: ["submission_id"],
      },
    },
  • Underlying RedditClient method that performs the API call to fetch comments by submission ID.
    async getCommentsBySubmission(submissionId: string, limit: number = 20): Promise<RedditComment[]> {
      await this.authenticate();
      try {
        const response = await this.api.get(`/comments/${submissionId}.json`, {
          params: { limit }
        });
    
        if (!response.data || response.data.length < 2) {
          throw new Error(`Submission with ID ${submissionId} not found or has no comments`);
        }
    
        const comments = response.data[1].data.children;
        return comments.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: response.data[0].data.children[0].data.title,
            createdUtc: comment.created_utc,
            edited: !!comment.edited,
            isSubmitter: comment.is_submitter,
            permalink: comment.permalink,
          };
        });
      } catch (error) {
        console.error(`[Error] Failed to get comments for submission ${submissionId}:`, error);
        throw new Error(`Failed to get comments for submission ${submissionId}`);
      }

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/samy-clivolt/reddit-mcp-server'

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