# Sentinel MCP Server

Sentinel MCP Server 是一个 [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) 服务器，用于从 ZooKeeper 查询 Sentinel 限流、熔断规则，供 AI 工具使用。

## 功能特性

- 查询限流规则 (FlowRule)
- 查询熔断降级规则 (DegradeRule)
- 查询热点参数限流规则 (ParamFlowRule)
- 查询授权规则 (AuthorityRule)
- 列出所有已配置规则的应用

## ZooKeeper 路径规则

基于易盾 Sentinel 的 ZooKeeper 存储规范：

| 规则类型 | ZK 路径 |
|----------|---------|
| 限流规则 | `/sentinel_rule_config/{app}/flowRules` |
| 熔断规则 | `/sentinel_rule_config/{app}/degradeRules` |
| 热点参数限流 | `/sentinel_rule_config/{app}/paramFlowRules` |
| 授权规则 | `/sentinel_rule_config/{app}/authorityRules` |

## 安装

```bash
npm install @yidun/sentinel-mcp-server
```

## 配置

### Claude Desktop 配置

在 `claude_desktop_config.json` 中添加：

```json
{
  "mcpServers": {
    "sentinel": {
      "command": "node",
      "args": ["/path/to/sentinel-mcp-server/dist/index.js"]
    }
  }
}
```

### 网络要求

MCP Server 需要能够访问 ZooKeeper。如果 ZooKeeper 在内网，可以通过以下方式解决：

1. **VPN 连接** - 连接到公司 VPN
2. **SSH 隧道** - `ssh -L 2181:zk-server:2181 jump-server`

## 可用工具

### get_flow_rules

获取指定应用的限流规则。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址，如 `127.0.0.1:2181`
- `appName` (必需): 应用名称

**示例：**
```
获取 my-app 的限流规则，ZK 地址是 localhost:2181
```

### get_degrade_rules

获取指定应用的熔断降级规则。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址
- `appName` (必需): 应用名称

### get_param_flow_rules

获取指定应用的热点参数限流规则。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址
- `appName` (必需): 应用名称

### get_authority_rules

获取指定应用的授权规则。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址
- `appName` (必需): 应用名称

### get_all_sentinel_rules

获取指定应用的所有 Sentinel 规则。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址
- `appName` (必需): 应用名称

### list_sentinel_apps

列出 ZooKeeper 中已配置 Sentinel 规则的所有应用。

**参数：**
- `zkAddress` (必需): ZooKeeper 地址

## 开发

```bash
# 安装依赖
npm install

# 开发模式运行
npm run dev

# 构建
npm run build

# 运行测试
npm test
```

## 规则类型说明

### FlowRule (限流规则)

| 字段 | 说明 |
|------|------|
| resource | 资源名称 |
| grade | 阈值类型：0-线程数，1-QPS |
| count | 阈值 |
| strategy | 流控模式：0-直接，1-关联，2-链路 |
| controlBehavior | 流控效果：0-快速失败，1-Warm Up，2-排队等待 |

### DegradeRule (熔断规则)

| 字段 | 说明 |
|------|------|
| resource | 资源名称 |
| grade | 熔断策略：0-慢调用比例，1-异常比例，2-异常数 |
| count | 阈值 |
| timeWindow | 熔断时长（秒） |
| minRequestAmount | 最小请求数 |

## License

MIT
