Skip to main content
Glama

search_stories

Search Hacker News stories using simple queries, with options to filter by date, relevance, points, or comments. Retrieve targeted results efficiently.

Instructions

Search stories from Hacker News. It is generally recommended to use simpler queries to get a broader set of results (less than 5 words). Very targetted queries may not return any results.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
num_resultsNoNumber of results to get, defaults to 10
queryYesSearch query
search_by_dateNoSearch by date, defaults to False. If this is False, then we search by relevance, then points, then number of comments.

Implementation Reference

  • Core handler function that executes the search_stories tool logic: constructs HN Algolia API URL based on parameters, fetches data, and formats story results using _format_story_details.
    def search_stories(query: str, num_results: int = DEFAULT_NUM_STORIES, search_by_date: bool = False): """ Searches Hacker News stories using a query string. Args: query: Search terms to find in stories num_results: Number of results to return (default: 10) search_by_date: If True, sorts by date. If False, sorts by relevance/points/comments (default: False) Returns: List[Dict]: List of matching story dictionaries, each containing: { "id": int, # Story ID "title": str, # Story title "url": str, # Story URL "author": str, # Author username "points": int, # Points (may be null) } Raises: requests.exceptions.RequestException: If the API request fails """ if search_by_date: url = f"{BASE_API_URL}/search_by_date?query={query}&hitsPerPage={num_results}&tags=story" else: url = f"{BASE_API_URL}/search?query={query}&hitsPerPage={num_results}&tags=story" print(url) response = requests.get(url) response.raise_for_status() return [_format_story_details(story) for story in response.json()["hits"]]
  • MCP server tool call handler that extracts arguments, invokes hn.search_stories, JSON serializes the output, and returns as TextContent.
    elif name == "search_stories": query = arguments.get("query") search_by_date = arguments.get("search_by_date", False) num_results = arguments.get("num_results", DEFAULT_NUM_STORIES) output = json.dumps(hn.search_stories(query, num_results, search_by_date), indent=2) return [types.TextContent(type="text", text=output)]
  • Registers the search_stories tool in the MCP server's list_tools() by creating and returning a Tool object with name, description, and input schema.
    types.Tool( name="search_stories", description="Search stories from Hacker News. It is generally recommended to use simpler queries to get a broader set of results (less than 5 words). Very targetted queries may not return any results.", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "Search query", }, "search_by_date": { "type": "boolean", "description": "Search by date, defaults to False. If this is False, then we search by relevance, then points, then number of comments.", }, "num_results": { "type": "integer", "description": f"Number of results to get, defaults to {DEFAULT_NUM_STORIES}", }, }, "required": ["query"], }, ),
  • Input JSON schema for validating arguments to the search_stories tool: requires 'query', optional 'search_by_date' and 'num_results'.
    "type": "object", "properties": { "query": { "type": "string", "description": "Search query", }, "search_by_date": { "type": "boolean", "description": "Search by date, defaults to False. If this is False, then we search by relevance, then points, then number of comments.", }, "num_results": { "type": "integer", "description": f"Number of results to get, defaults to {DEFAULT_NUM_STORIES}", }, }, "required": ["query"], },
  • Helper function used by search_stories to format raw API story data into a consistent dictionary structure (basic mode excludes comments).
    def _format_story_details(story: Union[Dict, int], basic: bool = True) -> Dict: """ Formats a story's details into a standardized dictionary structure. Args: story: Either a story ID or dictionary containing story data basic: If True, excludes comments. If False, includes formatted comments to depth of 2 Returns: Dict with the following structure: { "id": int, # Story ID "title": str, # Story title if present "url": str, # Story URL if present "author": str, # Author username "points": int, # Points (may be null) "comments": list # List of comment dicts (only if basic=False) } The function handles both raw story IDs and story dictionaries, fetching additional data if needed. For non-basic requests, it ensures comments are properly formatted. """ if isinstance(story, int): story = _get_story_info(story) output = { "id": story["story_id"], "author": story["author"], } if "title" in story: output["title"] = story["title"] if "points" in story: output["points"] = story["points"] if "url" in story: output["url"] = story["url"] if not basic: if _validate_comments_is_list_of_dicts(story["children"]): story = _get_story_info(story["story_id"]) output["comments"] = [ _format_comment_details(child) for child in story["children"] ] return output

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/erithwik/mcp-hn'

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