/**
* get_story MCP Tool
*/
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { HNError } from '../lib/errors.js';
import type { HNClient } from '../lib/hn-client.js';
import { createChildLogger } from '../lib/logger.js';
export function registerGetStory(server: McpServer, hnClient: HNClient): void {
server.registerTool(
'get_story',
{
title: 'Get Story',
description:
'Retrieve a specific story by ID with full nested comment tree. Returns complete story details including all comments and replies.',
inputSchema: {
id: z.number().int().min(1).describe('Story ID (numeric)'),
},
outputSchema: { id: z.number(), author: z.string(), points: z.number(), children: z.array(z.any()).optional() },
},
async ({ id }) => {
const correlationId = crypto.randomUUID();
const toolLogger = createChildLogger({ correlationId, tool: 'get_story', id });
try {
toolLogger.info({ id }, 'Getting story');
const result = await hnClient.getItem(id);
toolLogger.info('Retrieved story');
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
structuredContent: result as unknown as Record<string, unknown>,
};
} catch (error) {
toolLogger.error({ error }, 'Failed to get story');
if (error instanceof HNError) {
const errorContent = { error: error.message };
return {
content: [{ type: 'text', text: JSON.stringify(errorContent) }],
isError: true,
};
}
throw error;
}
}
);
}