granular-software/template-docker-oauth-postgresql
If you are the rightful owner of template-docker-oauth-postgresql 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.
This template provides a production-ready MCP server with OAuth2.1 authentication and PostgreSQL database, containerized with Docker.
{{PROJECT_NAME}}
{{PROJECT_DESCRIPTION}}
Quick Start
-
Install dependencies
npm install -
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Initialize the database
npm run db:initThis will create the PostgreSQL database with all necessary tables, functions, and indexes.
-
Start development server
npm run dev -
Build for production
npm run build npm start
Database Setup
This template uses PostgreSQL. You must set a connection string and initialize the entire schema via the init script.
1) Configure the connection string
Option A — interactive (recommended):
npm run db:init
You will be prompted for DATABASE_URL (example: postgresql://postgres:postgres@localhost:5432/mcpresso). The script will write it to .env and run the schema setup.
Option B — manual:
- Copy env file:
cp .env.example .env - Edit
.envand set:
DATABASE_URL=postgresql://USER:PASS@HOST:5432/DB
- Initialize full schema:
node scripts/init-db.js
2) Database Structure
The initialization script creates the following tables:
- users - User accounts with UUID primary keys (email, username, hashed_password, scopes, profile)
- oauth_clients - OAuth client registry (redirect URIs, scopes, grant types)
- oauth_authorization_codes - Authorization codes (with PKCE fields)
- oauth_access_tokens - Access tokens with expiry
- oauth_refresh_tokens - Refresh tokens with expiry
- notes - Example resource (user-authored notes)
Database Features
- ✅ PostgreSQL with UUID primary keys - Scalable and secure
- ✅ Foreign key constraints - Maintains data integrity
- ✅ Optimized indexes - Fast lookups for common queries
- ✅ Automatic timestamps - Created/updated tracking with triggers
- ✅ OAuth integration - Session and token management
- ✅ Database functions and triggers - Automatic updated_at maintenance
Database Requirements
- PostgreSQL 12+ with UUID extension
- Connection string in
DATABASE_URLenvironment variable - SSL support for production deployments
Features
- OAuth2.1 authentication with PostgreSQL
- User management and sessions
- Notes resource with author relationships
- TypeScript support
- Development and production builds
- Environment variable configuration
- Docker support with docker-compose
Project Structure
src/
├── server.ts # Main server file
├── auth/ # OAuth configuration
│ └── oauth.ts
├── resources/ # MCP resources
│ ├── schemas/ # Resource schemas
│ │ └── Note.ts # Note data model
│ └── handlers/ # Resource handlers
│ └── note.ts # Notes with author relationships
└── storage/ # Database layer
└── postgres-storage.ts
Environment Variables
| Variable | Description | Required | Default |
|---|---|---|---|
| PORT | Server port | No | 3000 |
| SERVER_URL | Base URL of your server | Yes | - |
| JWT_SECRET | Secret key for JWT tokens | Yes | - |
| DATABASE_URL | PostgreSQL connection string | Yes | - |
| NODE_ENV | Environment mode | No | development |
JWT Secret
Generate a secure JWT secret for token signing.
Option A — script (uses openssl under the hood):
npm run secret:generate
Option B — manual (with openssl):
JWT_SECRET=$(openssl rand -hex 64)
echo "JWT_SECRET=$JWT_SECRET" >> .env # or replace existing JWT_SECRET in .env
Keep this value secret. Rotating it will invalidate existing tokens.
Development
npm run dev- Start development server with hot reloadnpm run build- Build for productionnpm run typecheck- Type check without buildingnpm run db:init- Interactive database setup (prompts for connection string and initializes the full schema)npm run secret:generate- Generate secure JWT secretnpm run user:create- Create a new user account
Create a Test User
After the DB is initialized and JWT_SECRET is set, create a user:
npm run user:create "John Doe" "john@example.com" "strongpassword"
The script validates uniqueness and hashes the password before insert.
Docker Deployment
This template includes Docker support:
# Build and run with Docker Compose
docker-compose up --build
# Or build manually
npm run docker:build
npm run docker:run
License
MIT