Skip to main content
Glama
jordanburke

reddit-mcp-server

create_post

Create a new post in a specified subreddit with a title and content, supporting both text and link submissions.

Instructions

Create a new post in a subreddit

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYesContent of the post (text for self posts, URL for link posts)
is_selfNoWhether this is a self (text) post (true) or link post (false)
subredditYesName of the subreddit to post in
titleYesTitle of the post

Implementation Reference

  • src/index.ts:497-532 (registration)
    MCP tool registration for 'create_post', including schema and inline handler function
    server.addTool({
      name: "create_post",
      description: "Create a new post in a subreddit (requires REDDIT_USERNAME and REDDIT_PASSWORD)",
      parameters: z.object({
        subreddit: z.string().describe("The subreddit name (without r/ prefix)"),
        title: z.string().describe("The post title"),
        content: z.string().describe("The post content (text for self posts, URL for link posts)"),
        is_self: z.boolean().default(true).describe("Whether this is a self post (text) or link post"),
      }),
      execute: async (args) => {
        const client = getRedditClient()
        if (!client) {
          throw new Error("Reddit client not initialized")
        }
    
        // Check if user credentials are configured
        if (!process.env.REDDIT_USERNAME || !process.env.REDDIT_PASSWORD) {
          throw new Error(
            "User authentication required. Please set REDDIT_USERNAME and REDDIT_PASSWORD environment variables.",
          )
        }
    
        const post = await client.createPost(args.subreddit, args.title, args.content, args.is_self)
        const formattedPost = formatPostInfo(post)
    
        return `# Post Created Successfully
    
    ## Post Details
    - Title: ${formattedPost.title}
    - Subreddit: r/${formattedPost.subreddit}
    - Type: ${formattedPost.type}
    - Link: ${formattedPost.links.fullPost}
    
    Your post has been successfully submitted to r/${formattedPost.subreddit}.`
      },
    })
  • Core handler in RedditClient that performs the actual POST /api/submit to Reddit API
    async createPost(subreddit: string, title: string, content: string, isSelf: boolean = true): Promise<RedditPost> {
      await this.authenticate()
    
      if (!this.username || !this.password) {
        throw new Error("User authentication required for posting")
      }
    
      try {
        const kind = isSelf ? "self" : "link"
        const params = new URLSearchParams()
        params.append("sr", subreddit)
        params.append("kind", kind)
        params.append("title", title)
        params.append(isSelf ? "text" : "url", content)
        params.append("api_type", "json") // Request standard JSON response format
    
        const response = await this.makeRequest("/api/submit", {
          method: "POST",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          body: params.toString(),
        })
    
        if (!response.ok) {
          const errorText = await response.text()
          console.error(`[Reddit API] Create post failed: ${response.status} ${response.statusText}`)
          console.error(`[Reddit API] Error response: ${errorText}`)
          throw new Error(`HTTP ${response.status}: ${errorText}`)
        }
    
        const json = (await response.json()) as any
        console.error(`[Reddit API] Create post response:`, JSON.stringify(json, null, 2))
    
        // With api_type=json, response has json.data.id or json.errors
        if (json.json?.errors && json.json.errors.length > 0) {
          const errors = json.json.errors.map((e: any) => e.join(": ")).join(", ")
          console.error(`[Reddit API] Post creation errors: ${errors}`)
          throw new Error(`Reddit API errors: ${errors}`)
        }
    
        // Extract post ID from standard JSON response
        const postId = json.json?.data?.id || json.json?.data?.name?.replace("t3_", "")
    
        if (!postId) {
          console.error(`[Reddit API] No post ID in response`)
          throw new Error("No post ID returned from Reddit")
        }
    
        console.error(`[Reddit API] Post created with ID: ${postId}`)
        return await this.getPost(postId, subreddit)
      } catch (error) {
        // Log and re-throw the actual error
        console.error(`[Reddit API] Create post exception:`, error)
        if (error instanceof Error && error.message.includes("HTTP")) {
          throw error
        }
        throw new Error(
          `Failed to create post in ${subreddit}: ${error instanceof Error ? error.message : String(error)}`,
        )
      }
    }
  • Input schema validation using Zod for the create_post tool parameters
    parameters: z.object({
      subreddit: z.string().describe("The subreddit name (without r/ prefix)"),
      title: z.string().describe("The post title"),
      content: z.string().describe("The post content (text for self posts, URL for link posts)"),
      is_self: z.boolean().default(true).describe("Whether this is a self post (text) or link post"),
    }),

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

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