search_reddit_posts
Search Reddit posts using keywords, sort by relevance or time, and filter results by date range to find specific discussions and content.
Instructions
Search for Reddit posts with various filters
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | Yes | Max result count | |
| query | Yes | Main search query | |
| sort | No | Type of search results sorting | relevance |
| time_filter | No | Time filter for search results | all |
| timeout | No | Max scrapping execution timeout (in seconds) |
Implementation Reference
- src/index.ts:755-771 (handler)The handler function that executes the tool: constructs request data, calls the Reddit search API endpoint via makeRequest, logs progress, returns formatted JSON response or error.async ({ query, count, timeout, sort, time_filter }) => { const requestData = { timeout, query, sort, time_filter, count }; log("Starting Reddit posts search with:", JSON.stringify(requestData)); try { const response = await makeRequest(API_CONFIG.ENDPOINTS.REDDIT_SEARCH_POSTS, requestData); log(`Search complete, found ${response.length} results`); return { content: [{ type: "text", text: JSON.stringify(response, null, 2) }] }; } catch (error) { log("Reddit search posts error:", error); return { content: [{ type: "text", text: `Reddit search posts API error: ${formatError(error)}` }], isError: true }; } }
- src/index.ts:748-754 (schema)Zod schema defining the input parameters for the search_reddit_posts tool, including query, count, timeout, sort, and time_filter with descriptions and defaults.{ query: z.string().describe("Search query"), count: z.number().default(10).describe("Max results"), timeout: z.number().default(300).describe("Timeout in seconds"), sort: z.string().default("relevance").describe("Sort order"), time_filter: z.string().default("all").describe("Time filter") },
- src/index.ts:745-772 (registration)The MCP server.tool registration call that defines and registers the search_reddit_posts tool with its name, description, input schema, and handler function.server.tool( "search_reddit_posts", "Search Reddit posts", { query: z.string().describe("Search query"), count: z.number().default(10).describe("Max results"), timeout: z.number().default(300).describe("Timeout in seconds"), sort: z.string().default("relevance").describe("Sort order"), time_filter: z.string().default("all").describe("Time filter") }, async ({ query, count, timeout, sort, time_filter }) => { const requestData = { timeout, query, sort, time_filter, count }; log("Starting Reddit posts search with:", JSON.stringify(requestData)); try { const response = await makeRequest(API_CONFIG.ENDPOINTS.REDDIT_SEARCH_POSTS, requestData); log(`Search complete, found ${response.length} results`); return { content: [{ type: "text", text: JSON.stringify(response, null, 2) }] }; } catch (error) { log("Reddit search posts error:", error); return { content: [{ type: "text", text: `Reddit search posts API error: ${formatError(error)}` }], isError: true }; } } );
- src/index.ts:29-29 (helper)API endpoint constant used by the handler to route the request to the Reddit posts search backend.REDDIT_SEARCH_POSTS: "/api/reddit/search/posts",
- src/index.ts:100-145 (helper)Generic helper function makeRequest used by the tool handler to perform authenticated HTTPS POST requests to the AnySite API endpoints.const makeRequest = (endpoint: string, data: any, method: string = "POST"): Promise<any> => { return new Promise((resolve, reject) => { const url = new URL(endpoint, API_CONFIG.BASE_URL); const postData = JSON.stringify(data); const options = { hostname: url.hostname, port: url.port || 443, path: url.pathname, method: method, headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(postData), "access-token": API_KEY, ...(ACCOUNT_ID && { "x-account-id": ACCOUNT_ID }) } }; const req = https.request(options, (res) => { let responseData = ""; res.on("data", (chunk) => { responseData += chunk; }); res.on("end", () => { try { const parsed = JSON.parse(responseData); if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) { resolve(parsed); } else { reject(new Error(`API error ${res.statusCode}: ${JSON.stringify(parsed)}`)); } } catch (e) { reject(new Error(`Failed to parse response: ${responseData}`)); } }); }); req.on("error", (error) => { reject(error); }); req.write(postData); req.end(); }); };