Drift Detection
Related Topics: Testing (endpoint tests) | Configuration (profiles) | Deployment (CI/CD)
MXCP’s drift detection system helps you track changes to your database schema and endpoint definitions across different environments and over time. This is crucial for maintaining consistency, catching unintended changes, and ensuring your AI applications work reliably across development, staging, and production environments.
What is Drift Detection?
Section titled “What is Drift Detection?”Drift detection compares the current state of your MXCP repository against a previously captured baseline snapshot to identify:
- Database Schema Changes: Added, removed, or modified tables and columns
- Endpoint Changes: Added, removed, or modified tools, resources, and prompts
- Validation Changes: Changes in endpoint validation results
- Test Changes: Changes in test results or test definitions
Why Use Drift Detection?
Section titled “Why Use Drift Detection?”Environment Consistency
Section titled “Environment Consistency”Ensure your development, staging, and production environments stay in sync:
# Generate baseline from productionmxcp drift-snapshot --profile prod
# Check if staging matches productionmxcp drift-check --profile staging --baseline prod-snapshot.jsonChange Monitoring
Section titled “Change Monitoring”Detect unintended changes before they cause issues:
- Schema modifications that break existing endpoints
- Endpoint parameter changes that affect LLM integrations
- Test failures that indicate breaking changes
Deployment Validation
Section titled “Deployment Validation”Verify deployments haven’t introduced unexpected changes:
# Before deploymentmxcp drift-snapshot --profile staging
# After deploymentmxcp drift-check --profile stagingCompliance and Auditing
Section titled “Compliance and Auditing”Track all changes for compliance and debugging:
- Maintain audit trails of schema evolution
- Identify when and what changed between versions
- Ensure changes follow approval processes
Configuration
Section titled “Configuration”Add drift configuration to your mxcp-site.yml:
mxcp: 1project: my_projectprofile: default
profiles: default: duckdb: path: "db-default.duckdb" drift: path: "drift-default.json"
staging: duckdb: path: "db-staging.duckdb" drift: path: "drift-staging.json"
production: duckdb: path: "db-production.duckdb" drift: path: "drift-production.json"Create Baseline
Section titled “Create Baseline”# Create snapshot for current statemxcp drift-snapshot --profile production
# Overwrite existing snapshotmxcp drift-snapshot --profile production --force
# Preview without writingmxcp drift-snapshot --dry-runThis creates a JSON file with:
- Database schema (tables, columns)
- Endpoint definitions
- Validation results
- Test results
Check for Drift
Section titled “Check for Drift”# Compare current state to baselinemxcp drift-check --profile production
# With custom baselinemxcp drift-check --baseline snapshots/v1.0.0.json
# JSON output for automationmxcp drift-check --json-output
# Get detailed outputmxcp drift-check --debug
# Read-only database accessmxcp drift-check --readonlyExit codes:
0- No drift detected1- Drift detected
Snapshot Structure
Section titled “Snapshot Structure”A drift snapshot contains comprehensive information about your MXCP repository state:
{ "version": "1", "generated_at": "2025-01-27T10:30:00.000Z", "tables": [ { "name": "users", "columns": [ { "name": "id", "type": "INTEGER", "nullable": false }, { "name": "email", "type": "VARCHAR", "nullable": false } ] } ], "resources": [ { "validation_results": { "status": "ok", "path": "tools/get_user.yml" }, "test_results": { "status": "ok", "tests_run": 2, "tests": [...] }, "definition": { "mxcp": "1", "tool": { "name": "get_user", "description": "Get user by ID" } } } ]}Drift Report Structure
Section titled “Drift Report Structure”When drift is detected, you get a detailed report:
{ "version": "1", "generated_at": "2025-01-27T10:35:00.000Z", "baseline_snapshot_path": "drift-default.json", "has_drift": true, "summary": { "tables_added": 1, "tables_removed": 0, "tables_modified": 1, "resources_added": 2, "resources_removed": 0, "resources_modified": 1 }, "table_changes": [ { "name": "orders", "change_type": "added", "columns_added": [...] }, { "name": "users", "change_type": "modified", "columns_added": [ { "name": "created_at", "type": "TIMESTAMP", "nullable": true } ] } ], "resource_changes": [ { "path": "tools/new_tool.yml", "endpoint": "tool/new_tool", "change_type": "added" }, { "path": "tools/existing_tool.yml", "endpoint": "tool/existing_tool", "change_type": "modified", "validation_changed": true, "test_results_changed": false, "definition_changed": true } ]}CI/CD Integration
Section titled “CI/CD Integration”GitHub Actions
Section titled “GitHub Actions”name: Drift Detectionon: [push, pull_request]
jobs: drift-check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.11' - run: pip install mxcp - name: Check for drift run: mxcp drift-check --baseline baseline.jsonThe command exits with code 1 if drift is detected, failing the CI check.
GitLab CI
Section titled “GitLab CI”drift-check: image: python:3.11 script: - pip install mxcp - mxcp drift-check --baseline baseline.json rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event"Use Cases
Section titled “Use Cases”Environment Synchronization
Section titled “Environment Synchronization”Keep multiple environments in sync:
# Generate production baselinemxcp drift-snapshot --profile production
# Check if development matches productionmxcp drift-check --profile development --baseline drift-production.json
# Check if staging matches productionmxcp drift-check --profile staging --baseline drift-production.jsonPre-Deployment Validation
Section titled “Pre-Deployment Validation”Validate changes before deploying:
# Before making changesmxcp drift-snapshot --profile staging
# After making changes, check what changedmxcp drift-check --profile staging
# If drift is acceptable, update baselinemxcp drift-snapshot --profile staging --forceSchema Evolution Tracking
Section titled “Schema Evolution Tracking”Track how your schema evolves over time:
# Tag snapshots with versionsmxcp drift-snapshot --profile productioncp drift-production.json snapshots/v1-snapshot.json
# Later, compare against historical snapshotsmxcp drift-check --baseline snapshots/v1-snapshot.jsonJSON Output for Automation
Section titled “JSON Output for Automation”Get machine-readable output for automation:
# Get JSON outputmxcp drift-check --json-output > drift-report.json
# Process with jqmxcp drift-check --json-output | jq '.summary'
# Check if drift exists in scriptsif mxcp drift-check --json-output | jq -r '.has_drift' | grep -q true; then echo "Drift detected!" exit 1fiCLI Reference
Section titled “CLI Reference”drift-snapshot
Section titled “drift-snapshot”mxcp drift-snapshot [OPTIONS]
Options: --profile TEXT Profile name to use --force Overwrite existing snapshot file --dry-run Show what would be done without writing --json-output Output in JSON format --debug Show detailed debug informationdrift-check
Section titled “drift-check”mxcp drift-check [OPTIONS]
Options: --profile TEXT Profile name to use --baseline TEXT Path to baseline snapshot file --json-output Output in JSON format --debug Show detailed debug information --readonly Open database in read-only modeSecurity Considerations
Section titled “Security Considerations”- Sensitive Data: Snapshots may contain schema information; store securely
- Access Control: Limit who can generate and modify baseline snapshots
- Encryption: Encrypt snapshots if they contain sensitive metadata
- Audit Trails: Log all drift detection activities for security auditing
Performance Considerations
Section titled “Performance Considerations”- Large Schemas: Drift detection time increases with schema size
- Frequent Checks: Consider caching for frequently run drift checks
- CI/CD Optimization: Run drift checks only on relevant file changes
Next Steps
Section titled “Next Steps”- Testing - Endpoint testing
- Deployment - Production deployment with CI/CD
- Configuration - Profile configuration