Skip to main content
Glama
8enSmith

mcp-open-library

get_book_by_id

Retrieve detailed book information by specifying its identifier type (ISBN, LCCN, OCLC, OLID) and value using the Open Library API.

Instructions

Get detailed information about a book using its identifier (ISBN, LCCN, OCLC, OLID).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idTypeYesThe type of identifier used (ISBN, LCCN, OCLC, OLID).
idValueYesThe value of the identifier.

Implementation Reference

  • Implements the core logic for the 'get_book_by_id' tool: validates input arguments, fetches book data from Open Library API via axios, processes and formats the response into BookDetails, handles errors, and returns structured content.
    export const handleGetBookById = async (
      args: unknown,
      axiosInstance: AxiosInstance,
    ): Promise<CallToolResult> => {
      const parseResult = GetBookByIdArgsSchema.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_id: ${errorMessages}`,
        );
      }
    
      const { idType, idValue } = parseResult.data;
      const apiUrl = `/api/volumes/brief/${idType}/${idValue}.json`;
    
      try {
        const response = await axiosInstance.get<OpenLibraryBookResponse>(apiUrl);
    
        // Check if records object exists and is not empty
        if (
          !response.data ||
          !response.data.records ||
          Object.keys(response.data.records).length === 0
        ) {
          return {
            content: [
              {
                type: "text",
                text: `No book found for ${idType}: ${idValue}`,
              },
            ],
          };
        }
    
        // Get the first record from the records object
        const recordKey = Object.keys(response.data.records)[0];
        const record: OpenLibraryRecord | undefined =
          response.data.records[recordKey];
    
        if (!record) {
          // This case should theoretically not happen if the length check passed, but good for safety
          return {
            content: [
              {
                type: "text",
                text: `Could not process book record for ${idType}: ${idValue}`,
              },
            ],
          };
        }
    
        const recordData = record.data;
        const recordDetails = record.details?.details; // Access the nested details
    
        const bookDetails: BookDetails = {
          title: recordData.title,
          subtitle: recordData.subtitle,
          authors: recordData.authors?.map((a) => a.name) || [],
          publishers: recordData.publishers?.map((p) => p.name),
          publish_date: recordData.publish_date,
          number_of_pages:
            recordData.number_of_pages ?? recordDetails?.number_of_pages,
          // Prefer identifiers from recordData, fallback to recordDetails if necessary
          isbn_13: recordData.identifiers?.isbn_13 ?? recordDetails?.isbn_13,
          isbn_10: recordData.identifiers?.isbn_10 ?? recordDetails?.isbn_10,
          lccn: recordData.identifiers?.lccn ?? recordDetails?.lccn,
          oclc: recordData.identifiers?.oclc ?? recordDetails?.oclc_numbers,
          olid: recordData.identifiers?.openlibrary, // Add OLID from identifiers
          open_library_edition_key: recordData.key, // From recordData
          open_library_work_key: recordDetails?.works?.[0]?.key, // From nested details
          cover_url: recordData.cover?.medium, // Use medium cover from recordData
          info_url: record.details?.info_url ?? recordData.url, // Prefer info_url from details
          preview_url:
            record.details?.preview_url ?? recordData.ebooks?.[0]?.preview_url,
        };
    
        // Clean up undefined fields
        Object.keys(bookDetails).forEach((key) => {
          const typedKey = key as keyof BookDetails;
          if (
            bookDetails[typedKey] === undefined ||
            ((typedKey === "authors" || typedKey === "publishers") &&
              Array.isArray(bookDetails[typedKey]) &&
              bookDetails[typedKey].length === 0)
          ) {
            delete bookDetails[typedKey];
          }
        });
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(bookDetails, null, 2),
            },
          ],
        };
      } catch (error) {
        let errorMessage = "Failed to fetch book data from Open Library.";
        if (axios.isAxiosError(error)) {
          if (error.response?.status === 404) {
            errorMessage = `No book found for ${idType}: ${idValue}`;
          } else {
            errorMessage = `API Error: ${error.response?.statusText ?? error.message}`;
          }
        } else if (error instanceof Error) {
          errorMessage = `Error processing request: ${error.message}`;
        }
        console.error("Error in get_book_by_id:", error);
    
        // Return error as text content
        return {
          content: [
            {
              type: "text",
              text: errorMessage,
            },
          ],
        };
      }
    };
  • Zod validation schema for the tool's input arguments: idType (enum: isbn, lccn, oclc, olid) and idValue (non-empty string).
    // Schema for the get_book_by_id tool arguments
    const GetBookByIdArgsSchema = z.object({
      idType: z
        .string()
        .transform((val) => val.toLowerCase())
        .pipe(
          z.enum(["isbn", "lccn", "oclc", "olid"], {
            errorMap: () => ({
              message: "idType must be one of: isbn, lccn, oclc, olid",
            }),
          }),
        ),
      idValue: z.string().min(1, { message: "idValue cannot be empty" }),
    });
  • TypeScript interface defining the structured BookDetails output returned by the handler.
    export interface BookDetails {
      title: string;
      subtitle?: string;
      authors: string[];
      publishers?: string[];
      publish_date?: string;
      number_of_pages?: number;
      isbn_13?: string[];
      isbn_10?: string[];
      lccn?: string[];
      oclc?: string[];
      olid?: string[]; // Add OLID field
      open_library_edition_key: string; // e.g., "/books/OL24194264M"
      open_library_work_key?: string; // e.g., "/works/OL15610910W"
      cover_url?: string;
      info_url: string;
      preview_url?: string;
    }
  • src/index.ts:142-162 (registration)
    Registers the tool in the MCP server's listTools response, providing name, description, and input schema.
    {
      name: "get_book_by_id",
      description:
        "Get detailed information about a book using its identifier (ISBN, LCCN, OCLC, OLID).",
      inputSchema: {
        type: "object",
        properties: {
          idType: {
            type: "string",
            enum: ["isbn", "lccn", "oclc", "olid"],
            description:
              "The type of identifier used (ISBN, LCCN, OCLC, OLID).",
          },
          idValue: {
            type: "string",
            description: "The value of the identifier.",
          },
        },
        required: ["idType", "idValue"],
      },
    },
  • src/index.ts:180-181 (registration)
    Routes tool calls named 'get_book_by_id' to the handleGetBookById handler function in the callTool request handler.
    case "get_book_by_id":
      return handleGetBookById(args, this.axiosInstance);
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool retrieves 'detailed information' but does not specify what that includes (e.g., title, author, publication year), whether it's a read-only operation, potential error conditions, or rate limits. This leaves significant gaps in understanding the tool's behavior.

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 a single, efficient sentence that directly states the tool's purpose and key input mechanism. It is front-loaded with essential information and contains no redundant or unnecessary details.

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

Completeness2/5

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

Given the lack of annotations and output schema, the description is insufficient for a tool that retrieves 'detailed information'. It does not clarify what data is returned, potential limitations, or how to handle errors, leaving the agent with incomplete context for effective use.

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?

The schema description coverage is 100%, with clear descriptions for both parameters, including an enum for 'idType'. The description adds minimal value by listing the identifier types in parentheses, but it does not provide additional context such as format examples or constraints beyond what the schema already documents.

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's purpose with a specific verb ('Get') and resource ('detailed information about a book'), and it specifies the mechanism ('using its identifier'). However, it does not explicitly differentiate from sibling tools like 'get_book_by_title', which serves a similar purpose but uses a different lookup method.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It does not mention sibling tools like 'get_book_by_title' for title-based lookups or 'get_author_info' for author-related queries, nor does it specify any prerequisites or exclusions for usage.

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

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/8enSmith/mcp-open-library'

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