Skip to main content
Glama
README.md
# Chaos Mesh MCP Server

Chaos Mesh MCP 服务端,基于 Python FastMCP 实现,提供 Chaos Mesh 混沌实验的 MCP 工具集。

## 项目结构

```
chaosmesh-mcp/
├── src/
│   └── chaosmesh_mcp/          # 源代码 (src 布局)
│       ├── __init__.py
│       ├── __main__.py          # python -m 入口
│       ├── server.py            # FastMCP 服务启动
│       ├── tools/               # MCP 工具注册
│       │   ├── chaos.py         # list_chaos / delete_chaos
│       │   ├── cluster.py       # 集群巡检 (namespace/pod/node)
│       │   ├── httpchaos.py     # HTTP 接口级延迟注入
│       │   ├── network_chaos.py # 网络延迟/丢包/外部延迟
│       │   └── pod.py           # Pod Kill 注入
│       ├── services/            # 服务适配层
│       │   ├── kubectl.py       # kubectl 命令封装
│       │   └── chaosmesh.py     # 模板渲染与 Chaos CRD 应用
│       ├── utils/               # 工具函数
│       │   ├── generate.py      # 混沌资源名生成
│       │   ├── response.py      # 统一响应格式
│       │   └── validate.py      # 输入校验
│       └── templates/           # Chaos Mesh YAML 模板
│           ├── network_delay.yaml
│           ├── network_loss.yaml
│           ├── network_external_delay.yaml
│           ├── pod_kill.yaml
│           └── httpchaos_delay.yaml
├── tests/
│   └── __init__.py
├── deploy/k8s/                  # Kubernetes 部署清单
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── kustomization.yaml
│   ├── kustomizeconfig.yaml
│   ├── .env.config
│   └── .env.secret.example
├── pyproject.toml
├── uv.lock
├── .python-version
├── Dockerfile
└── README.md
```

## 运行方式

### 环境准备
```bash
# 确保已安装 uv
uv --version

# 同步依赖(自动创建 .venv 并安装所有依赖)
uv sync
```

### 本地运行
```bash
# 使用 uv run 启动服务
uv run python -m chaosmesh_mcp

# 或通过 console_scripts
uv run chaosmesh-mcp
```

### Docker
```bash
docker build -t chaos-mcp:latest .
docker run -p 8000:8000 chaos-mcp:latest
```

### 环境变量
- `MCP_HOST` — 服务监听地址,默认 `0.0.0.0`
- `MCP_PORT` — 服务监听端口,默认 `8000`

## 功能说明

### 集群巡检工具 (`tools/cluster.py`)
- `get_namespaces` — 查询集群中的 namespace 列表
- `get_pods` — 以 wide 格式列出目标命名空间中的 Pod
- `get_nodes` — 以 wide 格式列出集群中的所有节点
- `cluster_info` — 获取 Kubernetes 集群端点与服务信息

### 网络混沌工具 (`tools/network_chaos.py`)
- `resolve_namespace` — 根据 K8s 服务名称查询所在的 namespace
- `network_delay` — 对服务与服务之间的网络连接注入延迟
- `network_external_delay` — 对发往第三方渠道域名注入响应延迟 (Workflow)
- `network_loss` — 对服务间注入 NetworkChaos 丢包故障

### HTTP 混沌工具 (`tools/httpchaos.py`)
- `httpchaos_delay` — 按 HTTP 路径和端口精准注入延迟

### Pod 混沌工具 (`tools/pod.py`)
- `pod_kill` — PodChaos pod-kill 故障注入

### 混沌生命周期工具 (`tools/chaos.py`)
- `list_chaos` — 列出指定命名空间中的混沌资源
- `delete_chaos` — 按类型、名称和命名空间删除单个混沌资源

## 响应格式

所有工具返回统一的 JSON 响应:

```json
{
  "success": true,
  "message": "操作描述",
  "data": {
    "kubectl": "kubectl 返回内容",
    "yaml": "渲染后的 YAML"
  },
  "yaml": "渲染后的 YAML (顶层)"
}
```

## Kubernetes 部署

```bash
kubectl create namespace chaos-mcp
cp deploy/k8s/.env.secret.example deploy/k8s/.env.secret
# 编辑 deploy/k8s/.env.secret 填入真实值
kubectl apply -k deploy/k8s
```

TDQS

B3.4/5.0

Scored across 12 tools

Disambiguation5/5

Each tool has a clearly distinct purpose. The two delay tools are explicitly disambiguated in their descriptions, and other tools cover different fault types or informational queries without overlap.

Naming Consistency4/5

Most tools follow a verb_noun pattern (e.g., delete_chaos, list_chaos, pod_kill), but there are minor inconsistencies like 'httpchaos_delay' (combined prefix) and 'network_external_delay' (three-part name) which mildly deviate from the norm.

Tool Count5/5

With 12 tools, the server covers core chaos engineering operations (query, inject, delete) for multiple fault types, without being overwhelming or too sparse.

Completeness3/5

The set includes common fault types (pod kill, network delay/loss, HTTP delay) and external delay, but lacks stress chaos injection tools despite listing them, and does not support update operations, leaving notable gaps.

Maintenance

ActivityInactive
ResponsivenessNo issues