自修复本体MCP代理系统

一个生产就绪的自修复多代理系统,利用本体和MCP(模型上下文协议)在数据库模式变更时自动适应。

  • by cloudbadal007
  • Python
  • 本地部署
  • 21 星标
  • 更新于
在 GitHub 查看 cloudbadal007/ontology-mcp-self-healing

使用说明

自愈本体MCP代理系统

一个生产就绪的自愈多代理系统,使用本体和MCP(模型上下文协议)在数据库模式发生变化时自动适应。

概述

该系统解决了现代AI代理部署中的一个关键问题: 当数据库模式更改时,代理查询中断该系统不再手动更新每个代理和查询,而是:

  1. 监视器 数据库模式持续
  2. 发现 架构自动更改
  3. 分析 使用Claude AI进行更改
  4. 治疗 本体自动映射
  5. 重新加载 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、测试、错误处理

快速启动(<5分钟)

1.安装依赖项

pip install -r requirements.txt

2.设置环境

# Create .env file
echo "ANTHROPIC_API_KEY=your_api_key_here" > .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 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

文档

项目结构

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 有关以下内容的分步说明:

  • 克隆存储库
  • 设置您的环境
  • 运行示例
  • 运行测试
  • 常见问题排查

贡献

欢迎投稿!请查看 贡献.md 作为指导方针。

许可证

MIT许可证-请参阅 许可证 文件以获取详细信息。

发布到GitHub

想发布这个项目吗?看 GITHUB_SETUP.md 获取分步说明。

致谢

相关文章


由...制作❤️ 面向AI代理社区