search
Find stories and comments on Hacker News by searching with specific queries, content types, and pagination controls.
Instructions
Search for stories and comments on Hacker News
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query | |
| type | No | The type of content to search for | all |
| page | No | The page number | |
| hitsPerPage | No | The number of results per page |
Implementation Reference
- src/index.ts:181-205 (handler)Handler function for the 'search' tool: validates arguments using SearchParamsSchema, performs search via algoliaApi, formats results into a numbered list and returns as text content.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 defining the input parameters for the 'search' tool, used for validation in the handler.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, specifying name, description, and JSON schema for inputs.{ 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)algoliaApi.search helper function that constructs the Algolia HN search API request and fetches results.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(); }