Book Library MCP Server
by Abishek-0607
README.md
# ๐ 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)
```bash
# 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
```bash
# 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
```bash
# 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 messages
- `httpx.TimeoutException` โ timeout message
- `httpx.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 |
This server cannot be deployed
Maintenance
ActivityInactive
ResponsivenessNo issues