books-catalog

books-catalog

A well-documented MCP server that exposes a local SQLite catalog of public-domain books through tools like search, get, filter by genre, top-rated, and catalog stats.

Category
访问服务器

README

mcp-quickstart — a books-catalog MCP server

A clean, well-documented Model Context Protocol (MCP) server, built with FastMCP, that exposes a local SQLite catalog of public-domain books as a set of well-designed tools. It is meant as a reference for how to design MCP tools an LLM can use reliably: clear names, schema-generating type hints, docstrings-as-contracts, validated inputs, and actionable error messages.

The data is a small set of public-domain classics (Austen, Verne, Doyle, Dostoevsky, ...) authored as seed data for this repo — nothing proprietary, and deliberately a generic domain.


What it exposes

Five tools over a 30-book catalog:

Tool Signature Purpose
search_books (query: str, limit: int = 10) Substring search over title + author.
get_book (book_id: int) Fetch one book by id; clear error if missing.
find_books_by_genre (genre, year_from?, year_to?, limit=20) Filter by genre + optional year range.
top_rated_books (limit=5, genre?) Highest-rated, optionally within a genre.
catalog_stats () Totals, per-genre counts, year range, averages.

Quickstart

git clone <this-repo> && cd mcp-quickstart
python -m pip install -r requirements.txt   # installs `mcp` and `pytest`

# Smoke test — list the tools and schemas without a live client:
python -m books_mcp list-tools

# Run the test suite (offline, in-memory catalog):
python -m pytest

# Run the server (stdio transport — this is what an MCP client launches):
python -m books_mcp

The smoke test output:

books-catalog MCP server exposes 5 tools (catalog: 30 books):

  - search_books(query, limit)
      Search the catalog by a keyword in the title or author.
  - get_book(book_id)
      Fetch a single book by its numeric id.
  - find_books_by_genre(genre, year_from, year_to, limit)
      List books in a genre, optionally filtered by publication-year range.
  - top_rated_books(limit, genre)
      Return the highest-rated books, optionally within a single genre.
  - catalog_stats()
      Summarize the whole catalog.

Example tool result (catalog_stats()):

{
  "total_books": 30,
  "total_authors": 19,
  "genres": { "Adventure": 7, "Fiction": 7, "Gothic": 6, "ScienceFiction": 3,
              "Historical": 2, "Mystery": 2, "Romance": 2, "Fantasy": 1 },
  "year_range": [1811, 1902],
  "avg_pages": 468.4,
  "avg_rating": 4.17
}

MCP tool-design patterns demonstrated

These are the choices that make tools usable by a model, not just callable:

  1. One job per tool, named as a verb-phrase. search_books, find_books_by_genre, get_book. A model picks the right tool from the name alone.
  2. The docstring is the contract. FastMCP turns each function's signature into the tool's JSON input schema and its docstring into the description the model sees. Both are written for a model reader: what it does, every argument, the return shape, and what raises.
  3. Type hints drive the schema. book_id: int, genre: str, year_from: int | None = None become a validated input schema with the right types and optionality — no hand-written JSON schema.
  4. Validate and bound every input. Limits are clamped to a hard MAX_LIMIT so a tool call can never trigger an unbounded scan; empty queries and bad ids are rejected up front.
  5. Errors are actionable. An unknown genre returns Unknown genre 'Cyberpunk'. Available genres: Adventure, Fiction, ...; a bad id returns the valid range. The model can correct itself from the message.
  6. Outputs are plain JSON-serializable dicts, never database row objects — stable, predictable, and easy for the model to consume.
  7. The data layer is separate from the protocol. db.py is a typed, independently testable SQLite layer; server.py only does validation + wiring. You can unit-test all the logic without standing up a server.

Connect it to an MCP client

Claude Desktop

Add the server to your claude_desktop_config.json (macOS: ~/Library/Application Support/Claude/; Windows: %APPDATA%\Claude\):

{
  "mcpServers": {
    "books-catalog": {
      "command": "python",
      "args": ["-m", "books_mcp"],
      "cwd": "/absolute/path/to/mcp-quickstart"
    }
  }
}

Restart Claude Desktop; the five tools appear under the server. Then ask natural-language questions like "What are the top-rated adventure books before 1880?" and the model will call find_books_by_genre / top_rated_books.

Any MCP client

The server speaks the standard MCP stdio transport, so any compliant client (the MCP Inspector, a custom client using the mcp SDK, etc.) can launch python -m books_mcp and discover the tools via the normal list_tools / call_tool handshake.


Storage

By default the server builds an in-memory SQLite catalog from the bundled seed data at startup — zero setup, always fresh, no files left behind. To materialize a file-backed catalog (e.g. to inspect it with the sqlite3 CLI or share it across processes):

python -m books_mcp seed --db catalog.db
BOOKS_DB_PATH=catalog.db python -m books_mcp        # serve from the file

Project layout

mcp-quickstart/
├── books_mcp/
│   ├── __init__.py
│   ├── __main__.py      # serve | list-tools | seed
│   ├── server.py        # FastMCP server + the five tools
│   ├── db.py            # typed SQLite data-access layer (no MCP)
│   ├── seed.py          # build/seed a file-backed catalog
│   └── data/
│       └── books.json   # public-domain seed catalog
└── tests/               # pytest suite, all offline
    ├── test_db.py       # data-layer behavior
    └── test_tools.py    # tool behavior, validation, and registration

License

MIT — see LICENSE.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选