Skip to main content
Glama

search

Find Hacker News stories and comments by entering a query, filtering by content type, and adjusting pagination settings for precise results.

Instructions

Search for stories and comments on Hacker News

Input Schema

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

Implementation Reference

  • The handler function for the 'search' tool, which validates input using SearchParamsSchema, calls algoliaApi.search, and formats the results into a text response.
    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 definition for the search tool input parameters.
    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, including name, description, and inputSchema.
    { 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"], }, },
  • The algoliaApi.search method called by the search handler to perform the actual search query against Hacker News Algolia API.
    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(); }

Other Tools

Related Tools

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