Skip to main content
Glama

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
NameRequiredDescriptionDefault
countYesMax result count
queryYesMain search query
sortNoType of search results sortingrelevance
time_filterNoTime filter for search resultsall
timeoutNoMax scrapping execution timeout (in seconds)

Implementation Reference

  • 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 }; } }
  • 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 }; } } );
  • API endpoint constant used by the handler to route the request to the Reddit posts search backend.
    REDDIT_SEARCH_POSTS: "/api/reddit/search/posts",
  • 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(); }); };

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/anysiteio/hdw-mcp-server'

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