Skip to main content
Glama
libralm-ai

LibraLM MCP Server

Official
by libralm-ai

list_books

Browse available AI-generated book summaries and chapter breakdowns to discover titles by business, self-help, and educational categories.

Instructions

List all available books with their basic information

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The main handler function for the 'list_books' tool. It fetches the list of books from the LibraLM API endpoint '/books', parses the response into BookInfo models, sorts them by title, and returns the list. Includes error handling and debug logging.
    @mcp.tool()
    def list_books() -> List[BookInfo]:
        """List all available books with their basic information"""
        try:
            # Debug: Check what API key and URL we're using
            api_key = get_api_key()
            api_url = get_api_base_url()
            print(f"DEBUG: Using API URL: {api_url}")
            print(f"DEBUG: API key present: {bool(api_key)}, length: {len(api_key) if api_key else 0}")
    
            data = _make_api_request("/books")
            print(f"DEBUG: Received data: {data}")
    
            books = []
    
            for book_data in data.get("books", []):
                books.append(BookInfo(**book_data))
    
            print(f"DEBUG: Returning {len(books)} books")
            return sorted(books, key=lambda x: x.title)
        except Exception as e:
            print(f"ERROR listing books: {str(e)}")
            import traceback
            traceback.print_exc()
            # Raise the error instead of silently returning empty list
            raise ValueError(f"Error listing books: {str(e)}")
  • Pydantic BaseModel defining the structure of BookInfo objects returned by the list_books tool, including fields like book_id, title, author, and flags for available content.
    class BookInfo(BaseModel):
        """Book information structure"""
    
        book_id: str
        title: str
        author: Optional[str] = None
        category: Optional[str] = None
        subtitle: Optional[str] = None
        summary: Optional[str] = None
        length: Optional[str] = None
        release_date: Optional[str] = None
        tier: Optional[str] = None
        has_summary: bool
        has_chapter_summaries: bool
        has_table_of_contents: bool
  • Helper function used by list_books to make authenticated GET requests to the LibraLM API, handling authentication, errors, and response parsing.
    def _make_api_request(endpoint: str) -> dict:
        """Make an authenticated request to the LibraLM API"""
        # Get API key and base URL from request context or environment
        api_key = get_api_key()
        base_url = get_api_base_url()
    
        headers = {"x-api-key": api_key, "Content-Type": "application/json"}
    
        url = f"{base_url}{endpoint}"
        response = requests.get(url, headers=headers)
    
        if response.status_code == 401:
            raise ValueError("Invalid API key. Please check your LibraLM API key.")
        elif response.status_code == 404:
            raise ValueError(f"Resource not found: {endpoint}")
        elif response.status_code != 200:
            raise ValueError(
                f"API request failed with status {response.status_code}: {response.text}"
            )
    
        # Handle wrapped response format from Lambda
        result = response.json()
        if isinstance(result, dict) and "data" in result:
            return result["data"]
        return result

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/libralm-ai/libralm_mcp_server'

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