Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
094a320
feat: implement optional OAuth 2.1 authentication with modular middle…
nickytonline Jul 31, 2025
da8a72e
feat(auth): Implement OAuth 2.1 support with gateway and built-in modes
nickytonline Aug 2, 2025
bc09dc2
feat(auth): integrate oauth2-server for OAuth 2.1 support with PKCE a…
nickytonline Aug 2, 2025
c1b67a2
feat: complete OAuth integration with comprehensive unit tests
nickytonline Aug 2, 2025
d6d4cad
feat: implement MCP-compliant OAuth proxy pattern for full mode
nickytonline Aug 2, 2025
cc5794b
wip
nickytonline Aug 3, 2025
f93499c
feat: add OAuth 2.1 dependencies for enhanced authentication support
nickytonline Aug 3, 2025
75bf87b
feat(auth): Refactor authentication flow and integrate OAuth provider
nickytonline Aug 3, 2025
b61f28d
refactor(auth): use URL constructor for endpoint generation in OAuth …
nickytonline Aug 3, 2025
4898446
refactor(config): cleaned up base url logic
nickytonline Aug 4, 2025
def04fe
docs: update authentication modes in README and .env.example for clarity
nickytonline Aug 4, 2025
e279e22
refactor(auth): replace AUTH_MODE with ENABLE_AUTH for streamlined au…
nickytonline Aug 4, 2025
ee40ad9
docs: update OAuth configuration details and usage instructions in mu…
nickytonline Aug 4, 2025
6b23482
docs: add optional OAUTH_REDIRECT_URI configuration to README for OAu…
nickytonline Aug 4, 2025
6af216a
fix(mcp): use JSON-RPC 2.0 format for error responses
nickytonline Aug 4, 2025
8d5ce10
security(oauth): enable client auth and remove token logging
nickytonline Aug 4, 2025
8209ff6
refactor(oauth): remove hardcoded demo values and improve user ID gen…
nickytonline Aug 4, 2025
9e1d0e9
feat(security): add rate limiting to OAuth endpoints
nickytonline Aug 4, 2025
9d32645
feat(security): apply rate limiting to OAuth endpoints
nickytonline Aug 4, 2025
5379518
feat(mcp): implement comprehensive session cleanup mechanism
nickytonline Aug 4, 2025
d911654
docs(readme): update production storage limitation section for clarity
nickytonline Aug 4, 2025
361e10c
feat(docs): enhance authentication modes section in README for clarity
nickytonline Aug 4, 2025
ab59e75
feat(tests): add unit tests for OAuthProvider functionality
nickytonline Aug 4, 2025
a6f6af1
chore(gitignore): add macOS metadata to .gitignore
nickytonline Aug 5, 2025
ff28a45
test (config): updated tests for config
nickytonline Aug 5, 2025
78eb8d8
refactor: ran prettier
nickytonline Aug 5, 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 @@ -140,3 +140,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