hn_latest
Retrieve recent Hacker News stories. Specify a number (1-50) to control how many are returned.
Instructions
Get the latest/newest stories from Hacker News
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of stories to fetch (1-50, default: 10) |
Implementation Reference
- index.ts:235-256 (handler)The handler function for the 'hn_latest' tool. Fetches latest/newest stories from Hacker News via the API, formats them, stores the result in lastStoriesList, and returns formatted text content.
if (name === "hn_latest") { const limit = typeof args?.limit === 'number' ? args.limit : 10; const stories = await api.getLatestStories(limit); const formattedStories = stories.map(story => ({ id: story.id, title: story.title, by: story.by, time: api.formatTime(story.time), url: story.url, score: story.score, commentsCount: story.kids?.length || 0 })); lastStoriesList = formattedStories; return { content: [ { type: "text", text: formatStoriesAsText(formattedStories) } ] }; } - index.ts:146-161 (schema)Registration of the 'hn_latest' tool with its name, description, and input schema. The schema defines an optional 'limit' parameter (number, 1-50, default 10).
{ name: "hn_latest", description: "Get the latest/newest stories from Hacker News", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to fetch (1-50, default: 10)", minimum: 1, maximum: 50, default: 10 } } } }, - index.ts:146-161 (registration)Tool registration via the ListToolsRequestSchema handler, listing 'hn_latest' as one of the available tools.
{ name: "hn_latest", description: "Get the latest/newest stories from Hacker News", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to fetch (1-50, default: 10)", minimum: 1, maximum: 50, default: 10 } } } }, - index.ts:48-59 (helper)The getLatestStories() method on the HackerNewsAPI class that fetches story IDs from the /newstories.json endpoint, retrieves each story's details, and returns only story-type items.
async getLatestStories(limit = 50): Promise<Story[]> { try { const response = await axios.get(`${baseUrl}/newstories.json`); const storyIds = response.data || []; const storyPromises = storyIds.slice(0, limit).map((id: number) => this.getItemDetails(id)); const stories = await Promise.all(storyPromises); return stories.filter((story): story is Story => story !== null && story.type === 'story'); } catch (error) { console.error('Error fetching latest stories:', error); return []; } } - index.ts:391-406 (helper)The formatStoriesAsText() helper used by the hn_latest handler to format the list of stories into a readable text output.
function formatStoriesAsText(stories: FormattedStory[]): string { if (!stories || stories.length === 0) { return "No stories found."; } return stories.map((story, index) => { return `${index + 1}. ${story.title} ID: ${story.id} By: ${story.by} Published: ${story.time} Score: ${story.score} Comments: ${story.commentsCount} URL: ${story.url || 'N/A'} ------------------------------`; }).join('\n\n'); }