Skip to content

feat: add get-book-by-id tool #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project implements an MCP server that provides tools for AI assistants to i
- **Get Author Details**: Retrieve detailed information for a specific author using their Open Library key (`get_author_info`).
- **Get Author Photo**: Get the URL for an author's photo using their Open Library ID (OLID) (`get_author_photo`).
- **Get Book Cover**: Get the URL for a book's cover image using various identifiers (ISBN, OCLC, LCCN, OLID, ID) (`get_book_cover`).
- **Get Book by ID**: Retrieve detailed book information using various identifiers (ISBN, LCCN, OCLC, OLID) (`get_book_by_id`).

## Installation

Expand Down Expand Up @@ -66,6 +67,7 @@ This server implements the Model Context Protocol, which means it can be used by
- `get_author_info`: Get detailed information for a specific author using their Open Library Author Key
- `get_author_photo`: Get the URL for an author's photo using their Open Library Author ID (OLID)
- `get_book_cover`: Get the URL for a book's cover image using a specific identifier (ISBN, OCLC, LCCN, OLID, or ID)
- `get_book_by_id`: Get detailed book information using a specific identifier (ISBN, LCCN, OCLC, or OLID)

**Example `get_book_by_title` input:**
```json
Expand Down Expand Up @@ -174,6 +176,50 @@ The `get_book_cover` tool accepts the following parameters:
- `value`: The value of the identifier
- `size`: Optional cover size (`S` for small, `M` for medium, `L` for large, defaults to `L`)

**Example `get_book_by_id` input:**
```json
{
"idType": "isbn",
"idValue": "9780547928227"
}
```

**Example `get_book_by_id` output:**
```json
{
"title": "The Hobbit",
"authors": [
"J. R. R. Tolkien"
],
"publishers": [
"Houghton Mifflin Harcourt"
],
"publish_date": "October 21, 2012",
"number_of_pages": 300,
"isbn_13": [
"9780547928227"
],
"isbn_10": [
"054792822X"
],
"oclc": [
"794607877"
],
"olid": [
"OL25380781M"
],
"open_library_edition_key": "/books/OL25380781M",
"open_library_work_key": "/works/OL45883W",
"cover_url": "https://covers.openlibrary.org/b/id/8231496-M.jpg",
"info_url": "https://openlibrary.org/books/OL25380781M/The_Hobbit",
"preview_url": "https://archive.org/details/hobbit00tolkien"
}
```

The `get_book_by_id` tool accepts the following parameters:
- `idType`: The type of identifier (one of: `isbn`, `lccn`, `oclc`, `olid`)
- `idValue`: The value of the identifier

An example of this tool being used in Claude Desktop can be see here:

<img width="1132" alt="image" src="https://github.com/user-attachments/assets/0865904a-f984-4f7b-a27d-6397ac59d6d2" />
Expand Down
8 changes: 4 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("OpenLibraryServer", () => {

if (listToolsHandler) {
const result = await listToolsHandler({} as any); // Call the handler
expect(result.tools).toHaveLength(5);
expect(result.tools).toHaveLength(6);
expect(result.tools[0].name).toBe("get_book_by_title");
expect(result.tools[0].description).toBeDefined();
expect(result.tools[0].inputSchema).toEqual({
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("OpenLibraryServer", () => {

if (listToolsHandler) {
const result = await listToolsHandler({} as any);
expect(result.tools).toHaveLength(5);
expect(result.tools).toHaveLength(6);
const authorTool = result.tools.find(
(tool: any) => tool.name === "get_authors_by_name",
);
Expand Down Expand Up @@ -176,7 +176,7 @@ describe("OpenLibraryServer", () => {

if (listToolsHandler) {
const result = await listToolsHandler({} as any);
expect(result.tools).toHaveLength(5);
expect(result.tools).toHaveLength(6);
const authorInfoTool = result.tools.find(
(tool: any) => tool.name === "get_author_info",
);
Expand Down Expand Up @@ -248,7 +248,7 @@ describe("OpenLibraryServer", () => {

if (listToolsHandler) {
const result = await listToolsHandler({} as any);
expect(result.tools).toHaveLength(5);
expect(result.tools).toHaveLength(6);
const authorPhotoTool = result.tools.find(
(tool: any) => tool.name === "get_author_photo",
);
Expand Down
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
handleGetBookCover,
handleGetAuthorsByName,
handleGetAuthorInfo,
handleGetBookById,
} from "./tools/index.js";

class OpenLibraryServer {
Expand Down Expand Up @@ -138,6 +139,27 @@ class OpenLibraryServer {
required: ["key", "value"],
},
},
{
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"],
},
},
],
}));

Expand All @@ -155,6 +177,8 @@ class OpenLibraryServer {
return handleGetAuthorPhoto(args);
case "get_book_cover":
return handleGetBookCover(args);
case "get_book_by_id":
return handleGetBookById(args, this.axiosInstance);
default:
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
}
Expand Down
Loading