get-ask-hn
Retrieve Ask HN posts where HackerNews users ask questions to the community, with pagination support for browsing multiple pages of results.
Instructions
Get latest "Ask HN" posts where users ask questions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hitsPerPage | No | Number of results per page (default: 20) | |
| page | No | Page number for pagination (default: 0) |
Implementation Reference
- src/index.ts:257-270 (handler)Handler function that fetches the latest 'Ask HN' posts from the HackerNews API using the /search_by_date endpoint with the 'ask_hn' tag filter. Supports pagination via page and hitsPerPage parameters. Returns results in both plain text JSON and structured content formats.async ({ page, hitsPerPage }) => { const params = new URLSearchParams(); params.append('tags', 'ask_hn'); if (page !== undefined) params.append('page', page.toString()); if (hitsPerPage !== undefined) params.append('hitsPerPage', hitsPerPage.toString()); const endpoint = `/search_by_date?${params.toString()}`; const result = await fetchHN(endpoint); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], structuredContent: result }; }
- src/index.ts:242-256 (schema)Input and output schemas defined using Zod for the 'get-ask-hn' tool, including optional pagination parameters for input and expected search result structure for output.{ title: 'Get Ask HN Posts', description: 'Get latest "Ask HN" posts where users ask questions', inputSchema: { page: z.number().optional().describe('Page number for pagination (default: 0)'), hitsPerPage: z.number().optional().describe('Number of results per page (default: 20)') }, outputSchema: { hits: z.array(z.any()), nbHits: z.number(), nbPages: z.number(), page: z.number(), hitsPerPage: z.number() } },
- src/index.ts:240-271 (registration)Full registration of the 'get-ask-hn' tool on the MCP server using server.registerTool, including name, metadata, schema, and handler function.server.registerTool( 'get-ask-hn', { title: 'Get Ask HN Posts', description: 'Get latest "Ask HN" posts where users ask questions', inputSchema: { page: z.number().optional().describe('Page number for pagination (default: 0)'), hitsPerPage: z.number().optional().describe('Number of results per page (default: 20)') }, outputSchema: { hits: z.array(z.any()), nbHits: z.number(), nbPages: z.number(), page: z.number(), hitsPerPage: z.number() } }, async ({ page, hitsPerPage }) => { const params = new URLSearchParams(); params.append('tags', 'ask_hn'); if (page !== undefined) params.append('page', page.toString()); if (hitsPerPage !== undefined) params.append('hitsPerPage', hitsPerPage.toString()); const endpoint = `/search_by_date?${params.toString()}`; const result = await fetchHN(endpoint); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], structuredContent: result }; } );
- src/index.ts:11-17 (helper)Shared helper function used by the 'get-ask-hn' handler (and other tools) to perform API requests to the HackerNews Algolia API and handle errors.async function fetchHN(endpoint: string): Promise<any> { const response = await fetch(`${HN_API_BASE}${endpoint}`); if (!response.ok) { throw new Error(`HN API error: ${response.status} ${response.statusText}`); } return await response.json(); }