A comprehensive FastAPI microservice for health-related operations with JWT authentication, PostgreSQL database, and Alembic migrations.It is backed by MCP server using FastApiMCP
health-api-mcp-server-with-fastapi-demo.webm
- JWT-based authentication
- PostgreSQL database with async SQLAlchemy
- Alembic database migrations
- Comprehensive health domain models (Patient, Doctor, Appointment, Medical Record)
- RESTful API endpoints
- Interactive API documentation (Swagger UI)
- Modular project structure
- CORS middleware
- Async/await support
- UV package manager for fast dependency resolution
- Comprehensive test suite
- Python 3.13.3+
- PostgreSQL
- UV package manager
The project follows a modular structure suitable for large applications with clear separation of concerns between models, schemas, routes, and business logic.
# On macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Or with pip
pip install uvClone the repository:
git clone <repository-url>
cd health-apiInstall dependencies:
uv syncSet up environment variables:
cp .env.example .env
# Edit .env with your database credentials and secret keyAlembic is used for handling database migrations. All commands below assume you are in the project root directory.
But first off all create up a PostgreSQL database:
CREATE DATABASE health_db;uv run alembic revision --autogenerate -m "Your migration message"uv run alembic upgrade headStart the application (choose one):
uv run uvicorn app.main:app --host 0.0.0.0 --port 5000 --reload
# Or use the convenience script
chmod +x scripts/start.sh
./scripts/start.shThese commands are not required during setup but are useful for managing migrations in the future during development.
uv run alembic downgrade -1uv run alembic currentuv run alembic historyFor more Alembic commands and usage, see the Alembic documentation.
Install development dependencies:
uv sync --group devRun tests:
uv run pytest
# Or use the test script
chmod +x scripts/test.sh
./scripts/test.shCode formatting:
uv run black app/ tests/
uv run isort app/ tests/Type checking:
uv run mypy app/Build and run with Docker Compose:
docker-compose up --buildThis will start both the PostgreSQL database and the FastAPI application.
Once the application is running, visit:
- Swagger UI: http://localhost:5000/docs
- ReDoc: http://localhost:5000/redoc
- MCP ping: check on terminal with the following command
curl -H "Authorization: Bearer <your_bearer_token>"
-H "Accept: text/event-stream" http://localhost:5000/mcpCreate a new migration:
uv run alembic revision --autogenerate -m "Description of changes"
# Or use the migration script
chmod +x scripts/migrate.sh
./scripts/migrate.sh "Description of changes"Apply migrations:
uv run alembic upgrade head- Register a new user:
POST /auth/register - Login to get access token:
POST /auth/login - Use the token in Authorization header:
Bearer <token>
After obtaining a JWT access token, you can access the following endpoints:
πΌοΈ Click to see the Health Microservice API endpoints
GET /doctorsβ List all doctorsGET /doctors/{doctor_id}β Get a specific doctor by IDPOST /doctorsβ Create a new doctorPUT /doctors/{doctor_id}β Update a doctor's informationDELETE /doctors/{doctor_id}β Delete a doctor
GET /patientsβ List all patientsGET /patients/{patient_id}β Get a specific patient by IDPOST /patientsβ Create a new patientPUT /patients/{patient_id}β Update a patient's informationDELETE /patients/{patient_id}β Delete a patient
GET /medical-recordsβ List all medical records (optionally filter by patient)GET /medical-records/{record_id}β Get a specific medical record by IDPOST /medical-recordsβ Create a new medical recordPUT /medical-records/{record_id}β Update a medical recordDELETE /medical-records/{record_id}β Delete a medical record
GET /appointmentsβ List all appointmentsGET /appointments/{appointment_id}β Get a specific appointment by IDPOST /appointmentsβ Create a new appointmentPUT /appointments/{appointment_id}β Update an appointmentDELETE /appointments/{appointment_id}β Delete an appointment
POST /telemedicine/visits/β Create a new virtual visitGET /telemedicine/visits/β List all virtual visitsGET /telemedicine/visits/{visit_id}β Get a specific virtual visit by IDPOST /telemedicine/chats/β Create a new chat logGET /telemedicine/chats/β List all chat logsGET /telemedicine/chats/{chat_id}β Get a specific chat log by IDPOST /telemedicine/videos/β Create a new video sessionGET /telemedicine/videos/β List all video sessionsGET /telemedicine/videos/{video_id}β Get a specific video session by ID
POST /lab/orders/β Create a new lab orderGET /lab/orders/β List all lab ordersGET /lab/orders/{order_id}β Get a specific lab order by IDPOST /lab/results/β Create a new lab resultGET /lab/results/β List all lab resultsGET /lab/results/{result_id}β Get a specific lab result by IDPOST /lab/images/β Create a new diagnostic imageGET /lab/images/β List all diagnostic imagesGET /lab/images/{image_id}β Get a specific diagnostic image by ID
POST /referral/requests/β Create a new referral requestGET /referral/requests/β List all referral requestsGET /referral/requests/{request_id}β Get a specific referral request by IDPOST /referral/statuses/β Create a new referral statusGET /referral/statuses/β List all referral statusesGET /referral/statuses/{status_id}β Get a specific referral status by IDPOST /referral/notes/β Create a new specialist noteGET /referral/notes/β List all specialist notesGET /referral/notes/{note_id}β Get a specific specialist note by ID
POST /pharmacy/medications/β Create a new medicationGET /pharmacy/medications/β List all medicationsGET /pharmacy/medications/{med_id}β Get a specific medication by IDPOST /pharmacy/prescriptions/β Create a new prescriptionGET /pharmacy/prescriptions/β List all prescriptionsGET /pharmacy/prescriptions/{pres_id}β Get a specific prescription by IDPOST /pharmacy/orders/β Create a new pharmacy orderGET /pharmacy/orders/β List all pharmacy ordersGET /pharmacy/orders/{order_id}β Get a specific pharmacy order by ID
POST /insurance/plans/β Create a new insurance planGET /insurance/plans/β List all insurance plansGET /insurance/plans/{plan_id}β Get a specific insurance plan by IDPOST /insurance/claims/β Create a new insurance claimGET /insurance/claims/β List all insurance claimsGET /insurance/claims/{claim_id}β Get a specific insurance claim by IDPOST /insurance/payments/β Create a new paymentGET /insurance/payments/β List all paymentsGET /insurance/payments/{payment_id}β Get a specific payment by IDPOST /insurance/invoices/β Create a new invoiceGET /insurance/invoices/β List all invoicesGET /insurance/invoices/{invoice_id}β Get a specific invoice by ID
All these endpoints require the Authorization: Bearer header. Refer to the interactive API docs at /docs for detailed request/response schemas and try out the endpoints interactively.
UV provides fast dependency resolution and installation. Useful commands:
uv sync- Install dependencies from lock fileuv add <package>- Add a new dependencyuv remove <package>- Remove a dependencyuv run <command>- Run command in virtual environmentuv lock- Update the lock file
Integration with MCP is done by using the
FastApiMCP class from the fastapi_mcp package.
The MCP server is mounted to the FastAPI
application using the mount method.
windsurf settings,
{
"mcpServers": {
"health-api": {
"serverUrl": "http://localhost:5000/mcp",
"headers": {
"Authorization": "Bearer
<put_your_bearer_token_here>"
}
}
}
}vscode or cursor settings,
{
"servers": {
"health-api": {
"url": "http://localhost:5000/mcp",
"headers": {
"Authorization": "Bearer
<put_your_bearer_token_here>"
}
}
}
}Note: I haven't tested on vscode or cursor yet.
MIT License
- Add more test cases
- Add more features
- Add more documentation
- Add more security features
- Add more logging
- Add more monitoring
- Add more performance optimization
- Install development dependencies:
uv sync --group dev - Make your changes
- Run tests:
uv run pytest - Format code:
uv run black app/ tests/ - Submit a pull request
See more at [Contributing](https://github.com/ AlwaysSany/health-api/blob/main/CONTRIBUTING.md).
- Author: Sany Ahmed
- Email: sany2k8@gmail.com

