An example Model Context Protocol (MCP) server implementation for managing a healthcare database with PostgreSQL. This demo showcases patient management, provider scheduling, therapy sessions, and complex payment tracking including insurance claims and patient responsibilities.
-
Complete Healthcare Database Schema
- Patients with insurance information
- Healthcare providers with specialties
- Therapy/consultation sessions
- Complex payment tracking (copays, coinsurance, deductibles, cash pay, insurance reimbursements)
- Payment statuses (pending, paid, denied, partially paid, appealed)
-
Powerful MCP Tools
- Patient and provider management
- Session tracking and scheduling
- Payment and claims processing
- Financial reporting and analytics
- Outstanding payments tracking
- Denied claims management
-
MCP Resources
- System-wide statistics (patient, provider, session, and payment counts)
- TypeScript - Type-safe development
- Prisma - Modern ORM for PostgreSQL
- Model Context Protocol (MCP) - AI assistant integration
- PostgreSQL 16 - Reliable database
- Docker Compose - Easy database setup
- Zod - Runtime type validation
This project follows a modular architecture for maintainability and scalability:
src/index.ts
- Main MCP server setup, request handlers, and transport configurationsrc/definitions/
- MCP tool definitions with input schemas (the "what" tools do)src/schemas/
- Zod validation schemas for runtime type checkingsrc/tools/
- Tool implementation handlers organized by domain (the "how" tools work)- Each file exports functions that interact with the database via Prisma
- Handlers are registered in
tools/index.ts
src/resources/
- MCP resource definitions and read handlers for direct data access
This separation allows for:
- Easy addition of new tools (add definition + handler + schema)
- Clear separation of concerns
- Testable business logic
- Type safety throughout the stack
- Node.js 18+
- Docker and Docker Compose (for database)
- npm or yarn
cd postgres-mcp-server
npm install
npm run docker:up
This will start a PostgreSQL 16 container with the healthcare demo database.
npm run db:setup
This command will:
- Generate Prisma Client
- Push the schema to the database
- Seed with realistic healthcare data (6 patients, 4 providers, 13 sessions, various payments)
npm test
This runs a comprehensive test suite that verifies all MCP tools and resources are working correctly.
npm run dev
The server will start and listen on stdio for MCP protocol messages.
npm run docker:up
- Start PostgreSQL containernpm run docker:down
- Stop PostgreSQL containernpm run db:setup
- Initial database setup (generate + migrate + seed)npm run db:seed
- Re-seed database with sample datanpm run db:reset
- Clear and re-seed databasenpm run db:studio
- Open Prisma Studio (visual database browser)npm run db:generate
- Generate Prisma Clientnpm test
- Run comprehensive MCP server tests
- Personal information (name, email, phone, DOB, address)
- Insurance details (provider name, policy ID)
- Relationship to sessions
- Professional information (name, email, phone)
- Specialty and license number
- Hourly rate
- Relationship to sessions
- Patient and provider relationships
- Session details (date, duration, type)
- Status (scheduled, completed, cancelled, no-show)
- Clinical codes (ICD-10 diagnosis, CPT procedure)
- Total charges
- Related payments
- Session relationship
- Amount and payment type:
COPAY
- Fixed patient responsibilityCOINSURANCE
- Percentage patient responsibilityDEDUCTIBLE
- Patient deductible amountCASH_PAY
- Full self-pay amountINSURANCE_REIMBURSEMENT
- Insurance payments
- Payment status:
PENDING
- Awaiting paymentPAID
- Payment receivedDENIED
- Insurance claim deniedPARTIALLY_PAID
- Partial payment receivedAPPEALED
- Claim under appeal
- Insurance claim tracking
- Denial reasons and notes
Search and retrieve patient records.
{
"searchTerm": "john", // Optional: search by name or email
"hasInsurance": true, // Optional: filter by insurance status
"limit": 10 // Optional: max results (default: 10)
}
Get comprehensive patient information including all sessions and payments.
{
"patientId": "uuid" // Required: patient ID
}
Retrieve provider records.
{
"specialty": "Psychology", // Optional: filter by specialty
"limit": 10 // Optional: max results (default: 10)
}
Flexible session querying with multiple filters.
{
"patientId": "uuid", // Optional: filter by patient
"providerId": "uuid", // Optional: filter by provider
"status": "completed", // Optional: scheduled|completed|cancelled|no-show
"startDate": "2024-09-01", // Optional: ISO date
"endDate": "2024-09-30", // Optional: ISO date
"limit": 20 // Optional: max results (default: 20)
}
Retrieve payment records with filtering.
{
"sessionId": "uuid", // Optional: filter by session
"type": "COPAY", // Optional: payment type
"status": "PENDING", // Optional: payment status
"limit": 20 // Optional: max results (default: 20)
}
Get all outstanding payments (pending, partially paid, denied).
{
"patientId": "uuid" // Optional: filter by patient
}
Retrieve denied insurance claims with reasons.
{
"includeAppealed": false // Optional: include appealed claims (default: false)
}
Generate comprehensive revenue analytics.
{
"startDate": "2024-09-01", // Optional: ISO date
"endDate": "2024-09-30" // Optional: ISO date
}
Create a new patient record.
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@email.com",
"phone": "(555) 123-4567",
"dateOfBirth": "1990-01-15",
"address": "123 Main St",
"city": "Boston",
"state": "MA",
"zipCode": "02108",
"insuranceName": "Blue Cross", // Optional
"insuranceId": "BCBS-123456" // Optional
}
Schedule or record a new session.
{
"patientId": "uuid",
"providerId": "uuid",
"sessionDate": "2024-10-15T10:00:00Z",
"durationMins": 60,
"sessionType": "Therapy",
"status": "scheduled",
"notes": "Initial consultation", // Optional
"diagnosisCode": "F41.1", // Optional: ICD-10
"procedureCode": "90834", // Optional: CPT
"totalCharge": 175.00
}
Update payment status and details.
{
"paymentId": "uuid",
"status": "PAID",
"paymentDate": "2024-10-08", // Optional: ISO date
"denialReason": "...", // Optional
"notes": "..." // Optional
}
healthcare://stats/overview
- System-wide statistics including patient, provider, session, and payment counts
The seed script creates realistic healthcare data:
- 6 Patients: Mix of insured and cash-pay patients
- 4 Providers: Various specialties (Psychology, Psychiatry, Family Therapy, Addiction Counseling)
- 13 Sessions: Past completed sessions and future scheduled appointments
- Multiple Payment Scenarios:
- Fully paid sessions with insurance + copay
- Pending insurance reimbursements
- Denied claims with reasons
- Partially paid deductibles
- Cash pay patients
- Appealed claims
- No-show fees
Add this server to your Claude Desktop configuration:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"postgres-healthcare": {
"command": "node",
"args": [
"/absolute/path/to/postgres-mcp-server/dist/index.js"
],
"env": {
"DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/healthcare_demo?schema=public"
}
}
}
}
Or for development (using tsx):
{
"mcpServers": {
"postgres-healthcare": {
"command": "npx",
"args": [
"tsx",
"/absolute/path/to/postgres-mcp-server/src/index.ts"
],
"env": {
"DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/healthcare_demo?schema=public"
}
}
}
}
After configuration, restart Claude Desktop and you'll see the PostgreSQL Healthcare server available.
Once connected, you can ask Claude:
- "Show me all patients with insurance"
- "What are the outstanding payments?"
- "Show me all denied insurance claims and why they were denied"
- "Generate a revenue report for September 2024"
- "Which patients have sessions scheduled in the future?"
- "Show me all therapy sessions for John Smith including payment details"
- "What's the total amount in pending insurance reimbursements?"
- "Create a new patient named Jane Wilson with email jane@email.com"
- "Show me the system statistics overview"
See EXAMPLE_QUERIES.md
for more comprehensive examples and use cases.
npm run build
npm start
View and edit data visually with Prisma Studio:
npm run db:studio
Reset database to fresh sample data:
npm run db:reset
postgres-mcp-server/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── definitions/
│ │ └── tools.ts # Tool definitions and schemas
│ ├── schemas/
│ │ └── index.ts # Zod validation schemas
│ ├── tools/
│ │ ├── index.ts # Tool handler registry
│ │ ├── patients.ts # Patient management tools
│ │ ├── providers.ts # Provider query tools
│ │ ├── sessions.ts # Session management tools
│ │ ├── payments.ts # Payment and claims tools
│ │ └── reports.ts # Financial reporting tools
│ └── resources/
│ └── index.ts # MCP resource definitions and handlers
├── prisma/
│ └── schema.prisma # Database schema
├── scripts/
│ ├── seed.ts # Database seeding script
│ ├── setup-db.ts # Initial setup script
│ ├── reset-db.ts # Database reset script
│ └── test-server.ts # MCP server test suite
├── docker-compose.yml # PostgreSQL container config
├── claude_desktop_config.example.json
├── EXAMPLE_QUERIES.md # Example queries for Claude
├── QUICK_START.md
├── PROJECT_OVERVIEW.md
├── package.json
├── tsconfig.json
└── README.md
Create a .env
file (already created with defaults):
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/healthcare_demo?schema=public"
For production, use a secure password and proper connection string.
If you can't connect to the database:
- Ensure Docker is running:
docker ps
- Check if PostgreSQL container is up:
docker ps | grep healthcare-postgres
- Restart the container:
npm run docker:down && npm run docker:up
- Verify DATABASE_URL in
.env
matches your setup
If port 5432 is already taken:
- Stop the conflicting service or
- Modify
docker-compose.yml
to use a different port:ports: - "5433:5432" # Use 5433 on host instead
- Update DATABASE_URL to:
postgresql://postgres:postgres@localhost:5433/...
To start completely fresh:
npm run docker:down
docker volume rm postgres-mcp-server_postgres-data # Remove old data
npm run docker:up
npm run db:setup
MIT
This is a demo project showcasing MCP server capabilities with PostgreSQL. Feel free to use it as a starting point for your own healthcare or database-backed MCP servers!