compliance_tools_refactored.pyโข2.26 kB
"""
Compliance Tools - Refactored version with project root enforcement
This demonstrates the proper way to refactor tools for project root discipline.
"""
from pathlib import Path
from typing import Any, Dict
from server.utils.base_tool import BaseMCPTool
from utils.security import SecurityManager
class ComplianceToolsRefactored(BaseMCPTool):
"""Compliance validation tools with project root enforcement."""
def __init__(self, security_manager: SecurityManager):
"""Initialize compliance tools with proper project root enforcement."""
super().__init__(security_manager)
async def validate_compliance(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
"""
Validate project compliance with security and regulatory standards.
Args:
arguments: Must contain project_root or use environment variables
Returns:
Compliance validation results
Raises:
ProjectRootError: If no valid project root can be resolved
"""
# This will raise ProjectRootError if no project root provided - let it bubble up
normalized = self.normalize_inputs(arguments)
project_root_str = self.resolve_project_root(normalized)
try:
# Extract parameters
compliance_type = normalized.get("compliance_type", "gdpr")
# Ensure we're not working in server CWD
from server.utils.no_cwd_guard import assert_not_server_cwd
assert_not_server_cwd(project_root_str)
# Perform compliance validation
results = {
"project_root": project_root_str,
"compliance_type": compliance_type,
"validation_successful": True,
"findings": [],
}
# Log audit event
self.security_manager.log_audit_event(
"validate_compliance",
f"compliance_type:{compliance_type}",
f"project_root:{project_root_str}",
)
return {"success": True, "compliance_results": results}
except (OSError, ValueError, RuntimeError) as e:
return {"success": False, "error": f"Compliance validation failed: {str(e)}"}