Crates.io | pulseengine-mcp-security-middleware |
lib.rs | pulseengine-mcp-security-middleware |
version | 0.10.0 |
created_at | 2025-08-26 04:15:45.089908+00 |
updated_at | 2025-08-26 04:15:45.089908+00 |
description | Zero-configuration security middleware for MCP servers with Axum integration |
homepage | https://github.com/pulseengine/mcp |
repository | https://github.com/pulseengine/mcp |
max_upload_size | |
id | 1810506 |
size | 176,082 |
Zero-configuration security middleware for MCP servers with Axum integration.
This crate provides a simple, secure-by-default authentication and authorization middleware system for MCP servers. It transforms security from a complex, multi-crate barrier into a competitive advantage with minimal configuration.
middleware::from_fn
for seamless integrationAdd to your Cargo.toml
:
[dependencies]
pulseengine-mcp-security-middleware = "0.10.0"
axum = { version = "0.7", features = ["macros"] }
tokio = { version = "1.0", features = ["full"] }
use pulseengine_mcp_security_middleware::*;
use axum::{Router, routing::get, middleware::from_fn};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Zero-config development setup
let security_config = SecurityConfig::development();
let middleware = security_config.create_middleware().await?;
let app = Router::new()
.route("/", get(|| async { "Hello, secure world!" }))
.layer(from_fn(move |req, next| {
let middleware = middleware.clone();
async move { middleware.process(req, next).await }
}));
// Server setup...
Ok(())
}
use pulseengine_mcp_security_middleware::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Production-ready security
let security_config = SecurityConfig::production()
.with_api_key(std::env::var("MCP_API_KEY")?)
.with_jwt_secret(std::env::var("MCP_JWT_SECRET")?);
let middleware = security_config.create_middleware().await?;
// Use with your MCP server...
Ok(())
}
let config = SecurityConfig::development();
// API key auto-generated and logged for testing
let config = SecurityConfig::staging();
// Balanced security for testing environments
let config = SecurityConfig::production()
.with_api_key(env::var("MCP_API_KEY")?)
.with_jwt_secret(env::var("MCP_JWT_SECRET")?);
Zero CLI tools required - configure everything via environment variables:
# Security profile (development, staging, production)
MCP_SECURITY_PROFILE=production
# Auto-generated if not provided (development/staging only)
MCP_API_KEY=your-api-key-here
MCP_JWT_SECRET=your-jwt-secret-here
# CORS configuration
MCP_CORS_ORIGIN=https://yourdomain.com
# or for multiple origins:
MCP_CORS_ORIGIN=https://app1.com,https://app2.com
# Rate limiting
MCP_RATE_LIMIT=100/min
# Security features
MCP_REQUIRE_HTTPS=true
MCP_ENABLE_AUDIT_LOG=true
Feature | Development | Staging | Production |
---|---|---|---|
Authentication | Optional | Required | Strict |
Auto-Generate Keys | ✅ | ✅ | ❌ |
HTTPS Required | ❌ | ✅ | ✅ |
Rate Limiting | Disabled | 1000/min | 100/min |
CORS | Permissive | Localhost | Explicit |
Audit Logging | ✅ | ✅ | ✅ |
JWT Expiry | 24 hours | 1 hour | 15 minutes |
# In Authorization header
curl -H "Authorization: ApiKey mcp_your_key_here" https://api.example.com/
# In X-API-Key header
curl -H "X-API-Key: mcp_your_key_here" https://api.example.com/
curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." https://api.example.com/
Automatically adds security headers to all responses:
If you're currently using the complex multi-crate system:
// Complex setup with multiple CLI tools and crates
use pulseengine_mcp_auth::*;
use pulseengine_mcp_security::*;
use pulseengine_mcp_monitoring::*;
// ... extensive configuration ...
use pulseengine_mcp_security_middleware::*;
let config = SecurityConfig::development();
let middleware = config.create_middleware().await?;
See examples/hello-world-with-auth/
for a complete working example showing:
use pulseengine_mcp_macros::{mcp_server, mcp_tools};
use pulseengine_mcp_security_middleware::*;
#[mcp_server(name = "Secure MCP Server")]
#[derive(Default, Clone)]
pub struct SecureServer;
#[mcp_tools]
impl SecureServer {
pub async fn secure_operation(&self) -> anyhow::Result<String> {
// Your business logic here
// Security is handled transparently by middleware
Ok("Operation completed securely".to_string())
}
}
The middleware provides clear error responses:
401 Unauthorized
: Missing or invalid authentication403 Forbidden
: HTTPS required but not provided429 Too Many Requests
: Rate limit exceeded500 Internal Server Error
: Configuration or validation errorsmcp_
)Contributions welcome! This middleware was designed based on real-world production needs and feedback.
Priority areas:
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Transform security from complexity to competitive advantage with zero-configuration MCP security middleware.