The Self-Healing Ontology MCP Agent System is designed to automatically maintain semantic mappings between ontologies and database schemas, ensuring AI agents can continue querying data even when schemas change.
Purpose: Bridge between semantic queries and SQL databases.
Key Files:
server.py: Main server implementationtools.py: Tool generation from ontology
How it works:
- Loads OWL ontology from file
- Extracts class → table and property → column mappings
- Generates MCP tools dynamically
- Translates semantic queries to SQL at runtime
- Executes queries and returns results
Features:
- Connection pooling for performance
- Query caching with TTL
- Hot-reload support
- Multiple database backends (SQLite, PostgreSQL)
Purpose: Continuously monitor database schema for changes.
Key Files:
schema_monitor.py: Monitoring loop and change detectiondiff_engine.py: Schema diff computation
How it works:
- Captures schema snapshot using SQLAlchemy inspector
- Computes SHA-256 hash of schema
- Compares hash on configurable interval
- Computes detailed diffs when hash changes
- Triggers callbacks for detected changes
Features:
- SHA-256 hash-based detection
- Heuristic rename detection
- Configurable check intervals
- Event-driven callbacks
Purpose: Automatically update ontology mappings when schema changes.
Key Files:
auto_remapper.py: Claude API integration for remappingvalidator.py: Triple validation
How it works:
- Extracts current ontology mappings
- Generates LLM prompt with schema changes
- Calls Claude API for remapping suggestions
- Validates proposed RDF triples
- Applies updates to ontology file (with approval)
Features:
- Claude API integration
- RDF triple validation
- Manual approval mode
- Backup creation before updates
Purpose: Orchestrate monitoring → detection → healing workflow.
Key Files:
self_healing.py: Main orchestration logicalerts.py: Alert management
How it works:
- Initializes all components
- Starts schema monitoring
- Handles schema change events
- Triggers ontology remapping
- Reloads MCP server after healing
- Sends alerts for operations teams
Features:
- Complete audit logging
- Configurable auto-approve
- Graceful error handling
- Webhook alert integration
Purpose: Provide base agent class using MCP tools.
Key Files:
base_agent.py: Base agent implementationexamples/: Example agents (analytics, support)
How it works:
- Initializes LangChain agent
- Creates tools from MCP server
- Executes queries using natural language
- Agents share semantic understanding via MCP
Features:
- LangChain integration
- Shared ontology understanding
- Natural language queries
- Tool-based architecture
Agent Query (Natural Language)
↓
BaseAgent.query()
↓
LangChain Agent
↓
MCP Tool Execution
↓
Ontology → SQL Translation
↓
Database Query
↓
Result → Agent → User
Schema Change Detected
↓
SchemaMonitor.on_change()
↓
DiffEngine.compute_diff()
↓
SelfHealingAgentSystem._heal_ontology()
↓
OntologyRemapper.remap_ontology()
↓
Claude API Call
↓
Triple Validation
↓
Ontology File Update
↓
MCP Server Reload
↓
Alert Sent
The system uses OWL ontologies with custom annotations:
<!-- Class to Table Mapping -->
<owl:Class rdf:about="#Customer">
<mapsToTable rdf:datatype="xsd:string">customers</mapsToTable>
</owl:Class>
<!-- Property to Column Mapping -->
<owl:DatatypeProperty rdf:about="#email">
<mapsToColumn rdf:datatype="xsd:string">email</mapsToColumn>
</owl:DatatypeProperty>
<!-- Rename Handling -->
<owl:DatatypeProperty rdf:about="#oldProperty">
<equivalentProperty rdf:resource="#newProperty"/>
</owl:DatatypeProperty>Configuration is managed via YAML with environment variable support:
database:
connection_string: ${DATABASE_URL} # Environment variable substitution
healing:
auto_approve: ${AUTO_APPROVE:-false} # Default value supportThe system uses structured logging and graceful error handling:
- All components log with
structlog - Errors are caught and logged, not propagated
- Retry logic for transient failures
- Audit logging for all operations
- Connection Pooling: SQLAlchemy connection pools for database access
- Query Caching: MCP server caches query results with TTL
- Async Operations: Schema monitoring and healing use async/await
- Lazy Loading: Ontology loaded only when needed
- API keys stored in environment variables
- No sensitive data in logs
- Input validation for all queries
- Audit logging for compliance
The system is designed for extensibility:
- Database Adapters: Easy to add new database backends
- Agent Types: Simple to create new agent types
- Alert Channels: Easy to add new alert channels
- Remapping Strategies: Can add different remapping strategies
- Support for MongoDB and other NoSQL databases
- Multi-ontology support
- Web-based monitoring dashboard
- Kubernetes operator for K8s deployments
- GraphQL API layer
- Real-time schema change notifications