Books API MCP Server

Books API MCP Server

MCP server that exposes book management tools for AI assistants, enabling CRUD operations on book collections through a .NET Books API backend.

Category
访问服务器

README

Books API MCP Server

A Model Context Protocol (MCP) server that exposes book management tools for AI assistants. This server provides a bridge between AI applications and a .NET Books API, enabling seamless book collection management through standardized MCP tool calls.

The .NET Book API backend that this MCP server interacts with is available at: https://github.com/0x1D-1983/book-api.

Features

  • MCP Protocol Support: Implements Model Context Protocol for tool discovery and execution
  • RESTful API Integration: Connects to a .NET Books API backend
  • Comprehensive CRUD Operations: Full Create, Read, Update, and Delete functionality for books
  • FastAPI Framework: Modern, fast, and easy-to-maintain Python web framework
  • Type-Safe Models: Uses Pydantic for robust data validation
  • Error Handling: Comprehensive error handling with clear error messages

Available Tools

The MCP server exposes the following tools:

1. list_books

Retrieve all books from the database.

Parameters:

  • None

Returns:

  • Array of book objects with the following fields:
    • id (integer): Unique book identifier
    • title (string): Book title
    • author (string): Book author
    • isbn (string): International Standard Book Number
    • publishedDate (string): Publication date in ISO 8601 format
    • createdAt (string): Record creation timestamp

Example Usage:

result = BookService.list_books()
# Returns: List of all books in the database

2. get_book

Retrieve a specific book by its ID.

Parameters:

  • book_id (integer, required): The unique identifier of the book to retrieve

Returns:

  • Book object with full details, or error if book not found

Example Usage:

result = BookService.get_book(1)
# Returns: Book with ID 1

3. create_book

Create a new book in the database.

Parameters:

  • book_data (object, required): Book information object containing:
    • id (integer, optional): Unique identifier for the book
    • title (string, required): Title of the book
    • author (string, required): Author of the book
    • isbn (string, optional): ISBN of the book
    • publishedDate (string, optional): Publication date in ISO 8601 UTC format (must end with 'Z', e.g., 2024-04-11T00:00:00Z)

Returns:

  • Created book object with all fields including generated createdAt timestamp

Example Usage:

book_data = {
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "isbn": "978-0743273565",
    "publishedDate": "1925-04-10T00:00:00Z"
}
result = BookService.create_book(book_data)

Important: The publishedDate field must be in ISO 8601 UTC format ending with 'Z' to ensure PostgreSQL compatibility.


4. update_book

Update an existing book's information.

Parameters:

  • book_id (integer, required): The unique identifier of the book to update
  • book_data (object, required): Updated book information object containing:
    • id (integer): ID of the book (must match book_id)
    • title (string, optional): New title
    • author (string, optional): New author
    • isbn (string, optional): New ISBN
    • publishedDate (string, optional): New publication date in ISO 8601 UTC format (must end with 'Z')

Returns:

  • Success message if update was successful, or error if book not found

Example Usage:

book_data = {
    "id": 1,
    "title": "Updated Title",
    "author": "Updated Author",
    "isbn": "978-1234567890",
    "publishedDate": "2023-01-01T00:00:00Z"
}
result = BookService.update_book(1, book_data)

5. delete_book

Delete a book from the database.

Parameters:

  • book_id (integer, required): The unique identifier of the book to delete

Returns:

  • Success message if deletion was successful, or error if book not found

Example Usage:

result = BookService.delete_book(1)
# Returns: Success message confirming deletion

Setup Instructions

Prerequisites

  • Python 3.8 or higher
  • Access to a running Books API instance (default: http://localhost:5288)
  • Virtual environment (recommended)

Installation

  1. Clone or navigate to the project directory:

    cd book-api-mcp-server
    
  2. Create and activate a virtual environment:

    python3 -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install dependencies:

    pip install fastapi uvicorn requests pydantic
    

    Or if you have a requirements.txt file:

    pip install -r requirements.txt
    
  4. Configure the Books API URL:

    Edit services.py and update the BOOKS_API_URL constant if your Books API is running on a different host/port:

    BOOKS_API_URL = "http://localhost:5288"  # Update as needed
    
  5. Run the server:

    python run.py
    

    The server will start on http://0.0.0.0:8080 by default.

    You can customize the host and port using environment variables:

    export MCP_SERVER_HOST=localhost
    export MCP_SERVER_PORT=8080
    python run.py
    

Testing the Server

  1. Check server health:

    curl http://localhost:8080/health
    
  2. List available tools:

    curl http://localhost:8080/tools
    
  3. Run the test suite:

    python test_tools.py
    

Configuration

Environment Variables

  • MCP_SERVER_HOST: Server host address (default: 0.0.0.0)
  • MCP_SERVER_PORT: Server port number (default: 8080)
  • BOOKS_API_URL: Base URL of the Books API (configured in services.py, default: http://localhost:5288)

Cursor Integration

To use this MCP server with Cursor, add the following to your ~/.cursor/mcp.json:

{
  "mcpServers": {
    "books-api": {
      "command": "python3",
      "args": ["/path/to/book-api-mcp-server/run.py"]
    }
  }
}

Note: Restart Cursor after updating the MCP configuration for changes to take effect.


API Endpoints

The MCP server exposes the following HTTP endpoints:

  • GET /: Root endpoint with server information
  • GET /tools: List all available MCP tools
  • POST /tool-calls: Execute one or more tool calls
  • GET /health: Health check endpoint
  • GET /docs: Interactive API documentation (FastAPI Swagger UI)

Usage Examples

Using Python Client

from services import BookService
import json

# List all books
books = BookService.list_books()
print(json.dumps(books.result, indent=2))

# Get a specific book
book = BookService.get_book(1)
print(json.dumps(book.result, indent=2))

# Create a new book
new_book = {
    "title": "1984",
    "author": "George Orwell",
    "isbn": "978-0451524935",
    "publishedDate": "1949-06-08T00:00:00Z"
}
result = BookService.create_book(new_book)
print(json.dumps(result.result, indent=2))

# Update a book
updated_data = {
    "id": 1,
    "title": "Nineteen Eighty-Four",
    "author": "George Orwell"
}
result = BookService.update_book(1, updated_data)

# Delete a book
result = BookService.delete_book(1)

Using HTTP API

# List all tools
curl http://localhost:8080/tools

# Call a tool
curl -X POST http://localhost:8080/tool-calls \
  -H "Content-Type: application/json" \
  -d '{
    "tool_calls": [
      {
        "name": "list_books",
        "parameters": {}
      }
    ]
  }'

Project Structure

book-api-mcp-server/
├── main.py              # FastAPI application and server setup
├── routes.py            # API route handlers for MCP endpoints
├── tools.py             # Tool definitions and schemas
├── services.py          # Business logic and Books API integration
├── models.py            # Pydantic models for data validation
├── config.py            # Configuration settings
├── run.py               # Server entry point
├── test_tools.py        # Test suite for validating tools
├── requirements.txt     # Python dependencies
└── README.md           # This file

Date Format Requirements

Important: All date fields (publishedDate) must be provided in ISO 8601 UTC format ending with 'Z'. This is required for PostgreSQL compatibility.

Correct Format:

  • 2024-04-11T00:00:00Z
  • 2023-05-11T12:30:45Z

Incorrect Formats (will cause errors):

  • 2024-04-11T00:00:00 (missing 'Z')
  • 2024-04-11 (not full ISO 8601)
  • 04/11/2024 (not ISO 8601)

Technologies

  • Python 3.8+: Programming language
  • FastAPI: Modern web framework for building APIs
  • Uvicorn: ASGI server for running FastAPI
  • Pydantic: Data validation using Python type annotations
  • Requests: HTTP library for making API calls to the Books API

Error Handling

All tools return a ToolCallResult object with the following structure:

{
    "result": <data> | None,  # Result data if successful
    "error": <string> | None  # Error message if operation failed
}

Common error scenarios:

  • 404 Not Found: Book with specified ID does not exist
  • 400 Bad Request: Invalid input data (e.g., missing required fields, invalid date format)
  • 500 Server Error: Database or API connection issues
  • Connection Error: Books API is not accessible

Troubleshooting

Server won't start

  • Check if port 8080 is already in use
  • Verify Python version (3.8+)
  • Ensure all dependencies are installed

Tools return connection errors

  • Verify the Books API is running and accessible
  • Check the BOOKS_API_URL in services.py
  • Test the Books API directly: curl http://localhost:5288/books

Date format errors

  • Ensure all dates end with 'Z' for UTC timezone
  • Use full ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ

License

This project is part of a book management system demonstration.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选