克劳德·普雷托里安
一 模型上下文协议(MCP) 用于积极压缩上下文的服务器 克劳德代码通过将网络研究、任务输出和对话压缩到结构化快照中,节省90%以上的令牌。
______________________________________________________________________
安装
要求:

来自shell:
claude mcp add claude-praetorian-mcp -- npx claude-praetorian-mcp从内部克劳德 (需要重新启动):
Add this to our global mcp config: npx claude-praetorian-mcp
Install this mcp: https://github.com/Vvkmnn/claude-praetorian-mcp从任何手动配置 mcp.json:(光标、风帆等)
{
"mcpServers": {
"claude-praetorian-mcp": {
"command": "npx",
"args": ["claude-praetorian-mcp"],
"env": {}
}
}
}有 不 npm install 必需的 --没有外部数据库或服务,只有平面文件。
然而,如果 npx 解决错误的包,您可以使用以下命令强制解决:
npm install -g claude-praetorian-mcp技能
可选地,安装技能来教克劳德何时主动使用禁卫军:
npx skills add Vvkmnn/claude-praetorian-mcp --skill claude-praetorian --global
# Optional: add --yes to skip interactive prompt and install to all agents这使得Claude在研究、子代理任务和上下文重置之前自动压缩。MCP在没有技能的情况下工作,但技能提高了可发现性。
插件
对于带挂钩的全自动化,请从 克劳德商场 市场:
/plugin marketplace add Vvkmnn/claude-emporium
/plugin install claude-praetorian@claude-emporium这 克劳德总督 插件提供:
钩子 (有针对性,仅在高价值时刻开火):
- 进入计划模式之前→ 恢复此项目之前的压实
- 在上下文压缩之前→ 在上下文重置之前保存决策/见解
- WebFetch/WebSearch之后→ 紧凑型网络研究成果
- SubgentStop之后→ 紧凑的子代理任务结果
命令: /praetorian-compact, /praetorian-restore, /praetorian-search
需要先安装MCP服务器。请参阅商场了解其他Claude Code插件和MCP。
特性
逐个项目运行,将工件保存到 {$project}/.claude/praetorian (带着皇家卫队 ⚜️):
praetorian_compact
使用TOON格式(递增)压缩上下文,从活动中获取最有价值的令牌。
⚜️ praetorian_compact type= title=
> "ACE Framework research - save 1,450 tokens"
> "Icon rendering bug investigation - compact the findings"
> "Database architecture decisions - preserve the rationale"
> "WebFetch results from authentication docs"
> "Task output from explore subagent - code structure analysis"⚜️ compact | Created
┌─ ⚜️ ────────────────────────────────────────────────── Created ─┐
│ Compacted: "ACE Framework Research" • 1,450 tokens saved
│ Type: web_research • ID: cpt_1765245902396_nxetoc
└───────────────────────────────────────────────────────────────────┘{
"type": "web_research",
"title": "ACE Framework Research",
"source": "https://github.com/humanlayer/ace-fca",
"key_insights": [
"Frequent intentional compaction saves 90%+ tokens",
"TOON format is 30-60% smaller than JSON/YAML",
"Compaction should happen after every expensive operation"
],
"refs": ["ace-fca.md:42 - compaction strategy", "toon-spec.md:1 - format definition"],
"recommendations": ["Compact after every WebFetch", "Use type='decisions' for architecture choices"]
}⚜️ compact | Merged
┌─ ⚜️ ───────────────────────────────────────────────────── Merged ─┐
│ Compacted: "Authentication Patterns" • 890 tokens saved
│ Type: decisions • ID: cpt_1765245903512_xk9mp1
│ Merged with: cpt_1765245903512_xk9mp1
└────────────────────────────────────────────────────────────────────┘{
"type": "decisions",
"title": "Authentication Patterns",
"decisions": [
{ "chose": "JWT with refresh tokens", "over": ["sessions", "API keys"], "reason": "Stateless, works across microservices" },
{ "chose": "httpOnly cookies", "over": ["localStorage"], "reason": "XSS protection" }
],
"refs": ["src/middleware/auth.ts:45 - token validation", "src/routes/login.ts:23 - refresh flow"],
"anti_patterns": ["Never store tokens in localStorage", "Never skip CSRF on cookie-based auth"]
}praetorian_restore
根据需要,通过将TOON令牌注入到当前上下文中来搜索和恢复上下文。
⚜️ praetorian_restore query=
> "What did we learn about authentication?"
> "Find the Docker container debugging session"
> "Show recent architecture decisions"
> "Search for MCP server implementation patterns"
> "" (empty = recent compactions)⚜️ restore | Search
┌─ ⚜️ ───────────────────────────────────────────────────── Search ─┐
│ Found 2 compactions
│ Query: "authentication"
└────────────────────────────────────────────────────────────────────┘{
"compactions": [
{
"id": "cpt_1765245903512_xk9mp1",
"type": "decisions",
"title": "Authentication Patterns",
"relevance": 0.85,
"key_insights": ["JWT with refresh tokens", "httpOnly cookies for XSS protection"],
"refs": ["src/middleware/auth.ts:45", "src/routes/login.ts:23"]
},
{
"id": "cpt_1765245902396_nxetoc",
"type": "web_research",
"title": "OAuth2 Best Practices",
"relevance": 0.72,
"key_insights": ["PKCE flow for public clients", "Token rotation every 15min"]
}
],
"total": 2
}⚜️ restore | Recent
┌─ ⚜️ ───────────────────────────────────────────────────── Recent ─┐
│ Found 3 compactions
└────────────────────────────────────────────────────────────────────┘状态指示器:
- 创建 -保存新的压实
- 合并 -更新了现有的压缩(通过以下方式实现>70%的标题相似性 Jaccard指数)
- 搜索 -返回搜索结果(关键字匹配)
- 最近 -列出的最近契约(按更新时间)
Praetorian专为 频繁使用。你越紧凑,节省的就越多。
何时压缩:
- \[x\] 每次WebFetch之后
- \[x\] 每个任务/子代理完成后
- \[x\] 读取多个文件后
- \[x\] 做出决定后
- \[x\] 在长时间交谈中(主动压缩)
- \[x\] 在上下文超过60%之前
真实世界示例会话:
| 压缩 | 之前 | 之后 | 保存 |
|---|---|---|---|
| 网络研究(3个网址) | 4500 | 300 | 4,200 |
| 子代理输出(2) | 3500 | 300 | 3,200 |
| 建筑辩论 | 5000 | 300 | 4,700 |
| 钩子研究 | 1500 | 150 | 1,350 |
| 总计 | 14,500 | 1,050 | 13,450 (93%) |
下次会议: restore() 加载约1000个令牌。即时简历,无需重新研究。
方法论
⚜️ claude-praetorian-mcp
════════════════════════
compact (save) restore (load)
────────────── ──────────────
INPUT QUERY
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ Zod │ │ Inverted│
│Validate │ │ Index │
└────┬────┘ └────┬────┘
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ Jaccard │ │ TOON │
│ >70% ? │ │ Decode │
└──┬───┬──┘ └────┬────┘
│ │ │
new│ │match ▼
│ │ OUTPUT
▼ ▼
┌─────────┐
│ TOON │
│ Encode │
└────┬────┘
│
▼
┌─────────┐
│ Index │
│ Update │
└────┬────┘
│
▼
OUTPUT
storage: .claude/praetorian/
────────────────────────────
index.json word index + metadata
compactions/*.toon encoded compaction files核心优化:
- TOON格式:令牌比YAML/JSON少30-60%
- Zod验证:生产级运行时类型安全
- Jaccard相似性:通过标题匹配实现智能重复数据删除(>70%阈值)
- 倒排索引:无需向量嵌入的快速关键字搜索
- 智能合并:合并类似的契约,不重复
设计原则:
- 增量 --通过Jaccard相似性合并相似的压缩,不要替换
- 令牌最小 --TOON格式的令牌比YAML/JSON少30-60%
- 项目范围 --每个项目都有自己的存储
.claude/praetorian/ - 仅限平面文件 --没有数据库,没有外部服务
- 离线 --永远不要离开你的机器,没有网络呼叫
文件访问:
- 店铺位于: `
/.claude/praetorian/`
- 格式:
.toon文件(编码压缩)+index.json(单词索引+元数据)
发展
git clone https://github.com/Vvkmnn/claude-praetorian-mcp && cd claude-praetorian-mcp
npm install && npm run build
npm test包装要求:
- Node.js:>=20.0.0(ES模块)
- 运行时:
@modelcontextprotocol/sdk,@toon-format/toon,zod - 零外部数据库 --与合作
npx
开发工作流程:
npm run build # TypeScript compilation with executable permissions
npm run dev # Watch mode with tsc --watch
npm run start # Run the MCP server directly
npm run lint # ESLint code quality checks
npm run lint:fix # Auto-fix linting issues
npm run format # Prettier formatting (src/)
npm run format:check # Check formatting without changes
npm run typecheck # TypeScript validation without emit
npm run test # Lint + type check
npm run prepublishOnly # Pre-publish validation (build + lint + format:check)Git钩子(通过赫斯基):
- 预承诺:自动格式化已暂存
.ts使用Prettier和ESLint的文件
贡献:
- 分叉存储库并创建功能分支
- 在提交PR之前,使用多种压实类型进行测试
- 遵循TypeScript严格模式 MCP协议 标准
从示例中学习:
- 官方MCP服务器 供参考实现
- TypeScript SDK 最佳实践
- 用于npm包开发
许可证
_公元41年罗马皇帝 通过 劳伦斯·阿尔玛-塔德玛 (1871). 格拉杜斯 的 禁卫军 发现 克劳狄乌斯 躲在窗帘后面 宣布他为皇帝. 沃尔特艺术博物馆,公共领域。_
