自愈本体MCP代理系统
一个生产就绪的自愈多代理系统,使用本体和MCP(模型上下文协议)在数据库模式发生变化时自动适应。
概述
该系统解决了现代AI代理部署中的一个关键问题: 当数据库模式更改时,代理查询中断该系统不再手动更新每个代理和查询,而是:
- 监视器 数据库模式持续
- 发现 架构自动更改
- 分析 使用Claude AI进行更改
- 治疗 本体自动映射
- 重新加载 MCP工具无需停机
结果是:即使数据库不断发展,代理也能继续无缝工作。
建筑
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Agents │─────▶│ MCP Server │─────▶│ Database │
│ (Analytics, │ │ (Ontology) │ │ (SQLite, │
│ Support) │ │ │ │ PostgreSQL)│
└─────────────┘ └──────┬───────┘ └─────────────┘
│
▼
┌─────────────────┐
│ Schema Monitor │
│ (SHA-256 Hash) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Diff Engine │
│ (Change Detect) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Ontology Remap │
│ (Claude AI) │
└─────────────────┘特性
- ✅ 自动模式更改检测 -基于SHA-256哈希的监控
- ✅ 智能差分分析 -检测重命名、添加、删除
- ✅ 人工智能驱动的治疗 -使用Claude更新本体映射
- ✅ MCP协议支持 -本地模型上下文协议集成
- ✅ 多代理支持 -跨代理共享语义理解
- ✅ 热重新加载 -MCP服务器在不停机的情况下重新加载
- ✅ 审计日志 -完整的JSON审计跟踪
- ✅ 警报集成 -Slack/Teams webhook支持
- ✅ 生产就绪 -Docker、测试、错误处理
快速启动(\ .env
### 3.初始化数据库和本体
Create sample database
python scripts/init_db.py
Generate initial ontology
python scripts/setup_ontology.py
### 4.运行快速启动示例
python examples/quickstart.py
您应该看到:
- ✓ MCP服务器已初始化
- ✓ 从本体生成的工具
- ✓ 模式监视已激活
- ✓ 代理系统就绪
### 5.测试模式更改修复
In another terminal, modify the database schema
sqlite3 test_database.db "ALTER TABLE customers ADD COLUMN phone TEXT;"
Watch the system automatically detect and heal
python examples/full_system.py
## 安装
### 来源
git clone https://github.com/yourusername/ontology-mcp-self-healing.git cd ontology-mcp-self-healing pip install -r requirements.txt pip install -e .
### 使用Docker
Build and run
docker-compose up -d
View logs
docker-compose logs -f
## 配置
配置通过以下方式管理 `config/config.yaml`:
Database Configuration
database: type: sqlite # sqlite, postgresql, mysql connection_string: sqlite:///./test_database.db
Ontology Configuration
ontology: main_file: ontologies/business_domain.owl auto_reload: true
Schema Monitoring
monitoring: enabled: true check_interval: 60 # seconds detect_renames: true
Auto-Healing
healing: enabled: true auto_approve: false # Set to true for automatic healing claude_model: claude-3-5-sonnet-20241022 validation_enabled: true
Alerts
alerts: enabled: true webhook_url: ${ALERT_WEBHOOK_URL} # Optional
## 使用示例
### 基本MCP服务器
from src.mcp_server.server import OntologyMCPServer
Initialize server
server = OntologyMCPServer()
Get available tools
tools = server.get_tools()
Execute query
result = await server.execute_tool( "query_order", {"query": "all orders", "limit": 10} )
### 创建代理
from src.mcp_server.server import OntologyMCPServer from src.agents.examples.analytics_agent import AnalyticsAgent
Initialize MCP server
mcp_server = OntologyMCPServer()
Create agent
agent = AnalyticsAgent(mcp_server, claude_api_key="your_key")
Query using natural language
response = await agent.query("What are the total sales for last month?")
### 全自愈系统
from src.system.self_healing import SelfHealingAgentSystem
Initialize system
system = SelfHealingAgentSystem()
Start monitoring and healing
system.start()
System runs forever, auto-healing on schema changes
## 建筑细部
### MCP服务器
MCP服务器加载OWL本体并动态生成工具:
- 提取类→ 表映射
- 提取属性→ 列映射
- 将语义查询转换为SQL
- 缓存性能查询
### 架构监视器
使用以下方法进行持续监测:
- 用于模式捕获的SQLAlchemy检查器
- SHA-256哈希用于变化检测
- 可配置的检查间隔
- 更改时的事件回调
### 差速器发动机
智能差分计算:
- 检测表/列的添加/删除
- 基于启发式的重命名检测
- 类型更改检测
- 详细的差异报告
### 汽车改装
人工智能驱动的本体修复:
- 提取当前本体映射
- 生成带有架构更改的LLM提示
- 验证所提出的RDF三元组
- 对本体文件应用更新
- 支持手动审批模式
## 生产部署
### Docker部署
Build image
docker build -t ontology-mcp-self-healing .
Run container
docker run -d \ -e ANTHROPIC_API_KEY=your_key \ -v $(pwd)/ontologies:/app/ontologies \ -v $(pwd)/logs:/app/logs \ ontology-mcp-self-healing
### Docker Compose
Start all services
docker-compose up -d
View logs
docker-compose logs -f self-healing
Stop services
docker-compose down
看 [docs/deployment.md](docs/deployment.md) Kubernetes、Helm和生产最佳实践。
## 测试
Run all tests
pytest tests/ -v
Run with coverage
pytest tests/ --cov=src --cov-report=html
Run specific test
pytest tests/test_mcp_server.py -v
## 文档
- [架构指南](docs/architecture.md) -详细的系统架构
- [部署指导](docs/deployment.md) -生产部署说明
- [故障排除](docs/troubleshooting.md) -常见问题和解决方案
## 项目结构
ontology-mcp-self-healing/ ├── README.md ├── requirements.txt ├── setup.py ├── docker-compose.yml ├── Dockerfile ├── config/ │ └── config.yaml ├── ontologies/ │ └── business_domain.owl ├── src/ │ ├── mcp_server/ # MCP server implementation │ ├── monitoring/ # Schema monitoring │ ├── healing/ # Auto-healing │ ├── system/ # Orchestration │ └── agents/ # Agent implementations ├── tests/ # Test suite ├── examples/ # Example scripts ├── scripts/ # Setup scripts └── docs/ # Documentation
## 入门指南
这个项目的新手?关注我们的综合 [SETUP_GUIDE.md](SETUP_GUIDE.md) 有关以下内容的分步说明:
- 克隆存储库
- 设置您的环境
- 运行示例
- 运行测试
- 常见问题排查
## 贡献
欢迎投稿!请查看 [贡献.md](CONTRIBUTING.md) 作为指导方针。
## 许可证
MIT许可证-请参阅 [许可证](LICENSE) 文件以获取详细信息。
## 发布到GitHub
想发布这个项目吗?看 获取分步说明。
## 致谢
- 建于 [owlread2](https://owlready2.readthedocs.io/) 用于本体管理
- 用途 [安thropic克劳德](https://www.anthropic.com/) 用于人工智能驱动的治疗
- 实现 [MCP协议](https://modelcontextprotocol.io/) 用于代理通信
- 由...驱动 [LangChain](https://www.langchain.com/) 用于代理编排
## 相关文章
- [中篇文章:自愈AI代理](YOUR_ARTICLE_URL) -深入系统设计
- [博客文章:MCP for Production](YOUR_BLOG_URL) -在生产系统中使用MCP
______________________________________________________________________
**由...制作❤️ 面向AI代理社区**