MCP微服务健康监测器
监控4个Spring Boot微服务,从Neo4j获取日志, 并将不健康的服务与您的 凯伊代理 用于汽车修理。
______________________________________________________________________
建筑
┌─────────────────────────────────────────────────────────┐
│ MCP Server (Python) │
│ │
│ check_health ──► /actuator/health (each service) │
│ fetch_logs ──► Neo4j bolt://localhost:7687 │
│ trigger_fix ──► Kaii Agent REST API │
│ restart_service──► ./scripts/restart_.sh │
└─────────────────────────────────────────────────────────┘
▲
Kaii Agent calls these tools______________________________________________________________________
受监控的服务
| 服务 | 端口 | 运行状况URL |
|---|---|---|
| 用户 | 8080 | http://localhost:8080/actuator/health |
| 订单 | 8082 | http://localhost:8082/actuator/health |
| 付款 | 8083 | http://localhost:8083/actuator/health |
| 通知 | 8084 | http://localhost:8084/actuator/health |
______________________________________________________________________
设置
1.安装Python依赖项
pip install -r requirements.txt2.设置环境变量
export NEO4J_URI=bolt://localhost:7687
export NEO4J_USER=neo4j
export NEO4J_PASSWORD=your_password
# Your Kaii Agent REST endpoint
export KAII_AGENT_URL=http://localhost:9000/kaii/analyze3.使重启脚本可执行
chmod +x scripts/restart_*.sh4.运行MCP服务器
python server.py______________________________________________________________________
连接到Kaii代理
将此添加到您的Kaii Agent的MCP客户端配置(claude_desktop_config.json或等效文件)中:
{
"mcpServers": {
"microservice-monitor": {
"command": "python",
"args": ["/path/to/mcp_server/server.py"],
"env": {
"NEO4J_URI": "bolt://localhost:7687",
"NEO4J_USER": "neo4j",
"NEO4J_PASSWORD": "your_password",
"KAII_AGENT_URL": "http://localhost:9000/kaii/analyze"
}
}
}
}______________________________________________________________________
Spring Boot集成
步骤1:将Neo4j Logback Appender添加到每个服务中
复制 config/Neo4jLogAppender.java 进入:
src/main/java/com/yourorg/logging/Neo4jLogAppender.java复制 config/logback-spring.xml 进入:
src/main/resources/logback-spring.xml步骤2:添加到pom.xml
org.neo4j.driver
neo4j-java-driver
5.18.0
步骤3:在application.properties中启用弹簧执行器
management.endpoints.web.exposure.include=health,info
management.endpoint.health.show-details=always______________________________________________________________________
MCP工具参考
check_health
{ "service": "payment" } // single service
{ "service": "all" } // all 4 servicesfetch_logs
{ "service": "order", "level": "ERROR", "limit": 20 }trigger_fix
{ "service": "payment", "auto_fetch_logs": true }向Kaii发送:服务名称、运行状况快照、最近20个错误/致命日志。
restart_service
{ "service": "user" }跑 ./scripts/restart_user.sh.
______________________________________________________________________
凯伊代理有效载荷形状
{
"service": "payment",
"status": "DOWN",
"health_details": { ... },
"error": "Connection refused",
"error_logs": [
{
"level": "ERROR",
"message": "...",
"timestamp": "2025-01-01T10:00:00",
"exception": "java.lang.NullPointerException: ..."
}
],
"requested_action": "diagnose_and_fix",
"metadata": {
"base_path": "/home/user/project",
"restart_script": "./scripts/restart_payment.sh",
"triggered_at": "2025-01-01T10:00:05Z"
}
}调整字段名称 trigger_kaii_agent() 在 server.py 匹配 您的Kaii代理商的实际API合同。
______________________________________________________________________
Neo4j日志节点架构
(:Log {
service: "payment",
level: "ERROR",
message: "Database connection pool exhausted",
timestamp: datetime("2025-01-01T10:00:00"),
thread: "http-nio-8083-exec-3",
logger: "com.yourorg.PaymentService",
exception: "java.sql.SQLException: No available connections"
})有用的Cypher查询:
-- Recent errors for payment service
MATCH (l:Log {service: "payment", level: "ERROR"})
RETURN l ORDER BY l.timestamp DESC LIMIT 20;
-- All services error count today
MATCH (l:Log) WHERE l.level IN ['ERROR','FATAL']
RETURN l.service, count(*) AS errors ORDER BY errors DESC;