Skip to main content
Glama
erithwik

mcp-hn

by erithwik

search_stories

Search Hacker News stories using queries to find relevant content, with options to filter by date and control result count.

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
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.
num_resultsNoNumber of results to get, defaults to 10

Implementation Reference

  • Core implementation of the search_stories tool: queries HN Algolia API (/search or /search_by_date), fetches stories matching the query, formats them using _format_story_details, and returns the list.
    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"]]
  • Input JSON schema for the search_stories tool, defining parameters: query (required string), search_by_date (boolean), num_results (integer).
    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"],
    },
  • Registers the search_stories tool in the list_tools() handler 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"],
        },
    ),
  • MCP server tool call handler: extracts arguments, calls 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)]
  • Helper function used by search_stories to format raw API story data into a standard dict with id, title, url, author, points (basic mode).
    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
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It adds useful context about query performance (broader queries work better, targeted ones may fail) and implies read-only behavior through 'search'. However, it doesn't cover important aspects like rate limits, authentication needs, error handling, or response format, leaving significant gaps for a search tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded: the first sentence states the core purpose, and the second provides essential usage guidance. Every sentence earns its place with no wasted words, making it efficient and easy for an agent to parse.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (search with 3 parameters), no annotations, and no output schema, the description is partially complete. It covers purpose and query guidelines well but lacks details on behavioral traits (e.g., rate limits), response format, and error handling. For a search tool without annotations or output schema, more context would be helpful, making it adequate but with clear gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all three parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema. It mentions 'queries' generally but doesn't explain the 'query' parameter's semantics, format, or the 'search_by_date' and 'num_results' parameters. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool searches stories from Hacker News, providing a specific verb ('search') and resource ('stories'). It distinguishes from sibling tools like 'get_stories' (likely listing without search) and 'get_story_info' (getting details for a specific story). However, it doesn't explicitly contrast with these siblings, keeping it at 4 rather than 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context on when to use this tool: for searching stories with queries. It offers practical guidance on query construction (simpler queries with <5 words recommended, targeted queries may fail), which helps the agent decide when to use it. However, it doesn't explicitly mention when NOT to use it or name alternatives like 'get_stories' for non-search scenarios.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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