pulseengine-mcp-auth
Authentication and authorization framework for MCP servers
This crate provides authentication and authorization for MCP servers. It includes API key management, role-based access control, and security features like rate limiting and audit logging.
What This Provides
API Key Management:
- Secure key generation with cryptographic randomness
- Multiple key types (live, test) with different permissions
- Key expiration and rotation support
- Storage with file system permissions (600/700)
Role-Based Access:
- Predefined roles: Admin, Operator, Monitor, Device
- Custom role definitions with specific permissions
- Tool-level access control
- IP address whitelisting
Security Features:
- Rate limiting with configurable thresholds
- Failed attempt tracking and cooldown periods
- Request size limits
- Audit logging of all authentication events
Real-World Usage
This authentication system is currently used in production by the Loxone MCP Server where it:
- Manages API keys for different client types (admin tools, automation systems, monitoring)
- Enforces role-based access to 30+ home automation tools
- Provides audit trails for security compliance
- Integrates with HTTP and WebSocket transports
Quick Start
[]
= "0.2.0"
= "0.2.0"
= { = "1.0", = ["full"] }
Basic Usage
Setting Up Authentication
use ;
// Configure authentication
let mut config = default;
config.enabled = true;
config.require_api_key = true;
config.default_role = Monitor; // Least privileged by default
// Initialize the auth manager
let auth_manager = new.await?;
Creating API Keys
use ;
// Create an admin key
let request = CreateKeyRequest ;
let key_info = auth_manager.create_key.await?;
println!; // lmk_live_xxxx...
Validating Requests
use AuthRequest;
// In your request handler
let auth_request = AuthRequest ;
match auth_manager.validate_request.await
Current Status
Production-ready for basic to intermediate needs. The core authentication works well and has been tested in real deployment scenarios.
What's solid:
- ✅ API key generation and validation
- ✅ Role-based access control
- ✅ Rate limiting and security features
- ✅ File system storage with proper permissions
- ✅ Integration with HTTP transport layer
- ✅ Audit logging and security events
What could be improved:
- 🔧 Key rotation could be more automated
- 📝 More examples for different deployment scenarios
- 🧪 Testing utilities for auth scenarios
- 🔧 More granular permission systems
Key Management
Creating Different Types of Keys
// Admin key with full access
let admin_key = CreateKeyRequest ;
// Operator key for automation systems
let automation_key = CreateKeyRequest ;
// Monitor key for dashboards
let monitor_key = CreateKeyRequest ;
Managing Keys
// List all keys
let keys = auth_manager.list_keys.await?;
for key in keys
// Update key permissions
auth_manager.update_key_whitelist.await?;
// Revoke a key
auth_manager.revoke_key.await?;
Role-Based Access Control
Predefined Roles
use Role;
// Admin: Full access to all tools and management functions
Admin
// Operator: Can use tools but not manage keys
Operator
// Monitor: Read-only access to status and monitoring tools
Monitor
// Device: Limited access for IoT devices
Device
Custom Permissions
use ;
let custom_role = CustomRole ;
Security Features
Rate Limiting
let mut config = default;
config.rate_limit.max_requests_per_minute = 60;
config.rate_limit.max_failed_attempts = 5;
config.rate_limit.lockout_duration_minutes = 30;
Audit Logging
// Authentication events are automatically logged
// Check logs for security monitoring
auth_manager.get_audit_log.await?;
Integration with MCP Server
use ServerConfig;
use AuthConfig;
let mut auth_config = default;
auth_config.enabled = true;
let server_config = ServerConfig ;
// Authentication is handled automatically by the server
CLI Tool
The auth system includes a command-line tool for key management:
# Create a new key
# List all keys
# Revoke a key
Contributing
This authentication system grows from real deployment needs. The most valuable contributions are:
- Security improvements - Better cryptographic practices, security auditing
- Integration examples - How to integrate with different transport layers
- Management tools - Better CLI tools, web interfaces for key management
- Testing utilities - Helpers for testing authenticated endpoints
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Repository: https://github.com/avrabe/mcp-loxone