hn_latest
Fetch recent stories from Hacker News with customizable limits (1-50) to stay updated on tech and development trends efficiently.
Instructions
Get the latest/newest stories from Hacker News
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of stories to fetch (1-50, default: 10) |
Implementation Reference
- index.ts:235-256 (handler)Executes the hn_latest tool: fetches latest stories from Hacker News API with optional limit, formats them, updates the last stories list, and returns formatted text response.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:147-161 (registration)Registers the hn_latest tool in the listTools response, including name, description, and input schema for limit parameter.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:150-160 (schema)Defines the input schema for the hn_latest tool, specifying an optional limit parameter with constraints.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)Helper method in HackerNewsAPI class to fetch the latest story IDs and their details, filtering for story type.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)Helper function to format the list of stories as a readable text string for the tool response.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'); }