get_book_by_title
Search for books by title using the Open Library API. Retrieve detailed book information by providing the exact or partial title as input with this MCP server tool.
Instructions
Search for a book by its title on Open Library.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes | The title of the book to search for. |
Input Schema (JSON Schema)
{
"properties": {
"title": {
"description": "The title of the book to search for.",
"type": "string"
}
},
"required": [
"title"
],
"type": "object"
}
Implementation Reference
- The core handler function `handleGetBookByTitle` that implements the tool logic: validates input with Zod schema, queries Open Library search API, maps results to BookInfo, handles errors, and returns MCP CallToolResult.const handleGetBookByTitle = async ( args: unknown, axiosInstance: AxiosInstance, ): Promise<CallToolResult> => { const parseResult = GetBookByTitleArgsSchema.safeParse(args); if (!parseResult.success) { const errorMessages = parseResult.error.errors .map((e) => `${e.path.join(".")}: ${e.message}`) .join(", "); throw new McpError( ErrorCode.InvalidParams, `Invalid arguments for get_book_by_title: ${errorMessages}`, ); } const bookTitle = parseResult.data.title; try { const response = await axiosInstance.get<OpenLibrarySearchResponse>( "/search.json", { params: { title: bookTitle }, }, ); if ( !response.data || !response.data.docs || response.data.docs.length === 0 ) { return { content: [ { type: "text", text: `No books found matching title: "${bookTitle}"`, }, ], }; } const bookResults = Array.isArray(response.data.docs) ? response.data.docs.map((doc) => { const bookInfo: BookInfo = { title: doc.title, authors: doc.author_name || [], first_publish_year: doc.first_publish_year || null, open_library_work_key: doc.key, edition_count: doc.edition_count || 0, }; if (doc.cover_i) { bookInfo.cover_url = `https://covers.openlibrary.org/b/id/${doc.cover_i}-M.jpg`; } return bookInfo; }) : []; return { content: [ { type: "text", text: JSON.stringify(bookResults, null, 2), }, ], }; } catch (error) { let errorMessage = "Failed to fetch book data from Open Library."; if (axios.isAxiosError(error)) { errorMessage = `Error processing request: ${ error.response?.statusText ?? error.message }`; } else if (error instanceof Error) { errorMessage = `Error processing request: ${error.message}`; } console.error("Error in get_book_by_title:", error); return { content: [ { type: "text", text: errorMessage, }, ], isError: true, }; } };
- Zod schema `GetBookByTitleArgsSchema` used for input validation in the handler.// Schema for the get_book_by_title tool arguments export const GetBookByTitleArgsSchema = z.object({ title: z.string().min(1, { message: "Title cannot be empty" }), });
- src/index.ts:55-68 (registration)Tool registration in ListTools handler: defines name, description, and JSON inputSchema for MCP protocol.{ name: "get_book_by_title", description: "Search for a book by its title on Open Library.", inputSchema: { type: "object", properties: { title: { type: "string", description: "The title of the book to search for.", }, }, required: ["title"], }, },
- src/index.ts:170-171 (registration)Dispatch case in CallToolRequest handler that routes to the `handleGetBookByTitle` function.case "get_book_by_title": return handleGetBookByTitle(args, this.axiosInstance);
- TypeScript interfaces defining OpenLibrary API response shapes and output BookInfo used in the handler.export interface OpenLibraryDoc { title: string; author_name?: string[]; first_publish_year?: number; key: string; // Work key, e.g., "/works/OL45883W" edition_count: number; cover_i?: number; // Add optional cover ID } export interface OpenLibrarySearchResponse { docs: OpenLibraryDoc[]; } export interface BookInfo { title: string; authors: string[]; first_publish_year: number | null; open_library_work_key: string; edition_count: number; cover_url?: string; }