Skip to main content
Glama

reply_to_post

Post a reply to an existing Reddit post by providing the post ID and content, enabling direct engagement in discussions.

Instructions

Post a reply to an existing Reddit post

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
post_idYesThe ID of the post to reply to
contentYesThe content of the reply
subredditNoThe subreddit name if known (for validation)

Implementation Reference

  • The main tool handler function for 'reply_to_post'. It extracts parameters, gets the Reddit client, calls client.replyToPost, formats the comment info, and returns a success response with details.
    export async function replyToPost(params: {
      post_id: string;
      content: string;
      subreddit?: string;
    }) {
      const { post_id, content, subreddit } = params;
      const client = getRedditClient();
    
      if (!client) {
        throw new McpError(
          ErrorCode.InternalError,
          "Reddit client not initialized"
        );
      }
    
      try {
        console.log(`[Tool] Replying to post ${post_id}`);
        const comment = await client.replyToPost(post_id, content);
        const formattedComment = formatCommentInfo(comment);
    
        return {
          content: [
            {
              type: "text",
              text: `
    # Reply Posted Successfully
    
    ## Comment Details
    - Author: u/${formattedComment.author}
    - Subreddit: r/${formattedComment.context.subreddit}
    - Thread: ${formattedComment.context.thread}
    - Link: ${formattedComment.link}
    
    Your reply has been successfully posted.
              `,
            },
          ],
        };
      } catch (error) {
        console.error(`[Error] Error replying to post: ${error}`);
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to reply to post: ${error}`
        );
      }
    }
  • The input schema and metadata for the 'reply_to_post' tool as registered in the listTools response.
      name: "reply_to_post",
      description: "Post a reply to an existing Reddit post",
      inputSchema: {
        type: "object",
        properties: {
          post_id: {
            type: "string",
            description: "The ID of the post to reply to",
          },
          content: {
            type: "string",
            description: "The content of the reply",
          },
          subreddit: {
            type: "string",
            description: "The subreddit name if known (for validation)",
          },
        },
        required: ["post_id", "content"],
      },
    },
  • src/index.ts:464-471 (registration)
    The switch case in the CallToolRequest handler that dispatches 'reply_to_post' calls to the tools.replyToPost function.
    case "reply_to_post":
      return await tools.replyToPost(
        toolParams as {
          post_id: string;
          content: string;
          subreddit?: string;
        }
      );
  • The core RedditClient.replyToPost method that authenticates, checks post existence, makes the API POST to /api/comment, and constructs the RedditComment response object.
    async replyToPost(postId: string, content: string): Promise<RedditComment> {
      await this.authenticate();
    
      if (!this.username || !this.password) {
        throw new Error("User authentication required for posting replies");
      }
    
      try {
        if (!(await this.checkPostExists(postId))) {
          throw new Error(
            `Post with ID ${postId} does not exist or is not accessible`
          );
        }
    
        const params = new URLSearchParams();
        params.append("thing_id", `t3_${postId}`);
        params.append("text", content);
    
        const response = await this.api.post("/api/comment", params, {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
        });
    
        // Extract comment data from response
        const commentData = response.data;
        return {
          id: commentData.id,
          author: this.username,
          body: content,
          score: 1,
          controversiality: 0,
          subreddit: commentData.subreddit,
          submissionTitle: commentData.link_title,
          createdUtc: Date.now() / 1000,
          edited: false,
          isSubmitter: false,
          permalink: commentData.permalink,
        };
      } catch (error) {
        console.error(`[Error] Failed to reply to post ${postId}:`, error);
        throw new Error(`Failed to reply to post ${postId}`);
      }
    }

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