search
Find Hacker News stories and comments by entering a query, filtering by content type, and adjusting pagination settings for precise results.
Instructions
Search for stories and comments on Hacker News
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hitsPerPage | No | The number of results per page | |
| page | No | The page number | |
| query | Yes | The search query | |
| type | No | The type of content to search for | all |
Implementation Reference
- src/index.ts:181-205 (handler)The handler function for the 'search' tool, which validates input using SearchParamsSchema, calls algoliaApi.search, and formats the results into a text response.case "search": { const validatedArgs = validateInput(SearchParamsSchema, args); const { query, type, page, hitsPerPage } = validatedArgs; const tags = type === "all" ? undefined : type; const results = await algoliaApi.search(query, { tags, page, hitsPerPage, }); const hits = results.hits || []; const text = hits .map( (hit: any, index: number) => `${index + 1}. ${hit.title}\n` + ` ID: ${hit.objectID}\n` + ` URL: ${hit.url || "(text post)"}\n` + ` Points: ${hit.points} | Author: ${hit.author} | Comments: ${hit.num_comments}\n\n` ) .join(""); return { content: [{ type: "text", text: text.trim() }], }; }
- src/schemas/index.ts:38-43 (schema)Zod schema definition for the search tool input parameters.export const SearchParamsSchema = z.object({ query: z.string(), type: z.enum(["all", "story", "comment"]).default("all"), page: z.number().int().min(0).default(0), hitsPerPage: z.number().int().min(1).max(100).default(20), });
- src/index.ts:42-68 (registration)Registration of the 'search' tool in the ListTools response, including name, description, and inputSchema.{ name: "search", description: "Search for stories and comments on Hacker News", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query" }, type: { type: "string", enum: ["all", "story", "comment"], description: "The type of content to search for", default: "all", }, page: { type: "number", description: "The page number", default: 0, }, hitsPerPage: { type: "number", description: "The number of results per page", default: 20, }, }, required: ["query"], }, },
- src/api/algolia.ts:12-35 (helper)The algoliaApi.search method called by the search handler to perform the actual search query against Hacker News Algolia API.async search( query: string, options: { tags?: string; numericFilters?: string; page?: number; hitsPerPage?: number; } = {} ): Promise<any> { const params = new URLSearchParams(); params.append("query", query); if (options.tags) params.append("tags", options.tags); if (options.numericFilters) params.append("numericFilters", options.numericFilters); if (options.page !== undefined) params.append("page", options.page.toString()); if (options.hitsPerPage !== undefined) params.append("hitsPerPage", options.hitsPerPage.toString()); const url = `${API_BASE_URL}/search?${params.toString()}`; const response = await fetch(url); return response.json(); }