search_texts
Search the National Library of Norway's digital collection for newspapers, books, or journals using text queries and optional date filters.
Instructions
Search for texts in the National Library's digital collection.
Args: query: Search query string limit: Maximum number of results to return (default: 10) from_year: Start year for search period (optional) to_year: End year for search period (optional) media_type: Type of media to search. Options: 'digavis' (newspapers), 'digibok' (books), 'digitidsskrift' (journals). Default: 'digavis'
Returns: JSON string containing search results with metadata
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| limit | No | ||
| from_year | No | ||
| to_year | No | ||
| media_type | No | digavis |
Implementation Reference
- src/dhlab_mcp/server.py:11-51 (handler)The handler function that implements the core logic of the 'search_texts' tool. It constructs parameters for dhlab.Corpus search based on inputs and returns JSON results or error message.def search_texts( query: str, limit: int = 10, from_year: int | None = None, to_year: int | None = None, media_type: str = "digavis", ) -> str: """Search for texts in the National Library's digital collection. Args: query: Search query string limit: Maximum number of results to return (default: 10) from_year: Start year for search period (optional) to_year: End year for search period (optional) media_type: Type of media to search. Options: 'digavis' (newspapers), 'digibok' (books), 'digitidsskrift' (journals). Default: 'digavis' Returns: JSON string containing search results with metadata """ try: # Build search parameters using correct Corpus API params = { "fulltext": query, "limit": limit, "doctype": media_type } if from_year: params["from_year"] = from_year if to_year: params["to_year"] = to_year # Perform search using dhlab corpus = dhlab.Corpus(**params) # Return corpus information if hasattr(corpus, 'corpus') and corpus.corpus is not None: return corpus.corpus.to_json(orient='records', force_ascii=False) return "No results found" except Exception as e: return f"Error searching texts: {str(e)}"
- src/dhlab_mcp/server.py:10-10 (registration)The @mcp.tool() decorator registers the search_texts function as an MCP tool in the FastMCP server.@mcp.tool()
- src/dhlab_mcp/server.py:11-30 (schema)Input/output schema defined by Python type hints on parameters and return type, along with detailed docstring describing arguments and expected JSON string output.def search_texts( query: str, limit: int = 10, from_year: int | None = None, to_year: int | None = None, media_type: str = "digavis", ) -> str: """Search for texts in the National Library's digital collection. Args: query: Search query string limit: Maximum number of results to return (default: 10) from_year: Start year for search period (optional) to_year: End year for search period (optional) media_type: Type of media to search. Options: 'digavis' (newspapers), 'digibok' (books), 'digitidsskrift' (journals). Default: 'digavis' Returns: JSON string containing search results with metadata """