UNPKG

hn-mcp-server

Version:

Model Context Protocol server for HackerNews API access

97 lines (92 loc) 2.98 kB
/** * get-user MCP Tool * * Retrieves HackerNews user profile information by username. * Uses the users/:username API endpoint. */ import { z } from "zod"; import { HNAPIClient } from "../services/hn-api.js"; import { checkItemExists, createSuccessResult, handleAPIError } from "../utils/error-handlers.js"; /** * Input schema for get-user tool */ export const GetUserInputSchema = z.object({ username: z .string() .min(1, "username must not be empty") .regex(/^[a-zA-Z0-9_]+$/, "username must be alphanumeric with underscores only") .describe("HackerNews username"), }); /** * Output schema for get-user tool (HNUser) */ export const GetUserOutputSchema = z.object({ username: z.string(), karma: z.number().int().nonnegative(), about: z.string().nullable().optional(), created: z.number().int().positive().optional(), }); /** * Get user handler * * @param input - Input parameters (username) * @returns Tool result with user profile information */ export async function getUserHandler(input) { try { // Validate input const validatedParams = GetUserInputSchema.parse(input); // Create API client const client = new HNAPIClient(); // Call users API to get profile const result = await client.getUser(validatedParams.username); // Check if user exists const notFoundError = checkItemExists(result, "User", validatedParams.username); if (notFoundError) { return notFoundError; } // Validate output const validatedResult = GetUserOutputSchema.parse(result); return createSuccessResult(validatedResult); } catch (error) { return handleAPIError(error, "get-user"); } } /** * Tool metadata for MCP registration */ export const getUserTool = { name: "get-user", description: `Retrieve public profile information for a HackerNews user. Returns user profile including karma, bio, and account creation date. Use this to: - Check user reputation (karma score) - Read user bio and about information - See when account was created - Verify user existence before searching their content Features: - Username (case-sensitive) - Karma score (total upvotes received) - About/bio text (may contain HTML) - Account creation date (Unix timestamp) Examples: - Get famous user: { "username": "pg" } - Check moderator: { "username": "dang" } - Verify author: { "username": "tptacek" } Username validation: - Alphanumeric characters and underscores only - Case-sensitive - Must exist on HackerNews Returns error if user doesn't exist or username format is invalid.`, inputSchema: { type: "object", properties: { username: { type: "string", description: "HackerNews username (alphanumeric + underscores, e.g., 'pg')", }, }, required: ["username"], }, }; //# sourceMappingURL=get-user.js.map