Get Latest HackerNews Stories
get-latest-storiesRetrieve recent stories from HackerNews with customizable pagination to access current discussions and technology news.
Instructions
Get the most recent stories posted to HackerNews
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get-latest-stories"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hitsPerPage | No | Number of results per page (default: 20) | |
| page | No | Page number for pagination (default: 0) |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hits | Yes | ||
| page | Yes | ||
| nbHits | Yes | ||
| nbPages | Yes | ||
| hitsPerPage | Yes |
Implementation Reference
- src/index.ts:155-169 (handler)Handler function that fetches the most recent HackerNews stories using the /search_by_date endpoint with the 'story' tag filter.
async ({ page, hitsPerPage }) => { const params = new URLSearchParams(); params.append('tags', 'story'); 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:140-154 (schema)Schema definition including title, description, inputSchema (optional page and hitsPerPage), and outputSchema for the search results structure.
{ title: 'Get Latest HackerNews Stories', description: 'Get the most recent stories posted to HackerNews', 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:138-169 (registration)Complete registration of the 'get-latest-stories' tool on the MCP server.
server.registerTool( 'get-latest-stories', { title: 'Get Latest HackerNews Stories', description: 'Get the most recent stories posted to HackerNews', 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', 'story'); 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)Utility function used by the handler to perform API requests to the HackerNews Algolia API.
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(); }