getAskHNStories
Fetch Ask HN stories from Hacker News using MCP Hacker News server. Specify the number of stories to retrieve (default: 10, max: 30) for streamlined access to community-driven questions and discussions.
Instructions
Get Ask HN stories
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of stories to return (default: 10, max: 30) |
Input Schema (JSON Schema)
{
"properties": {
"limit": {
"default": 10,
"description": "Number of stories to return (default: 10, max: 30)",
"type": "number"
}
},
"type": "object"
}
Implementation Reference
- src/tools.ts:198-239 (handler)Handler function that implements the core logic for getAskHNStories: fetches story IDs from Hacker News API, retrieves and formats story details, handles caching indirectly via helpers, and returns formatted JSON response.execute: async (args: any) => { const limit = Math.min(args.limit || 10, 30); const askIds = await fetchFromAPI<number[]>("/askstories"); if (!askIds) { return { content: [ { type: "text", text: JSON.stringify({ error: "Failed to fetch Ask HN stories" }), }, ], }; } const stories = await fetchMultipleItems(askIds, limit); const formattedStories = stories.map((story) => ({ id: story.id, title: story.title, score: story.score, author: story.by, comments: story.descendants || 0, time: story.time ? formatTime(story.time) : "unknown", hnUrl: `https://news.ycombinator.com/item?id=${story.id}`, text: story.text, })); return { content: [ { type: "text", text: JSON.stringify( { message: `Latest ${limit} Ask HN stories`, stories: formattedStories, }, null, 2 ), }, ], };
- src/tools.ts:188-197 (schema)Input schema for the getAskHNStories tool, defining an optional 'limit' parameter.inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to return (default: 10, max: 30)", default: 10, }, }, },
- src/tools.ts:185-241 (registration)The tool object registration within the exported tools array, which is imported and used by the MCP server in index.ts for tool listing and execution.{ name: "getAskHNStories", description: "Get Ask HN stories", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to return (default: 10, max: 30)", default: 10, }, }, }, execute: async (args: any) => { const limit = Math.min(args.limit || 10, 30); const askIds = await fetchFromAPI<number[]>("/askstories"); if (!askIds) { return { content: [ { type: "text", text: JSON.stringify({ error: "Failed to fetch Ask HN stories" }), }, ], }; } const stories = await fetchMultipleItems(askIds, limit); const formattedStories = stories.map((story) => ({ id: story.id, title: story.title, score: story.score, author: story.by, comments: story.descendants || 0, time: story.time ? formatTime(story.time) : "unknown", hnUrl: `https://news.ycombinator.com/item?id=${story.id}`, text: story.text, })); return { content: [ { type: "text", text: JSON.stringify( { message: `Latest ${limit} Ask HN stories`, stories: formattedStories, }, null, 2 ), }, ], }; }, },
- src/fetch-actions.ts:9-27 (helper)fetchFromAPI helper used to fetch '/askstories' endpoint with caching support.export async function fetchFromAPI<T>(endpoint: string): Promise<T | null> { const cacheKey = endpoint; const cached = getCached<T>(cacheKey); if (cached) return cached; try { const response = await fetch( `https://hacker-news.firebaseio.com/v0${endpoint}.json` ); if (!response.ok) throw new Error(`HTTP ${response.status}`); const data = await response.json(); setCache(cacheKey, data); return data; } catch (error) { console.error(`Error fetching ${endpoint}:`, error); return null; } }
- src/helpers.ts:23-35 (helper)formatTime utility used to format story timestamps in the handler response.export function formatTime(timestamp: number): string { const date = new Date(timestamp * 1000); const now = new Date(); const diff = now.getTime() - date.getTime(); const minutes = Math.floor(diff / (1000 * 60)); const hours = Math.floor(diff / (1000 * 60 * 60)); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (minutes < 60) return `${minutes}m ago`; if (hours < 24) return `${hours}h ago`; return `${days}d ago`; }