Skip to main content
Glama

search

Find stories and comments on Hacker News by searching with specific queries, content types, and pagination controls.

Instructions

Search for stories and comments on Hacker News

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe search query
typeNoThe type of content to search forall
pageNoThe page number
hitsPerPageNoThe number of results per page

Implementation Reference

  • Handler function for the 'search' tool: validates arguments using SearchParamsSchema, performs search via algoliaApi, formats results into a numbered list and returns as text content.
    case "search": { const validatedArgs = validateInput(SearchParamsSchema, args); const { query, type, page, hitsPerPage } = validatedArgs; const tags = type === "all" ? undefined : type; const results = await algoliaApi.search(query, { tags, page, hitsPerPage, }); const hits = results.hits || []; const text = hits .map( (hit: any, index: number) => `${index + 1}. ${hit.title}\n` + ` ID: ${hit.objectID}\n` + ` URL: ${hit.url || "(text post)"}\n` + ` Points: ${hit.points} | Author: ${hit.author} | Comments: ${hit.num_comments}\n\n` ) .join(""); return { content: [{ type: "text", text: text.trim() }], }; }
  • Zod schema defining the input parameters for the 'search' tool, used for validation in the handler.
    export const SearchParamsSchema = z.object({ query: z.string(), type: z.enum(["all", "story", "comment"]).default("all"), page: z.number().int().min(0).default(0), hitsPerPage: z.number().int().min(1).max(100).default(20), });
  • src/index.ts:42-68 (registration)
    Registration of the 'search' tool in the ListTools response, specifying name, description, and JSON schema for inputs.
    { name: "search", description: "Search for stories and comments on Hacker News", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query" }, type: { type: "string", enum: ["all", "story", "comment"], description: "The type of content to search for", default: "all", }, page: { type: "number", description: "The page number", default: 0, }, hitsPerPage: { type: "number", description: "The number of results per page", default: 20, }, }, required: ["query"], }, },
  • algoliaApi.search helper function that constructs the Algolia HN search API request and fetches results.
    async search( query: string, options: { tags?: string; numericFilters?: string; page?: number; hitsPerPage?: number; } = {} ): Promise<any> { const params = new URLSearchParams(); params.append("query", query); if (options.tags) params.append("tags", options.tags); if (options.numericFilters) params.append("numericFilters", options.numericFilters); if (options.page !== undefined) params.append("page", options.page.toString()); if (options.hitsPerPage !== undefined) params.append("hitsPerPage", options.hitsPerPage.toString()); const url = `${API_BASE_URL}/search?${params.toString()}`; const response = await fetch(url); return response.json(); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/devabdultech/hn-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server