File size: 8,224 Bytes
2ec0d39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Base application class for Secure AI Agents Suite
Provides common functionality for all agents including security, logging, and MCP integration.
"""

import json
import hashlib
import logging
import re
from datetime import datetime
from typing import Dict, List, Optional, Any
from abc import ABC, abstractmethod
import aiohttp
import asyncio


class SecurityMiddleware:
    """Handles security features: prompt injection defense, output sanitization, audit logging."""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.audit_logger = logging.getLogger(f"{__name__}.audit")
        
        # Common injection patterns
        self.injection_patterns = [
            r"ignore\s+previous\s+instructions",
            r"forget\s+everything\s+above",
            r"system\s*:\s*ignore",
            r"<system>",
            r"<?xml",
            r"<script",
            r"javascript:",
            r"eval\(",
            r"exec\(",
            r"__import__",
            r"subprocess",
            r"os\.system",
            r"shell\s*=\s*True"
        ]
    
    def detect_prompt_injection(self, prompt: str) -> bool:
        """Detect potential prompt injection attempts."""
        prompt_lower = prompt.lower()
        for pattern in self.injection_patterns:
            if re.search(pattern, prompt_lower):
                return True
        return False
    
    def sanitize_output(self, content: str) -> str:
        """Sanitize output to remove sensitive information."""
        # Remove potential sensitive patterns
        patterns = [
            (r'\b\d{16}\b', '[CREDIT_CARD_MASKED]'),  # Credit card numbers
            (r'\b\d{3}-\d{2}-\d{4}\b', '[SSN_MASKED]'),  # SSN
            (r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', '[EMAIL_MASKED]'),  # Emails
            (r'\b(?:\+?1[-.\s]?)?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}\b', '[PHONE_MASKED]'),  # Phone
        ]
        
        sanitized = content
        for pattern, replacement in patterns:
            sanitized = re.sub(pattern, replacement, sanitized)
        
        return sanitized
    
    def log_mcp_call(self, session_id: str, tool: str, request: Dict, response: Dict):
        """Log MCP calls for audit purposes."""
        audit_entry = {
            "timestamp": datetime.utcnow().isoformat(),
            "session_id": session_id,
            "tool": tool,
            "request_hash": hashlib.sha256(json.dumps(request, sort_keys=True).encode()).hexdigest(),
            "response_hash": hashlib.sha256(json.dumps(response, sort_keys=True).encode()).hexdigest(),
            "request_summary": {
                "tool": request.get("tool"),
                "parameters": list(request.get("parameters", {}).keys()) if request.get("parameters") else []
            }
        }
        
        self.audit_logger.info(json.dumps(audit_entry))


class MCPClient:
    """Client for communicating with MCP (Model Context Protocol) servers."""
    
    def __init__(self, mcp_server_url: str, config: Dict[str, Any]):
        self.mcp_server_url = mcp_server_url
        self.config = config
        self.session: Optional[aiohttp.ClientSession] = None
        self.available_tools: Dict[str, Any] = {}
    
    async def __aenter__(self):
        self.session = aiohttp.ClientSession()
        await self.discover_tools()
        return self
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self.session:
            await self.session.close()
    
    async def discover_tools(self):
        """Discover available tools from MCP server."""
        try:
            async with self.session.post(f"{self.mcp_server_url}/tools/list") as response:
                if response.status == 200:
                    tools_data = await response.json()
                    self.available_tools = {tool["name"]: tool for tool in tools_data.get("tools", [])}
                else:
                    logging.warning(f"Failed to discover tools: {response.status}")
        except Exception as e:
            logging.error(f"Error discovering tools: {e}")
    
    async def call_tool(self, tool_name: str, parameters: Dict[str, Any], session_id: str = "default") -> Dict[str, Any]:
        """Call a tool on the MCP server."""
        if tool_name not in self.available_tools:
            raise ValueError(f"Tool {tool_name} not available. Available tools: {list(self.available_tools.keys())}")
        
        request = {
            "tool": tool_name,
            "parameters": parameters
        }
        
        try:
            async with self.session.post(
                f"{self.mcp_server_url}/tools/call",
                json=request
            ) as response:
                if response.status == 200:
                    result = await response.json()
                    # Security middleware will log this call
                    return result
                else:
                    error_msg = f"MCP tool call failed: {response.status}"
                    return {"error": error_msg}
        except Exception as e:
            error_msg = f"MCP tool call exception: {str(e)}"
            return {"error": error_msg}


class BaseAgent(ABC):
    """Base class for all AI agents with security and MCP integration."""
    
    def __init__(self, name: str, description: str, mcp_server_url: str, config: Dict[str, Any]):
        self.name = name
        self.description = description
        self.mcp_server_url = mcp_server_url
        self.config = config
        self.security_middleware = SecurityMiddleware(config)
        self.session_counter = 0
        
        # Set up logging
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        )
    
    def _generate_session_id(self) -> str:
        """Generate a unique session ID for tracking."""
        self.session_counter += 1
        return f"{self.name}_{self.session_counter}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}"
    
    def _check_permissions(self, session_id: str, action: str) -> bool:
        """Check if user has permission for the requested action (RBAC)."""
        # Simple RBAC implementation - can be extended
        role = self.config.get("user_roles", {}).get(session_id, "basic_user")
        
        admin_actions = ["crm_admin", "ticket_admin", "calendar_admin"]
        if action in admin_actions and role not in ["admin", "enterprise_user"]:
            return False
        
        return True
    
    @abstractmethod
    async def process_request(self, user_input: str, session_id: str = None) -> str:
        """Process user input and return agent response."""
        pass
    
    async def handle_user_input(self, user_input: str) -> str:
        """Main handler for user input with security checks."""
        session_id = self._generate_session_id()
        
        # Security check - detect prompt injection
        if self.security_middleware.detect_prompt_injection(user_input):
            return "❌ Potential security threat detected. Request blocked."
        
        try:
            response = await self.process_request(user_input, session_id)
            
            # Sanitize output
            sanitized_response = self.security_middleware.sanitize_output(response)
            
            return sanitized_response
            
        except Exception as e:
            logging.error(f"Error processing request: {e}")
            return f"❌ An error occurred while processing your request: {str(e)}"
    
    def get_available_tools(self) -> List[str]:
        """Get list of available tools for this agent."""
        # This should be overridden by subclasses to return agent-specific tools
        return ["general_inquiry"]
    
    def get_status(self) -> Dict[str, Any]:
        """Get current status of the agent."""
        return {
            "name": self.name,
            "description": self.description,
            "mcp_server": self.mcp_server_url,
            "status": "active",
            "tools": self.get_available_tools(),
            "security_enabled": True,
            "audit_logging": True
        }