This example demonstrates how to use EnrichMCP with SQLAlchemy ORM models. It's based on the shop_api_sqlite example but uses SQLAlchemy for database modeling and queries.
- SQLAlchemy model definitions with EnrichMCP integration
- Automatic conversion of SQLAlchemy models to EnrichModel representations
- Async SQLAlchemy with SQLite
- Automatic creation of CRUD resources and relationship resolvers
- Pagination examples using the generated list endpoints
The example includes four main models:
- User - Shop customers with orders
- Product - Items available for purchase
- Order - Customer orders with status tracking
- OrderItem - Individual items within orders
- SQLAlchemy Models: Uses declarative SQLAlchemy models instead of manual SQL
- Type Safety: SQLAlchemy provides better type safety and relationship handling
- Automatic Schema: Tables are created automatically from model definitions
- ORM Benefits: Lazy loading, eager loading options, and query building
- Install dependencies:
pip install enrichmcp[sqlalchemy]
pip install aiosqlite # For async SQLite support- Run the application:
python app.pyThe app will:
- Create the SQLite database (
shop.db) if it doesn't exist - Create all tables based on the SQLAlchemy models
- Seed sample data on first run
- Start the MCP server
The lifecycle is managed using the sqlalchemy_lifespan helper from
enrichmcp.sqlalchemy, which provides a session factory to all resources.
Passing cleanup_db_file=True removes the shop.db file when the app shuts
down.
All CRUD resources (list_* and get_*) along with relationship resolvers are
created automatically via include_sqlalchemy_models. The column info
dictionary supplies descriptions for the generated endpoints.