Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ce53d86
feat: implement optional OAuth 2.1 authentication with modular middle…
nickytonline Jul 31, 2025
85c050b
feat(auth): Implement OAuth 2.1 support with gateway and built-in modes
nickytonline Aug 2, 2025
05f9765
feat(auth): integrate oauth2-server for OAuth 2.1 support with PKCE a…
nickytonline Aug 2, 2025
4c9f0dc
feat: complete OAuth integration with comprehensive unit tests
nickytonline Aug 2, 2025
26c291c
feat: implement MCP-compliant OAuth proxy pattern for full mode
nickytonline Aug 2, 2025
ddb03f0
wip
nickytonline Aug 3, 2025
aa5e5a5
feat: add OAuth 2.1 dependencies for enhanced authentication support
nickytonline Aug 3, 2025
64fd0e4
feat(auth): Refactor authentication flow and integrate OAuth provider
nickytonline Aug 3, 2025
d2d1c67
refactor(auth): use URL constructor for endpoint generation in OAuth …
nickytonline Aug 3, 2025
d92cefb
refactor(config): cleaned up base url logic
nickytonline Aug 4, 2025
ec1de9c
docs: update authentication modes in README and .env.example for clarity
nickytonline Aug 4, 2025
1a7749d
refactor(auth): replace AUTH_MODE with ENABLE_AUTH for streamlined au…
nickytonline Aug 4, 2025
7a2796e
docs: update OAuth configuration details and usage instructions in mu…
nickytonline Aug 4, 2025
82d4ba5
docs: add optional OAUTH_REDIRECT_URI configuration to README for OAu…
nickytonline Aug 4, 2025
0b808c9
fix(mcp): use JSON-RPC 2.0 format for error responses
nickytonline Aug 4, 2025
14459ac
security(oauth): enable client auth and remove token logging
nickytonline Aug 4, 2025
ef42419
refactor(oauth): remove hardcoded demo values and improve user ID gen…
nickytonline Aug 4, 2025
10ec97e
feat(security): add rate limiting to OAuth endpoints
nickytonline Aug 4, 2025
cf39df2
feat(security): apply rate limiting to OAuth endpoints
nickytonline Aug 4, 2025
b277fb1
feat(mcp): implement comprehensive session cleanup mechanism
nickytonline Aug 4, 2025
048b672
docs(readme): update production storage limitation section for clarity
nickytonline Aug 4, 2025
08b66db
feat(docs): enhance authentication modes section in README for clarity
nickytonline Aug 4, 2025
f3a6def
feat(tests): add unit tests for OAuthProvider functionality
nickytonline Aug 4, 2025
a366546
chore(gitignore): add macOS metadata to .gitignore
nickytonline Aug 5, 2025
7715583
test (config): updated tests for config
nickytonline Aug 5, 2025
bebacb3
refactor: ran prettier
nickytonline Aug 5, 2025
0dddba6
refactor: remove unused code
nickytonline Aug 5, 2025
07d7036
test: add unit tests for OAuth discovery endpoints
nickytonline Aug 5, 2025
f5f8df5
fix: update dev script to handle .env file existence
nickytonline Oct 8, 2025
4982461
wip
nickytonline Oct 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion .cursorrules
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ This is a TypeScript template for building Model Context Protocol (MCP) servers.
- Defines all available MCP tools with their JSON schemas
- Routes tool calls to registered tool handlers
- Handles error responses in MCP format
- Conditionally applies OAuth middleware based on configuration
- **`src/config.ts`** - Environment configuration with validation using Zod
- **`src/logger.ts`** - Structured logging with Pino (OpenTelemetry compatible)
- **`src/lib/utils.ts`** - Utility functions for MCP response formatting
- **`src/auth/`** - Optional OAuth 2.1 authentication module (can be completely removed)

### Template MCP Tools Available

Expand Down Expand Up @@ -101,6 +103,16 @@ The following environment variables are supported (see `src/config.ts`):
- `SERVER_VERSION` - Server version (default: 1.0.0)
- `LOG_LEVEL` - Logging level (error/warn/info/debug, default: info)

### OAuth Configuration (Optional)

- `ENABLE_AUTH` - Enable OAuth 2.1 authentication (default: false)
- `OAUTH_ISSUER` - OAuth issuer URL (required if auth enabled)
- `OAUTH_CLIENT_ID` - OAuth client ID (required if auth enabled)
- `OAUTH_CLIENT_SECRET` - OAuth client secret (required if auth enabled)
- `OAUTH_AUDIENCE` - Expected audience in JWT tokens (optional but recommended)
- `OAUTH_SCOPE` - OAuth scope (default: "openid profile email")
- `OAUTH_REDIRECT_URI` - OAuth redirect URI (optional, defaults to BASE_URL/callback)

## Coding Guidelines

- Follow existing patterns in the codebase
Expand All @@ -119,4 +131,49 @@ The following environment variables are supported (see `src/config.ts`):
- Include relevant context in log messages (user IDs, session IDs, etc.)
- Log structured data as the second parameter: `logger.info("message", { key: value })`
- Error logs should include error details: `logger.error("Error message", { error: error.message })`
- The logger automatically includes trace correlation when OpenTelemetry is configured
- The logger automatically includes trace correlation when OpenTelemetry is configured

## OAuth Implementation

### Simple Binary Configuration

The template includes optional OAuth 2.1 authentication with a simple on/off approach:

- **Default**: No authentication required - server runs immediately with `ENABLE_AUTH=false`
- **Enable When Needed**: Set `ENABLE_AUTH=true` and provide OAuth configuration
- **Modular Design**: All OAuth code is in `src/auth/` directory
- **Zero Impact When Disabled**: No performance overhead when authentication is disabled
- **Easy Removal**: Delete `src/auth/` directory and remove auth import from `src/index.ts`

### Use Cases

**Authentication Disabled** (`ENABLE_AUTH=false` or omitted):
- Public MCP servers
- Gateway-protected deployments (Pomerium, nginx with auth, etc.)
- Development and testing
- Internal corporate networks with perimeter security

**Authentication Enabled** (`ENABLE_AUTH=true`):
- Direct OAuth 2.1 with token validation
- Self-contained secure deployment
- Production servers without gateway infrastructure

### Quick Setup

To enable authentication, add to your `.env`:
```bash
ENABLE_AUTH=true
OAUTH_ISSUER=https://your-provider.com
OAUTH_CLIENT_ID=your-client-id
OAUTH_CLIENT_SECRET=your-client-secret
```

### Removing OAuth

To completely remove OAuth support:

1. Delete the `src/auth/` directory
2. Remove the auth import and middleware lines from `src/index.ts`
3. Remove OAuth environment variables from `src/config.ts`

The core MCP server functionality is completely independent of the authentication layer.
66 changes: 66 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Server Configuration
PORT=3000
NODE_ENV=development
SERVER_NAME=mcp-typescript-template
SERVER_VERSION=1.0.0
LOG_LEVEL=info

# Base URL for the server (used for OAuth redirects and discovery endpoints)
# Defaults to http://localhost:PORT if not set (where PORT is the configured port)
# BASE_URL=http://localhost:3000

# ============================================================================
# Authentication Configuration (Optional)
# ============================================================================
# Default: No authentication required - server runs immediately
# Enable when you need OAuth 2.1 authentication with token validation
# ENABLE_AUTH=false

# When ENABLE_AUTH=true, configure your OAuth provider:
# ENABLE_AUTH=true
# OAUTH_ISSUER=https://your-provider.com
# OAUTH_CLIENT_ID=your-client-id
# OAUTH_CLIENT_SECRET=your-client-secret

# Additional OAuth settings (optional)
# OAUTH_AUDIENCE=your-api-identifier # For token audience validation
# OAUTH_SCOPE=openid profile email # Default scope
# OAUTH_REDIRECT_URI=http://localhost:3000/callback # Defaults to BASE_URL/callback

# ============================================================================
# Common OAuth Provider Examples
# ============================================================================

# Auth0 Example:
# ENABLE_AUTH=true
# OAUTH_ISSUER=https://your-domain.auth0.com
# OAUTH_CLIENT_ID=your-auth0-client-id
# OAUTH_CLIENT_SECRET=your-auth0-client-secret
# OAUTH_AUDIENCE=your-api-identifier

# Okta Example:
# ENABLE_AUTH=true
# OAUTH_ISSUER=https://your-domain.okta.com
# OAUTH_CLIENT_ID=your-okta-client-id
# OAUTH_CLIENT_SECRET=your-okta-client-secret

# Google Example:
# ENABLE_AUTH=true
# OAUTH_ISSUER=https://accounts.google.com
# OAUTH_CLIENT_ID=your-google-client-id.apps.googleusercontent.com
# OAUTH_CLIENT_SECRET=your-google-client-secret

# ============================================================================
# Use Cases
# ============================================================================
#
# Auth Disabled (ENABLE_AUTH=false or omitted):
# - Public MCP servers
# - Gateway-protected deployments (Pomerium, nginx with auth, etc.)
# - Development and testing
# - Internal corporate networks with perimeter security
#
# Auth Enabled (ENABLE_AUTH=true):
# - Direct OAuth 2.1 with token validation
# - Self-contained secure deployment
# - Production servers without gateway infrastructure
33 changes: 32 additions & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ See `src/config.ts` for all supported environment variables:
- `SERVER_VERSION` - Server version
- `LOG_LEVEL` - Logging level (error/warn/info/debug)

### OAuth Configuration (Optional)

- `ENABLE_AUTH` - Enable OAuth 2.1 authentication (default: false)
- `OAUTH_ISSUER` - OAuth issuer URL (required if auth enabled)
- `OAUTH_CLIENT_ID` - OAuth client ID (required if auth enabled)
- `OAUTH_CLIENT_SECRET` - OAuth client secret (required if auth enabled)
- `OAUTH_AUDIENCE` - Expected audience in JWT tokens (optional but recommended)
- `OAUTH_SCOPE` - OAuth scope (default: "openid profile email")
- `OAUTH_REDIRECT_URI` - OAuth redirect URI (optional, defaults to BASE_URL/callback)

## Common Patterns

### Adding a New MCP Tool
Expand Down Expand Up @@ -134,9 +144,30 @@ const port = config.PORT;
const serverName = config.SERVER_NAME;
```

## Authentication

### Simple Binary Configuration

The template includes optional OAuth 2.1 authentication:

- **Default**: No authentication required (`ENABLE_AUTH=false`)
- **Enable when needed**: Set `ENABLE_AUTH=true` and provide OAuth config
- **Use cases**: Public servers (auth disabled) or secure deployments (auth enabled)

### Quick Setup

To enable authentication, add to `.env`:
```bash
ENABLE_AUTH=true
OAUTH_ISSUER=https://your-provider.com
OAUTH_CLIENT_ID=your-client-id
OAUTH_CLIENT_SECRET=your-client-secret
```

## Important Notes

- **File extensions**: Use `.js` in import statements (TypeScript compilation requirement)
- **OpenTelemetry ready**: Logger automatically correlates with OTel traces when configured
- **Production ready**: Includes graceful shutdown, error handling, and structured logging
- **Template usage**: This is a template - customize for your specific MCP server needs
- **Template usage**: This is a template - customize for your specific MCP server needs
- **Authentication**: Server starts immediately with no auth setup required, add OAuth when needed
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ vite.config.ts.timestamp-*

# Claude Code local settings
.claude/

# macOS metadata
.DS_Store
49 changes: 48 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ This is a TypeScript template for building Model Context Protocol (MCP) servers.
- Defines all available MCP tools with their JSON schemas
- Routes tool calls to registered tool handlers
- Handles error responses in MCP format
- Conditionally applies OAuth middleware based on configuration
- **`src/config.ts`** - Environment configuration with validation using Zod
- **`src/logger.ts`** - Structured logging with Pino (OpenTelemetry compatible)
- **`src/lib/utils.ts`** - Utility functions for MCP response formatting
- **`src/auth/`** - Optional OAuth 2.1 authentication module (can be completely removed)

### Template MCP Tools Available

Expand Down Expand Up @@ -89,6 +91,16 @@ The following environment variables are supported (see `src/config.ts`):
- `SERVER_VERSION` - Server version (default: 1.0.0)
- `LOG_LEVEL` - Logging level (error/warn/info/debug, default: info)

### OAuth Configuration (Optional)

- `ENABLE_AUTH` - Enable OAuth 2.1 authentication (default: false)
- `OAUTH_ISSUER` - OAuth issuer URL (required if auth enabled)
- `OAUTH_CLIENT_ID` - OAuth client ID (required if auth enabled)
- `OAUTH_CLIENT_SECRET` - OAuth client secret (required if auth enabled)
- `OAUTH_AUDIENCE` - Expected audience in JWT tokens (optional but recommended)
- `OAUTH_SCOPE` - OAuth scope (default: "openid profile email")
- `OAUTH_REDIRECT_URI` - OAuth redirect URI (optional, defaults to BASE_URL/callback)

## Logging Best Practices

- Use appropriate log levels: `error`, `warn`, `info`, `debug`
Expand All @@ -108,4 +120,39 @@ When adding new tools to the MCP server:
4. Return responses in MCP content format with JSON stringified data
5. Handle errors gracefully and return appropriate error messages
6. Use structured logging to track tool usage: `logger.info("Tool executed", { toolName, args })`
7. Log errors with context: `logger.error("Tool execution failed", { toolName, error: error.message })`
7. Log errors with context: `logger.error("Tool execution failed", { toolName, error: error.message })`

## OAuth Implementation

### Simple Binary Configuration

The template includes optional OAuth 2.1 authentication with a simple on/off approach:

- **Default**: No authentication required - server runs immediately
- **Enable When Needed**: Set `ENABLE_AUTH=true` and provide OAuth config
- **Modular Design**: All OAuth code is in `src/auth/` directory
- **Zero Impact When Disabled**: No performance overhead when authentication is disabled
- **Easy Removal**: Delete `src/auth/` directory and remove auth import from `src/index.ts`

### Use Cases

**Authentication Disabled** (`ENABLE_AUTH=false` or omitted):
- Public MCP servers
- Gateway-protected deployments (Pomerium, nginx with auth, etc.)
- Development and testing
- Internal corporate networks with perimeter security

**Authentication Enabled** (`ENABLE_AUTH=true`):
- Direct OAuth 2.1 with token validation
- Self-contained secure deployment
- Production servers without gateway infrastructure

### Removing OAuth

To completely remove OAuth support:

1. Delete the `src/auth/` directory
2. Remove the auth import and middleware lines from `src/index.ts`
3. Remove OAuth environment variables from `src/config.ts`

The core MCP server functionality is completely independent of the authentication layer.
Loading
Loading