Skip to main content
Glama
liuyang1520

Reddit MCP Server

by liuyang1520

search_posts

Find Reddit posts by searching with keywords, filtering by subreddit, sorting by relevance or time, and retrieving specific numbers of results.

Instructions

Search for Reddit posts

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSearch query
subredditNoOptional: restrict search to specific subreddit
sortNoSort order for search resultsrelevance
timeNoTime period to filter results (works best with sort=top)
limitNoNumber of results to retrieve (1-100)

Implementation Reference

  • MCP tool handler for 'search_posts': parses arguments using SearchPostsSchema, calls redditClient.searchPosts, and returns JSON stringified results.
    case 'search_posts': { const args = SearchPostsSchema.parse(request.params.arguments); const posts = await redditClient.searchPosts(args.query, args.subreddit, args.sort, args.time, args.limit); return { content: [ { type: 'text', text: JSON.stringify(posts, null, 2), }, ], }; }
  • Zod schema for validating 'search_posts' tool input parameters.
    const SearchPostsSchema = z.object({ query: z.string().min(1, "Search query is required"), subreddit: z.string().optional(), sort: z.enum(['relevance', 'hot', 'top', 'new', 'comments']).default('relevance'), time: z.enum(['hour', 'day', 'week', 'month', 'year', 'all']).optional(), limit: z.number().min(1).max(100).default(25), });
  • src/index.ts:232-267 (registration)
    Tool registration in listTools response, defining name, description, and JSON inputSchema matching the Zod schema.
    { name: 'search_posts', description: 'Search for Reddit posts', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', }, subreddit: { type: 'string', description: 'Optional: restrict search to specific subreddit', }, sort: { type: 'string', enum: ['relevance', 'hot', 'top', 'new', 'comments'], description: 'Sort order for search results', default: 'relevance', }, time: { type: 'string', enum: ['hour', 'day', 'week', 'month', 'year', 'all'], description: 'Time period to filter results (works best with sort=top)', }, limit: { type: 'number', description: 'Number of results to retrieve (1-100)', minimum: 1, maximum: 100, default: 25, }, }, required: ['query'], }, },
  • Core implementation of post search logic in RedditClient: constructs Reddit search API endpoint with parameters and fetches/maps results.
    async searchPosts(query: string, subreddit?: string, sort: 'relevance' | 'hot' | 'top' | 'new' | 'comments' = 'relevance', time?: 'hour' | 'day' | 'week' | 'month' | 'year' | 'all', limit: number = 25): Promise<RedditPost[]> { const searchPath = subreddit ? `/r/${subreddit}/search` : '/search'; const params = new URLSearchParams({ q: query, sort, limit: limit.toString(), type: 'link', ...(subreddit && { restrict_sr: 'true' }), ...(time && { t: time }) }); const data = await this.makeRequest(`${searchPath}?${params}`); return data.data.children.map((child: any) => this.mapPost(child.data)); }

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