search-book
Search for books on Douban using ISBN numbers or keyword queries to find specific titles and authors.
Instructions
search books from douban, either by ISBN or by query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| isbn | No | ISBN number, e.g. "9787501524044" | |
| q | No | query string, e.g. "python" |
Implementation Reference
- src/index.ts:32-54 (handler)The handler function for the 'search-book' tool. Validates input arguments, calls the searchBooks helper from api.ts, formats the book results into a markdown table using json2md, and returns the content.async (args) => { if (!args.isbn && !args.q) { throw new McpError(ErrorCode.InvalidParams, "Either q or isbn must be provided") } const books = await searchBooks(args) const text = json2md({ table: { headers: ['publish_date', 'title', 'author', 'rating' ,'id', 'isbn'], rows: books.map(_ => ({ id: _.id || '', title: _.title || '', author: (_.author || []).join('、'), publish_date: _.pubdate, isbn: _.isbn13 || '', rating: `${_.rating?.average || 0} (${_.rating?.numRaters || 0}人)` })) } }) return { content: [{ type: 'text', text }] } }
- src/index.ts:28-31 (schema)Zod input schema defining optional 'q' (query string) and 'isbn' parameters for the 'search-book' tool.{ q: z.string().optional().describe('query string, e.g. "python"'), isbn: z.string().optional().describe('ISBN number, e.g. "9787501524044"') },
- src/index.ts:25-27 (registration)Registration of the 'search-book' tool using McpServer.tool method, referencing TOOL.SEARCH_BOOK constant.server.tool( TOOL.SEARCH_BOOK, 'search books from douban, either by ISBN or by query',
- src/api.ts:9-20 (helper)Helper function searchBooks that dispatches to keyword or ISBN search based on input params and returns array of books.export async function searchBooks(params: { q?: string isbn?: string }) { if (params.q) { return searchByKeyword(params.q) } if (params.isbn) { return searchByISBN(params.isbn) } return [] }
- src/api.ts:22-36 (helper)Implementation of keyword-based book search via Douban V2 API, parsing and returning books.async function searchByKeyword (q: string) { const url = new URL('https://api.douban.com/v2/book/search') url.searchParams.set('q', q) url.searchParams.set('apikey', apiKey) const res: { count: number start: number total: number books: Douban.Book[] } = await (await fetch(url.toString(), { headers: FAKE_HEADERS })).json() return res?.books ? res.books.map(parseDoubanBook) : [] }
- src/api.ts:38-45 (helper)Implementation of ISBN-based book search via Douban V2 API, returning single book or empty.async function searchByISBN (isbn: string) { const url = new URL(`https://api.douban.com/v2/book/isbn/${isbn}`) url.searchParams.set('apikey', apiKey) const res: Douban.Book = await (await fetch(url.toString(), { headers: FAKE_HEADERS })).json() return res?.id ? [parseDoubanBook(res)] : [] }
- src/types.ts:1-9 (schema)TOOL enum defining the tool name constant SEARCH_BOOK = 'search-book'.export enum TOOL { SEARCH_BOOK = 'search-book', LIST_BOOK_REVIEWS = 'list-book-reviews', SEARCH_MOVIE = 'search-movie', LIST_MOVIE_REVIEWS = 'list-movie-reviews', BROWSE = 'browse', LIST_GROUP_TOPICS = 'list-group-topics', GET_GROUP_TOPIC_DETAIL = 'get-group-topic-detail' }