Metadata-Version: 2.4
Name: zongyuan-gateway
Version: 0.2.0
Summary: Routing gateway kernel MCP: exact and prefix routes, default-deny, pass ledger, bitwise replay
Author: tanbopiao
License: Apache-2.0
Project-URL: Homepage, https://www.huodouai.com
Project-URL: Repository, https://github.com/tanbopiao/zongyuan-gateway
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# zongyuan-gateway 枢纽网关

路由网关内核：路由在册、默认拒绝、通行留痕、重放可验，纯标准库。

｜元枢体系 YUANSHU

## 定名语义

- 道统出处：枢纽者，众辐所凑；网关者，门以限行——所向有表，所禁有门
- 工程映射：纯标准库的路由网关内核——路由注册制，未注册路径一律拒，裁决留痕可重演

## 这是什么

一个纯标准库的路由网关内核：路由在册（精确路径与前缀路径分册注册，重复注册即拒）；过门裁决按「精确优先于前缀、前缀最长者胜」定序，未注册路径默认拒绝，不静默放行；每次放行或拒绝都记入只追加的通行簿（路径、路由、裁决、序号）；任意时刻可从通行簿重放裁决序，逐位一致——「裁决可重演」是本内核的第一契约；通行簿摘要在册，一字被改必现形。

## 道统映射

| 道统名 | 内核对象 | 职责 |
|--------|----------|------|
| 路由在册 | `register` | 精确与前缀分册注册，重复即拒，不静默覆盖 |
| 所向有表 | `match` | 精确优先于前缀，前缀最长者胜 |
| 所禁有门 | `handle` | 未注册路径默认拒绝，裁决一律留痕 |
| 通行留痕 | `ledger` | 只追加通行簿：路径、路由、裁决、序号 |
| 重放核验 | `replay` | 按簿重演裁决序，逐位一致 |
| 摘要 | `digest` | SHA-256(canonical 通行簿)，篡改现形 |

## 快速开始

```python
from zongyuan_gateway import Gateway

gw = Gateway()
gw.register("/health", "体检门")
gw.register("/api/", "总门", prefix=True)
gw.register("/api/v2/", "二版门", prefix=True)

gw.handle("/api/v2/users")   # 放行（二版门：最长前缀胜）
gw.handle("/secret")          # 拒绝（未注册路径默认拒）

print(gw.ledger())            # 通行留痕
print(gw.replay() == gw.ledger())  # True：裁决可重演
print(gw.digest())            # 摘要：篡改现形
```

命令行：

```bash
python tools/zongyuan_gateway.py stats  ledger.json
python tools/zongyuan_gateway.py digest ledger.json
python tools/zongyuan_gateway.py replay ledger.json
```

## MCP 门面

内核已拧成 MCP 服务器（stdio，FastMCP，八工具）：`register_route`／`match_route`／`handle_path`／`ledger_view`／`replay_ledger`／`digest`／`stats`／`describe`。进程内持有网关正本，通行簿随进程累积。

```bash
pip install "mcp>=1.0"   # 仅门面需要，内核保持纯标准库
python run_mcp_server.py
```

注册配置（server.json：`io.github.tanbopiao/zongyuan-gateway`，v0.2.0）。

## 设计约定

- 纯标准库（`json`/`hashlib`），无第三方依赖，无网络服务。
- 路由注册制：路径须以 `/` 开头，重复注册（含跨册同路径）一律 `GatewayError`，不静默覆盖。
- 路径合制：非字符串路径在匹配与过门入口一律拒以 `GatewayError`，不撞裸异常。
- 默认拒绝：未注册路径不猜测、不放行，裁决与拒绝理由同簿留痕。
- 摘要基于通行簿原字节序：任何一字被改，重放摘要必不一致——「篡改现形」是本内核的第一契约。
- 防篡改前提（统一声明）：本链可发现局部或单点篡改；持有全量记录的攻击者可整体重写并重算通过——链头哈希需带外留存（或对链头签名）方可对抗整体重写。

## 回归

`regression.md` 登记回归铁律（路由、匹配、拒绝、日志、摘要五域契约）。运行：

```bash
python3 -m unittest discover -s . -p "test_*.py"
```

## 许可证

Dual-licensed: AGPL-3.0 (default) or a commercial license for closed-source use — see [DUAL-LICENSE.md](DUAL-LICENSE.md)。仓库：<https://github.com/tanbopiao/zongyuan-gateway>

<!-- ECOSYSTEM:START -->
---
**生态导航**：本作品属于 [元枢工具货架](https://github.com/tanbopiao)（21 件 MCP，自动聚合）。
同门：[tanze](https://github.com/tanbopiao/tanze)（深研） · [pialuo](https://github.com/tanbopiao/pialuo)（网页提取） · [wengu](https://github.com/tanbopiao/wengu)（跨会话记忆） · [jiwei](https://github.com/tanbopiao/jiwei)（像素特效） · [constraint-runtime](https://github.com/tanbopiao/constraint-runtime)（约束治理） · [agent-eval-engine](https://github.com/tanbopiao/agent-eval-engine)（可验证评测） · [全部作品](https://github.com/tanbopiao)
<!-- ECOSYSTEM:END -->
