goagiq/mcp-fastapi-server
If you are the rightful owner of mcp-fastapi-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
A Model Context Protocol (MCP) server built with FastAPI for managing a book collection.
FastAPI Book Collection MCP Server
A Model Context Protocol (MCP) server built with FastAPI for managing a book collection. This application provides both a RESTful API and MCP tool integration for performing CRUD operations on books stored in a SQLite database.
Features
- Complete CRUD operations for book management
- FastAPI with automatic OpenAPI documentation
- SQLite database with SQLAlchemy ORM
- Model Context Protocol (MCP) integration
- Pydantic models for data validation
- Interactive API documentation
- Pagination support for book listings
- Automatic database initialization
Prerequisites
- Python 3.13 or higher
- No external dependencies (uses SQLite)
Installation
-
Clone or download this project
-
Install dependencies using uv:
uv syncOr using pip:
pip install fastapi[standard] sqlalchemy pydantic uvicorn fastapi-mcp
Usage
Running the Server
Start the FastAPI server with MCP integration:
python main.py
This will start the server on http://127.0.0.1:8000 with both REST API and MCP capabilities.
Alternatively, you can run with uvicorn directly:
uvicorn main:app --host 127.0.0.1 --port 8000
Accessing the API
- Interactive Documentation:
http://127.0.0.1:8000/docs(Swagger UI) - Alternative Documentation:
http://127.0.0.1:8000/redoc(ReDoc) - Root Endpoint:
http://127.0.0.1:8000/(Welcome page with links)
API Endpoints
Root
- GET /: Welcome message with navigation links
- Returns: Welcome message and links to documentation and book list
Books Management
-
GET /books: Retrieve all books with pagination
- Query Parameters:
skip(int, default=0): Number of books to skiplimit(int, default=10): Maximum number of books to return
- Returns: List of books
- Query Parameters:
-
GET /books/{book_id}: Retrieve a specific book
- Path Parameters:
book_id(int): Unique identifier of the book
- Returns: Book details or 404 if not found
- Path Parameters:
-
POST /books: Create a new book
- Request Body:
{ "title": "Book Title", "read": false } - Returns: Created book with assigned ID
- Request Body:
-
PUT /books/{book_id}: Update an existing book
- Path Parameters:
book_id(int): Unique identifier of the book
- Request Body:
{ "title": "Updated Book Title", "read": true } - Returns: Updated book details or 404 if not found
- Path Parameters:
-
DELETE /books/{book_id}: Delete a book
- Path Parameters:
book_id(int): Unique identifier of the book
- Returns: Deleted book details or 404 if not found
- Path Parameters:
Data Models
Book Model (Database)
class Book(Base):
id: int (Primary Key)
title: str (Indexed)
read: bool (Default: False)
Request/Response Models
BookCreate (Request)
{
"title": "string",
"read": false # Optional, defaults to false
}
BookResponse (Response)
{
"id": 1,
"title": "string",
"read": false
}
MCP Integration
This server includes Model Context Protocol (MCP) integration using FastAPI-MCP, which exposes the following operations as MCP tools:
get_books- Retrieve all booksget_book- Retrieve a specific book by IDcreate_book- Create a new bookupdate_book- Update an existing bookdelete_book- Delete a book
These tools can be used by MCP-compatible clients and AI assistants for book management operations.
MCP Tool Usage Example
# The MCP tools are automatically exposed and can be called by MCP clients
# Example operations available:
# - get_books(skip=0, limit=10)
# - get_book(book_id=1)
# - create_book(title="New Book", read=False)
# - update_book(book_id=1, title="Updated Title", read=True)
# - delete_book(book_id=1)
Database
The application uses SQLite for data storage with the following characteristics:
- Database File:
test.db(automatically created) - ORM: SQLAlchemy with declarative base
- Connection: SQLite with automatic session management
- Initialization: Tables are created automatically on startup
Database Schema
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title VARCHAR,
read BOOLEAN
);
Example Usage
Using cURL
Create a book:
curl -X POST "http://127.0.0.1:8000/books" \
-H "Content-Type: application/json" \
-d '{"title": "The Great Gatsby", "read": false}'
Get all books:
curl -X GET "http://127.0.0.1:8000/books"
Update a book:
curl -X PUT "http://127.0.0.1:8000/books/1" \
-H "Content-Type: application/json" \
-d '{"title": "The Great Gatsby", "read": true}'
Using Python Requests
import requests
# Create a book
response = requests.post(
"http://127.0.0.1:8000/books",
json={"title": "1984", "read": False}
)
book = response.json()
print(f"Created book: {book}")
# Get all books
response = requests.get("http://127.0.0.1:8000/books")
books = response.json()
print(f"All books: {books}")
# Mark book as read
book_id = book["id"]
response = requests.put(
f"http://127.0.0.1:8000/books/{book_id}",
json={"title": "1984", "read": True}
)
updated_book = response.json()
print(f"Updated book: {updated_book}")
Development
Project Structure
mcp-fastapi-server/
├── main.py # Main FastAPI application
├── pyproject.toml # Project configuration
├── README.md # This file
├── test.db # SQLite database (created at runtime)
├── uv.lock # Dependency lock file
└── .gitignore # Git ignore rules
Dependencies
- FastAPI: Modern, fast web framework for APIs
- SQLAlchemy: SQL toolkit and ORM
- Pydantic: Data validation using Python type hints
- Uvicorn: Lightning-fast ASGI server
- FastAPI-MCP: Model Context Protocol integration for FastAPI
Running in Development Mode
For development with auto-reload:
uvicorn main:app --host 127.0.0.1 --port 8000 --reload
Error Handling
The API includes proper error handling:
- 404 Not Found: When a book with the specified ID doesn't exist
- 422 Unprocessable Entity: When request data validation fails
- 500 Internal Server Error: For unexpected server errors
Error responses follow the FastAPI standard format:
{
"detail": "Book not found"
}
Testing
You can test the API using:
- Interactive Documentation: Visit
/docsfor Swagger UI testing - cURL: Use command-line HTTP requests
- Python Requests: Programmatic API testing
- Postman: Import the OpenAPI specification from
/openapi.json
Configuration
The application uses the following default configuration:
- Host: 127.0.0.1 (localhost)
- Port: 8000
- Database: SQLite (
test.db) - Auto-reload: Disabled in production
To modify configuration, edit the uvicorn.run() call in main.py.
Troubleshooting
Common Issues
-
Port already in use
- Change the port in
main.pyor kill the process using port 8000
- Change the port in
-
Database permission errors
- Ensure write permissions in the application directory
-
Import errors
- Verify all dependencies are installed:
uv sync
- Verify all dependencies are installed:
-
MCP tools not working
- Ensure FastAPI-MCP is properly installed and configured
Logging
The application uses FastAPI's built-in logging. For more detailed logs, run with:
uvicorn main:app --log-level debug
Contributing
Feel free to submit issues and enhancement requests!
License
This project is provided as-is for educational and development purposes.