| 
 | 1 | +# 📊 Metadata Tracking & Audit Trails  | 
 | 2 | + | 
 | 3 | +MCP Gateway provides comprehensive metadata tracking for all entities (Tools, Resources, Prompts, Servers, Gateways) to enable enterprise-grade audit trails, compliance monitoring, and operational troubleshooting.  | 
 | 4 | + | 
 | 5 | +---  | 
 | 6 | + | 
 | 7 | +## 🎯 **Overview**  | 
 | 8 | + | 
 | 9 | +Every entity in MCP Gateway now includes detailed metadata about:  | 
 | 10 | +- **Who** created or modified the entity  | 
 | 11 | +- **When** the operation occurred  | 
 | 12 | +- **From where** (IP address, user agent)  | 
 | 13 | +- **How** it was created (UI, API, bulk import, federation)  | 
 | 14 | +- **Source tracking** for federated entities and bulk operations  | 
 | 15 | + | 
 | 16 | +---  | 
 | 17 | + | 
 | 18 | +## 📊 **Metadata Fields**  | 
 | 19 | + | 
 | 20 | +All entities include the following metadata fields:  | 
 | 21 | + | 
 | 22 | +| Category | Field | Description | Example Values |  | 
 | 23 | +|----------|-------|-------------|----------------|  | 
 | 24 | +| **Creation** | `created_by` | Username who created entity | `"admin"`, `"alice"`, `"anonymous"` |  | 
 | 25 | +| | `created_at` | Creation timestamp | `"2024-01-15T10:30:00Z"` |  | 
 | 26 | +| | `created_from_ip` | IP address of creator | `"192.168.1.100"`, `"10.0.0.1"` |  | 
 | 27 | +| | `created_via` | Creation method | `"ui"`, `"api"`, `"import"`, `"federation"` |  | 
 | 28 | +| | `created_user_agent` | Browser/client info | `"Mozilla/5.0"`, `"curl/7.68.0"` |  | 
 | 29 | +| **Modification** | `modified_by` | Last modifier username | `"bob"`, `"system"`, `"anonymous"` |  | 
 | 30 | +| | `modified_at` | Last modification timestamp | `"2024-01-16T14:22:00Z"` |  | 
 | 31 | +| | `modified_from_ip` | IP of last modifier | `"172.16.0.1"` |  | 
 | 32 | +| | `modified_via` | Modification method | `"ui"`, `"api"` |  | 
 | 33 | +| | `modified_user_agent` | Client of last change | `"HTTPie/2.4.0"` |  | 
 | 34 | +| **Source** | `import_batch_id` | Bulk import UUID | `"550e8400-e29b-41d4-a716-446655440000"` |  | 
 | 35 | +| | `federation_source` | Source gateway name | `"gateway-prod-east"` |  | 
 | 36 | +| | `version` | Change tracking version | `1`, `2`, `3`... |  | 
 | 37 | + | 
 | 38 | +---  | 
 | 39 | + | 
 | 40 | +## 🖥️ **Viewing Metadata**  | 
 | 41 | + | 
 | 42 | +### **Admin UI**  | 
 | 43 | + | 
 | 44 | +Metadata is displayed in the detail view modals for all entity types:  | 
 | 45 | + | 
 | 46 | +1. **Navigate** to any entity list (Tools, Resources, Prompts, Servers, Gateways)  | 
 | 47 | +2. **Click "View"** on any entity  | 
 | 48 | +3. **Scroll down** to the "Metadata" section  | 
 | 49 | + | 
 | 50 | +**Example metadata display:**  | 
 | 51 | +```  | 
 | 52 | +┌─ Metadata ──────────────────────────────────────┐  | 
 | 53 | +│ Created By:      admin                           │  | 
 | 54 | +│ Created At:      1/15/2024, 10:30:00 AM        │  | 
 | 55 | +│ Created From:    192.168.1.100                  │  | 
 | 56 | +│ Created Via:     ui                              │  | 
 | 57 | +│ Last Modified By: alice                          │  | 
 | 58 | +│ Last Modified At: 1/16/2024, 2:22:00 PM        │  | 
 | 59 | +│ Version:         3                               │  | 
 | 60 | +│ Import Batch:    N/A                             │  | 
 | 61 | +└─────────────────────────────────────────────────┘  | 
 | 62 | +```  | 
 | 63 | + | 
 | 64 | +### **API Responses**  | 
 | 65 | + | 
 | 66 | +All entity read endpoints include metadata fields in JSON responses:  | 
 | 67 | + | 
 | 68 | +```bash  | 
 | 69 | +# Get tool with metadata  | 
 | 70 | +curl -H "Authorization: Bearer $TOKEN" \  | 
 | 71 | +     http://localhost:4444/tools/abc123  | 
 | 72 | + | 
 | 73 | +{  | 
 | 74 | +  "id": "abc123",  | 
 | 75 | +  "name": "example_tool",  | 
 | 76 | +  "description": "Example tool",  | 
 | 77 | +  "createdBy": "admin",  | 
 | 78 | +  "createdAt": "2024-01-15T10:30:00Z",  | 
 | 79 | +  "createdFromIp": "192.168.1.100",  | 
 | 80 | +  "createdVia": "ui",  | 
 | 81 | +  "createdUserAgent": "Mozilla/5.0...",  | 
 | 82 | +  "modifiedBy": "alice",  | 
 | 83 | +  "modifiedAt": "2024-01-16T14:22:00Z",  | 
 | 84 | +  "version": 3,  | 
 | 85 | +  "importBatchId": null,  | 
 | 86 | +  "federationSource": null,  | 
 | 87 | +  ...  | 
 | 88 | +}  | 
 | 89 | +```  | 
 | 90 | + | 
 | 91 | +---  | 
 | 92 | + | 
 | 93 | +## 🔍 **Metadata by Source Type**  | 
 | 94 | + | 
 | 95 | +### **Manual Creation (UI/API)**  | 
 | 96 | +- `created_via`: `"ui"` or `"api"`  | 
 | 97 | +- `created_by`: Authenticated username  | 
 | 98 | +- `created_from_ip`: Client IP address  | 
 | 99 | +- `federation_source`: `null`  | 
 | 100 | +- `import_batch_id`: `null`  | 
 | 101 | + | 
 | 102 | +### **Bulk Import Operations**  | 
 | 103 | +- `created_via`: `"import"`  | 
 | 104 | +- `import_batch_id`: UUID linking related imports  | 
 | 105 | +- `created_by`: User who initiated import  | 
 | 106 | +- `federation_source`: `null`  | 
 | 107 | + | 
 | 108 | +### **Federation (MCP Server Discovery)**  | 
 | 109 | +- `created_via`: `"federation"`  | 
 | 110 | +- `federation_source`: Source gateway name  | 
 | 111 | +- `created_by`: User who registered the gateway  | 
 | 112 | +- `import_batch_id`: `null`  | 
 | 113 | + | 
 | 114 | +### **Legacy Entities (Pre-Metadata)**  | 
 | 115 | +- All metadata fields: `null`  | 
 | 116 | +- UI displays: `"Legacy Entity"`, `"Pre-metadata"`  | 
 | 117 | +- `version`: `1` (automatically assigned)  | 
 | 118 | + | 
 | 119 | +---  | 
 | 120 | + | 
 | 121 | +## 🛡️ **Authentication Compatibility**  | 
 | 122 | + | 
 | 123 | +Metadata tracking works seamlessly across all authentication modes:  | 
 | 124 | + | 
 | 125 | +### **With Authentication (`AUTH_REQUIRED=true`)**  | 
 | 126 | +```bash  | 
 | 127 | +# Example: User "admin" creates a tool  | 
 | 128 | +{  | 
 | 129 | +  "createdBy": "admin",  | 
 | 130 | +  "createdVia": "api",  | 
 | 131 | +  "createdFromIp": "192.168.1.100"  | 
 | 132 | +}  | 
 | 133 | +```  | 
 | 134 | + | 
 | 135 | +### **Without Authentication (`AUTH_REQUIRED=false`)**  | 
 | 136 | +```bash  | 
 | 137 | +# Example: Anonymous creation  | 
 | 138 | +{  | 
 | 139 | +  "createdBy": "anonymous",  | 
 | 140 | +  "createdVia": "api",  | 
 | 141 | +  "createdFromIp": "192.168.1.100"  | 
 | 142 | +}  | 
 | 143 | +```  | 
 | 144 | + | 
 | 145 | +### **JWT vs Basic Authentication**  | 
 | 146 | +- **JWT Authentication**: Extracts username from token payload (`username` or `sub` field)  | 
 | 147 | +- **Basic Authentication**: Uses provided username directly  | 
 | 148 | +- **Both formats handled gracefully** by the `extract_username()` utility  | 
 | 149 | + | 
 | 150 | +---  | 
 | 151 | + | 
 | 152 | +## 🔄 **Version Tracking**  | 
 | 153 | + | 
 | 154 | +Each entity maintains a version number that increments on modifications:  | 
 | 155 | + | 
 | 156 | +```bash  | 
 | 157 | +# Initial creation  | 
 | 158 | +POST /tools -> version: 1  | 
 | 159 | + | 
 | 160 | +# First update  | 
 | 161 | +PUT /tools/123 -> version: 2  | 
 | 162 | + | 
 | 163 | +# Second update  | 
 | 164 | +PUT /tools/123 -> version: 3  | 
 | 165 | +```  | 
 | 166 | + | 
 | 167 | +Version tracking helps identify:  | 
 | 168 | +- **Configuration drift** between environments  | 
 | 169 | +- **Change frequency** for troubleshooting  | 
 | 170 | +- **Rollback points** for recovery scenarios  | 
 | 171 | + | 
 | 172 | +---  | 
 | 173 | + | 
 | 174 | +## 📈 **Use Cases**  | 
 | 175 | + | 
 | 176 | +### **Security Auditing**  | 
 | 177 | +- Track who created/modified sensitive configurations  | 
 | 178 | +- Identify unauthorized changes by IP address  | 
 | 179 | +- Monitor bulk import operations for compliance  | 
 | 180 | + | 
 | 181 | +### **Operational Troubleshooting**  | 
 | 182 | +- Trace entity origins during incident response  | 
 | 183 | +- Identify batch operations that may have caused issues  | 
 | 184 | +- Understand federation dependencies between gateways  | 
 | 185 | + | 
 | 186 | +### **Compliance Reporting**  | 
 | 187 | +- Generate audit reports for regulatory requirements  | 
 | 188 | +- Track change management processes  | 
 | 189 | +- Demonstrate access controls and change attribution  | 
 | 190 | + | 
 | 191 | +### **Development & Testing**  | 
 | 192 | +- Identify test vs production entities  | 
 | 193 | +- Track deployment-specific configurations  | 
 | 194 | +- Monitor cross-environment migrations  | 
 | 195 | + | 
 | 196 | +---  | 
 | 197 | + | 
 | 198 | +## 🔧 **Configuration**  | 
 | 199 | + | 
 | 200 | +### **No Additional Setup Required**  | 
 | 201 | + | 
 | 202 | +Metadata tracking is **automatically enabled** for all new installations and upgrades:  | 
 | 203 | + | 
 | 204 | +- **Database migration** runs automatically on startup  | 
 | 205 | +- **Existing entities** show graceful fallbacks for missing metadata  | 
 | 206 | +- **No environment variables** needed - uses existing `AUTH_REQUIRED` setting  | 
 | 207 | + | 
 | 208 | +### **Proxy Support**  | 
 | 209 | + | 
 | 210 | +Metadata capture automatically handles reverse proxy scenarios:  | 
 | 211 | + | 
 | 212 | +```bash  | 
 | 213 | +# Respects X-Forwarded-For headers  | 
 | 214 | +X-Forwarded-For: 203.0.113.1, 192.168.1.1, 127.0.0.1  | 
 | 215 | +# Records: created_from_ip = "203.0.113.1" (original client)  | 
 | 216 | +```  | 
 | 217 | + | 
 | 218 | +### **Privacy Considerations**  | 
 | 219 | + | 
 | 220 | +The system captures IP addresses and user agents for audit purposes:  | 
 | 221 | + | 
 | 222 | +- **IP addresses**: Consider GDPR/privacy implications for EU deployments  | 
 | 223 | +- **User agents**: May contain personally identifiable information  | 
 | 224 | +- **Data retention**: Define policies for metadata archival  | 
 | 225 | +- **Access control**: Metadata follows same permissions as parent entity  | 
 | 226 | + | 
 | 227 | +---  | 
 | 228 | + | 
 | 229 | +## 🚀 **Migration Guide**  | 
 | 230 | + | 
 | 231 | +### **Upgrading Existing Deployments**  | 
 | 232 | + | 
 | 233 | +1. **Automatic Migration**  | 
 | 234 | +   ```bash  | 
 | 235 | +   # Migration runs automatically on startup  | 
 | 236 | +   # Or run manually:  | 
 | 237 | +   alembic upgrade head  | 
 | 238 | +   ```  | 
 | 239 | + | 
 | 240 | +2. **Verify Migration**  | 
 | 241 | +   - Check admin UI - all entities show metadata sections  | 
 | 242 | +   - API responses include new metadata fields  | 
 | 243 | +   - Legacy entities display gracefully  | 
 | 244 | + | 
 | 245 | +3. **No Downtime Required**  | 
 | 246 | +   - All metadata columns are nullable  | 
 | 247 | +   - Existing functionality unchanged  | 
 | 248 | +   - Gradual adoption of metadata features  | 
 | 249 | + | 
 | 250 | +### **Metadata Backfill (Optional)**  | 
 | 251 | + | 
 | 252 | +For enhanced audit trails, optionally backfill known metadata:  | 
 | 253 | + | 
 | 254 | +```sql  | 
 | 255 | +-- Backfill system-created entities  | 
 | 256 | +UPDATE tools SET  | 
 | 257 | +    created_by = 'system',  | 
 | 258 | +    created_via = 'migration',  | 
 | 259 | +    version = 1  | 
 | 260 | +WHERE created_by IS NULL;  | 
 | 261 | + | 
 | 262 | +-- Similar for other entity tables  | 
 | 263 | +UPDATE gateways SET created_by = 'system', created_via = 'migration', version = 1 WHERE created_by IS NULL;  | 
 | 264 | +UPDATE servers SET created_by = 'system', created_via = 'migration', version = 1 WHERE created_by IS NULL;  | 
 | 265 | +UPDATE prompts SET created_by = 'system', created_via = 'migration', version = 1 WHERE created_by IS NULL;  | 
 | 266 | +UPDATE resources SET created_by = 'system', created_via = 'migration', version = 1 WHERE created_by IS NULL;  | 
 | 267 | +```  | 
 | 268 | + | 
 | 269 | +---  | 
 | 270 | + | 
 | 271 | +## 🔮 **Future Enhancements**  | 
 | 272 | + | 
 | 273 | +### **Enhanced Audit Features**  | 
 | 274 | +- **Change history tracking** - Before/after state comparison  | 
 | 275 | +- **Metadata-based filtering** - Search entities by creator, date, source  | 
 | 276 | +- **Audit log export** - Generate compliance reports  | 
 | 277 | +- **Custom metadata fields** - User-defined entity attributes  | 
 | 278 | + | 
 | 279 | +### **Cross-Gateway Features**  | 
 | 280 | +- **Metadata synchronization** across federated gateways  | 
 | 281 | +- **Trust scoring** based on metadata quality  | 
 | 282 | +- **Provenance tracking** for complex federation scenarios  | 
 | 283 | + | 
 | 284 | +### **Analytics Integration**  | 
 | 285 | +- **Usage pattern analysis** from metadata  | 
 | 286 | +- **Creator activity dashboards**  | 
 | 287 | +- **Import/export trend monitoring**  | 
 | 288 | + | 
 | 289 | +---  | 
 | 290 | + | 
 | 291 | +## 📋 **API Examples**  | 
 | 292 | + | 
 | 293 | +### **Creating Entities with Metadata**  | 
 | 294 | + | 
 | 295 | +Metadata is captured automatically - no additional parameters needed:  | 
 | 296 | + | 
 | 297 | +```bash  | 
 | 298 | +# Create tool - metadata captured automatically  | 
 | 299 | +curl -X POST http://localhost:4444/tools \  | 
 | 300 | +  -H "Authorization: Bearer $TOKEN" \  | 
 | 301 | +  -H "Content-Type: application/json" \  | 
 | 302 | +  -d '{  | 
 | 303 | +    "name": "example_tool",  | 
 | 304 | +    "url": "http://example.com/api",  | 
 | 305 | +    "integration_type": "REST",  | 
 | 306 | +    "request_type": "GET"  | 
 | 307 | +  }'  | 
 | 308 | + | 
 | 309 | +# Response includes metadata  | 
 | 310 | +{  | 
 | 311 | +  "id": "abc123",  | 
 | 312 | +  "name": "example_tool",  | 
 | 313 | +  "createdBy": "admin",  | 
 | 314 | +  "createdAt": "2024-01-15T10:30:00Z",  | 
 | 315 | +  "createdVia": "api",  | 
 | 316 | +  "version": 1,  | 
 | 317 | +  ...  | 
 | 318 | +}  | 
 | 319 | +```  | 
 | 320 | + | 
 | 321 | +### **Filtering by Metadata (Future)**  | 
 | 322 | + | 
 | 323 | +```bash  | 
 | 324 | +# Future enhancement - filter by creator  | 
 | 325 | +GET /tools?created_by=admin  | 
 | 326 | + | 
 | 327 | +# Filter by creation method  | 
 | 328 | +GET /tools?created_via=federation  | 
 | 329 | + | 
 | 330 | +# Filter by date range  | 
 | 331 | +GET /tools?created_after=2024-01-01&created_before=2024-01-31  | 
 | 332 | +```  | 
 | 333 | + | 
 | 334 | +---  | 
 | 335 | + | 
 | 336 | +## ❓ **FAQ**  | 
 | 337 | + | 
 | 338 | +### **Q: Will this affect existing deployments?**  | 
 | 339 | +A: No breaking changes. Existing entities show graceful fallbacks, all APIs work unchanged.  | 
 | 340 | + | 
 | 341 | +### **Q: What happens if authentication is disabled?**  | 
 | 342 | +A: Metadata still works - `created_by` will be `"anonymous"` instead of a username.  | 
 | 343 | + | 
 | 344 | +### **Q: How much storage does metadata require?**  | 
 | 345 | +A: Minimal - approximately 13 additional nullable text columns per entity.  | 
 | 346 | + | 
 | 347 | +### **Q: Can I disable metadata tracking?**  | 
 | 348 | +A: Not currently - metadata is core to the audit system. All fields are optional and backwards compatible.  | 
 | 349 | + | 
 | 350 | +### **Q: How do I export metadata for compliance?**  | 
 | 351 | +A: Use the standard export functionality - metadata is included in all entity exports.  | 
 | 352 | + | 
 | 353 | +This comprehensive metadata system provides enterprise-grade audit capabilities while maintaining full backwards compatibility and operational simplicity.  | 
0 commit comments