| Crates.io | mocopr-rbac |
| lib.rs | mocopr-rbac |
| version | 0.1.0 |
| created_at | 2025-08-13 23:39:53.214624+00 |
| updated_at | 2025-08-13 23:39:53.214624+00 |
| description | Role-based access control integration for MoCoPr MCP servers |
| homepage | https://github.com/ciresnave/mocopr |
| repository | https://github.com/ciresnave/mocopr |
| max_upload_size | |
| id | 1794268 |
| size | 151,254 |
Role-Based Access Control (RBAC) integration for MoCoPr MCP servers, built on the powerful role-system crate.
Add to your Cargo.toml:
[dependencies]
mocopr-rbac = "0.1"
mocopr-server = "0.1"
use mocopr_rbac::prelude::*;
use mocopr_server::McpServer;
#[tokio::main]
async fn main() -> Result<()> {
// Create RBAC middleware with default roles
let rbac = RbacMiddleware::builder()
.with_default_roles()
.with_audit_logging(true)
.build()
.await?;
// Build MCP server with RBAC
let server = McpServer::builder()
.name("Secure MCP Server")
.with_middleware(rbac)
.build()?;
server.run_stdio().await
}
let rbac = RbacMiddleware::builder()
.with_role("data_analyst", &[
"list:tools",
"call:tools:analytics/*",
"read:resources:data/*"
])
.with_role("admin", &[
"call:tools:*",
"read:resources:*",
"manage:server"
])
.build()
.await?;
use mocopr_rbac::context::ContextConditions;
let rbac = RbacMiddleware::builder()
.with_default_roles()
// Admin tools only during business hours
.with_conditional_permission(
"power_user",
"call:tools:admin/*",
ContextConditions::business_hours_only()
)
// Sensitive operations for high-trust clients only
.with_conditional_permission(
"admin",
"call:tools:dangerous/*",
|context| {
context.get("trust_level") == Some(&"high".to_string())
}
)
.build()
.await?;
Permissions follow the format action:resource_type:resource_id:
list:tools - List available toolscall:tools:calculator - Execute the calculator toolcall:tools:* - Execute any toolread:resources:file/* - Read any file resourceread:resources:data/sensitive.txt - Read specific filemanage:server - Server administrationWhen using .with_default_roles(), these roles are created:
list:toolslist:resourcescall:toolsread:resources*:tools*:resourceslist:promptsget:prompts*:* (super admin - access to everything)The system supports different types of subjects:
// Human users
let user = MocoPrSubject::user("alice");
// Automated services
let service = MocoPrSubject::service("data-processor");
// IoT devices
let device = MocoPrSubject::device("sensor-001");
// Groups
let group = MocoPrSubject::group("engineering-team");
// Custom types
let custom = MocoPrSubject::custom("ai-agent", "llm");
Context extractors provide runtime information for conditional permissions:
timestamp - Current timestamp (RFC3339)business_hours - "true" if 9 AM - 5 PMday_of_week - Day name (Monday, Tuesday, etc.)is_weekend - "true" on weekendsmethod - MCP method being calleduser_id - Subject identifierclient_ip - Client IP address (if provided)let rbac = RbacMiddleware::builder()
.with_context_extractor(
ExtendedContextExtractor::new()
.with_trust_level_extractor()
.with_location_extractor()
)
.build()
.await?;
To authenticate with an RBAC-enabled server, include auth parameters in requests:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "calculator",
"arguments": {"a": 5, "b": 3},
"auth": {
"subject_id": "alice",
"subject_type": "user"
},
"context": {
"trust_level": "high",
"location": "office"
}
}
}
use mocopr_rbac::config::RbacConfig;
// Load from file
let config = RbacConfig::from_file("rbac.json")?;
// Or create programmatically
let config = RbacConfig::production_template();
config.to_file("rbac.json")?;
// Development
let config = RbacConfig::development();
// Production
let config = RbacConfig::production_template();
// Set up inheritance chain: admin > manager > user > guest
rbac.add_role_inheritance("admin", "manager").await?;
rbac.add_role_inheritance("manager", "user").await?;
rbac.add_role_inheritance("user", "guest").await?;
// Elevate user to admin for 1 hour
rbac.elevate_role(
&user,
"admin",
Some(Duration::from_hours(1))
).await?;
When audit logging is enabled, all permission checks are logged:
INFO rbac: Permission check subject=alice action=call resource=calculator result=granted
WARN rbac: Permission check subject=bob action=call resource=admin/restart result=denied
See the examples/rbac-example directory for a complete working example demonstrating:
The RBAC middleware can be combined with other MoCoPr middleware:
let server = McpServer::builder()
.with_middleware(AuthMiddleware::jwt(&secret)) // Authentication
.with_middleware(rbac) // Authorization
.with_middleware(RateLimitMiddleware::new()) // Rate limiting
.with_middleware(LoggingMiddleware::new()) // Logging
.build()?;
Contributions are welcome! Please ensure that:
cargo testcargo fmtcargo clippyLicensed under either of:
at your option.