Book Library MCP Server
Enables Claude or any LLM to manage a book library via MCP tools, allowing listing, searching, creating, updating, and deleting books through natural language.
README
๐ Book Library โ MCP Server Demo
A production-ready reference implementation showing how Claude (or any LLM) connects to a real REST API through an MCP Server.
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ YOU / CLAUDE โ
โ (natural language: "find me Python books") โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Protocol (JSON-RPC 2.0)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP SERVER :9000 โ
โ (book_mcp โ FastMCP) โ
โ โ
โ Tools exposed: โ
โ book_list book_search book_get โ
โ book_create book_update book_delete โ
โ โ
โ Responsibilities: โ
โ โ
Translate MCP tool calls โ HTTP requests โ
โ โ
Validate inputs with Pydantic โ
โ โ
Format API responses as Markdown or JSON โ
โ โ
Handle and surface errors cleanly โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ REST API (HTTP/JSON)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BOOK LIBRARY API :8000 โ
โ (FastAPI) โ
โ โ
โ Endpoints: โ
โ GET /api/v1/books/ List + filter + paginate โ
โ GET /api/v1/books/search Full-text search โ
โ GET /api/v1/books/{id} Get single book โ
โ POST /api/v1/books/ Create book โ
โ PATCH /api/v1/books/{id} Update book โ
โ DELETE /api/v1/books/{id} Delete book โ
โ GET /health Health probe โ
โ GET /docs Swagger UI โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Folder Structure
book-mcp-demo/
โ
โโโ api/ # FastAPI REST API
โ โโโ core/
โ โ โโโ config.py # Settings from env vars
โ โโโ exceptions/
โ โ โโโ __init__.py # Custom exception classes
โ โโโ models/
โ โ โโโ book.py # Domain model + in-memory store
โ โโโ routers/
โ โ โโโ books.py # All CRUD route handlers
โ โโโ schemas/
โ โ โโโ book.py # Pydantic request/response schemas
โ โโโ main.py # FastAPI app entry point
โ โโโ requirements.txt
โ
โโโ mcp_server/
โ โโโ server.py # FastMCP server with 6 tools
โ โโโ requirements.txt
โ
โโโ mcp_client/
โ โโโ client.py # Demo client (shows MCP wire protocol)
โ โโโ requirements.txt
โ
โโโ tests/
โ โโโ test_books_api.py # API integration tests
โ
โโโ Dockerfile.api # Multi-stage Docker build for API
โโโ Dockerfile.mcp # Multi-stage Docker build for MCP server
โโโ Dockerfile.client # Docker build for demo client
โโโ docker-compose.yml # Orchestrates all 3 services
โโโ requirements.txt # Root deps for local dev
โโโ .env # Environment variables
๐ Quick Start
Option 1 โ Docker Compose (Recommended)
# Clone and enter the project
cd book-mcp-demo
# Start all 3 services (API + MCP Server + Client demo)
docker compose up --build
# You'll see:
# book_api โ ready on port 8000
# book_mcp โ ready on port 9000
# book_mcp_client โ runs all demo scenarios, then exits
Option 2 โ Local Development
# Install all dependencies
pip install -r requirements.txt
# Terminal 1: Start the API
uvicorn api.main:app --reload --port 8000
# Terminal 2: Start the MCP server
python mcp_server/server.py
# Terminal 3: Run the client demo
python mcp_client/client.py
๐งช Running Tests
# Install test deps
pip install -r requirements.txt
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ -v --cov=api
๐ API Documentation
Once the API is running, open:
| URL | Description |
|---|---|
| http://localhost:8000/docs | Swagger UI (interactive) |
| http://localhost:8000/redoc | ReDoc UI |
| http://localhost:8000/health | Health check |
๐ง MCP Tools Reference
| Tool | Description | Read-only |
|---|---|---|
book_list |
List all books with filters and pagination | โ Yes |
book_search |
Search by title or author | โ Yes |
book_get |
Get a single book by ID | โ Yes |
book_create |
Add a new book (ISBN must be unique) | โ No |
book_update |
Partially update a book | โ No |
book_delete |
Permanently delete a book | โ No |
๐ How MCP Works (Step by Step)
Step 1 โ Handshake
Client โ Server: initialize (announce capabilities)
Server โ Client: serverInfo + available tools list
Step 2 โ Tool Discovery
Client โ Server: tools/list
Server โ Client: [{name, description, inputSchema}, ...]
Step 3 โ Tool Call
Client โ Server: tools/call {name: "book_search", arguments: {query: "python"}}
Server: validates input โ calls Book API โ formats response
Server โ Client: {content: [{type: "text", text: "### Search results..."}]}
๐ก๏ธ Error Handling Strategy
API Layer (FastAPI)
- Custom exceptions (
BookNotFoundError,DuplicateISBNError) โ raised in business logic - Exception handlers in routers convert them to proper HTTP status codes
- Global handler catches anything unexpected โ always returns clean JSON
MCP Layer (FastMCP)
_handle_api_error()โ single shared function maps all error types:httpx.HTTPStatusErrorโ maps HTTP codes to user-friendly messageshttpx.TimeoutExceptionโ timeout messagehttpx.ConnectErrorโ connectivity message
- Never exposes raw stack traces to the LLM client
- Structured logging to stderr (stdout is reserved for MCP protocol)
๐ณ Container Design Decisions
| Decision | Reason |
|---|---|
| Multi-stage builds | Smaller final images (no build tools in runtime) |
| Non-root user | Security best practice |
| Health checks | Docker and orchestrators (K8s) need these for readiness |
depends_on: condition: service_healthy |
MCP won't start until API is truly ready |
PYTHONUNBUFFERED=1 |
Logs appear immediately in Docker |
Per-service requirements.txt |
Only installs what each container needs |
โ๏ธ Environment Variables
| Variable | Default | Description |
|---|---|---|
BOOK_API_BASE_URL |
http://localhost:8000/api/v1 |
API base URL seen by MCP server |
BOOK_API_TIMEOUT |
10.0 |
HTTP timeout in seconds |
MCP_HOST |
0.0.0.0 |
MCP server bind host |
MCP_PORT |
9000 |
MCP server port |
MCP_SERVER_URL |
http://localhost:9000 |
MCP URL seen by client |
DEBUG |
false |
Enable debug logging |
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
Neon Database
MCP server for interacting with Neon Management API and databases
E2B
Using MCP to run code via e2b.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.