使用说明
CodeModeTOON MCP服务器
|
Listed on Glama MCP Directory One-click installation for AI assistants |
轻量级 模型上下文协议(MCP) 编排器专为 规模效益.它的特点 TOON压缩 (将代币使用量减少30-90%)以及 延迟加载,使其成为复杂、多工具代理工作流的理想解决方案。
代理工作流中的“上下文陷阱”
最近的文章来自 Anthropic 和 云耀 (参见 这里)突出了一个关键瓶颈: 人工智能代理在复杂的多步骤工作流程中苦苦挣扎,因为它们缺乏状态。
当 代码执行 (例如TypeScript)允许代理有效地维护状态和结构工作流,这引入了一个新问题: 数据块真实世界的操作(如SRE日志分析或数据库转储)会生成大量的JSON有效载荷,这些有效载荷会爆炸上下文窗口,使有状态执行成本过高。
代码模式 弥合这一差距。它能够:
- 正式执行:运行复杂的TypeScript工作流以维护上下文 外面 模型。
- 上下文效率:使用 TOON压缩 “压缩”结果,使代理能够处理大量数据集,而不会超出其代币预算。
运作原理
graph LR
A[AI Agent<br/>Claude/Cursor] -->|JSON-RPC| B[CodeModeTOON<br/>Server]
B -->|Lazy Load| C[Perplexity]
B -->|Lazy Load| D[Context7]
B -->|Lazy Load| E[Custom Servers]
C -->|Raw JSON| B
D -->|Raw JSON| B
E -->|Raw JSON| B
B -->|TOON<br/>Compressed| A
style B fill:#4f46e5,color:#fff
style A fill:#10b981,color:#fff
数据流:请求通过CodeModeTOON路由→ 服务器按需延迟加载→ 响应在返回代理之前被压缩。
🔥 主要特点
🗜️ TOON压缩
通过以下方式减少令牌使用 30-90% 对于结构化数据。
- 已验证:Kubernetes审计节省约83%
- 最适合:SRE日志、数据库转储、API响应
- 运作原理:模式提取+值压缩
⚡ 延迟加载
服务器仅在需要时启动。未使用工具的零开销。
- 最适合:多工具工作流程,资源受限的环境
- 演出:活动服务器的启动时间低于100毫秒
🔒 沙盒执行
通过自动代理的MCP工具访问来确保JS的安全执行。
- 最适合:复杂的有状态工作流、批处理操作
- 安全:使用Node.js
vm模块(不适用于多租户使用)
🤖 代理友好功能
专为程序化发现和自我纠正而设计。
suggest_approach:推荐最佳执行策略的元工具(代码vs工作流vs直接调用)。- 效率指标:
execute_code返回操作计数和压缩节省,以增强高效行为。 - 恢复提示:错误消息包括代理可采取的后续步骤(例如,“找不到服务器?尝试list_servers”)。
目录
何时使用CodeModeTOON
✅ 非常适合:
- 多步人工智能工作流程 要求国家管理
- 处理大型结构化数据集 (日志、数据库转储、K8s清单)
- 协调多个MCP服务器 并行
- 令牌受限环境 (降低API成本)
❌ 不适合:
- 简单的单工具查询
- 非结构化文本大量响应(压缩<10%)
- 多租户生产服务器(vm模块安全限制)
安装
单击(光标)
手动设置
将此添加到您的 ~/.cursor/mcp.json:
{
"mcpServers": {
"code-mode-toon": {
"type": "stdio",
"command": "npx",
"args": ["-y", "code-mode-toon"],
"env": {
"CODE_MODE_TOON_CONFIG": "~/.cursor/mcp.json"
}
}
}
}
🧠 克劳德技能
CodeModeTOON包括一个预构建的 克劳德技能 让你的人工智能助手成为使用这个编排器的专家。
code-mode-toon-workflow-expert
一种专门的技能,教克劳德如何:
- 决定 何时使用工作流与即席代码。
- 创建 遵循最佳实践的新工作流程。
- 编排 高效地使用多种工具。
安装:
- 解压缩
claude-skills/code-mode-toon-workflow-expert.skill - 将文件夹放在您的
.claude/skills/目录(或通过Claude桌面应用程序导入)。
🤖 AI助手提示
将这些提示复制到AI的自定义指令中(例如。, .cursorrules 或Claude项目说明),以最大限度地发挥CodeModeTOON的潜力。
1.系统标识和编排(基本)
目标: 教导人工智能充当编排器并优先处理工作流程。
YOU ARE AN AGENTIC ORCHESTRATOR. You have access to "CodeModeTOON", a high-efficiency MCP bridge.
1. PRIORITIZE WORKFLOWS: Before running single tools, check `list_workflows`. If a workflow exists (e.g., `research`, `k8s-detective`), USE IT. It is faster and saves tokens.
2. HANDLE COMPRESSED DATA: Outputs may be "TOON encoded" (highly compressed JSON). This is normal. Do not complain about "unreadable data" - simply parse it or ask for specific fields if needed.
3. BATCH OPERATIONS: Never run 3+ sequential tool calls if they can be batched. Use `execute_code` to run them in a single block.
2.工具发现(延迟加载)
目标: 防止AI在工具不可见时放弃。
TOOLS ARE LAZY LOADED. If you need a capability (e.g., "search", "kubernetes", "database") and don't see the tool:
1. DO NOT assume it's missing.
2. RUN `search_tools({ query: "..." })` to find it.
3. RUN `get_tool_api({ serverName: "..." })` to learn how to use it.
4. Only then, execute the tool.
3.效率和TOON压缩
目标: 对大数据操作强制执行令牌保存行为。
OPTIMIZE FOR TOKENS. When fetching large datasets (logs, docs, API responses):
1. ALWAYS wrap the output in `TOON.encode(data)` inside `execute_code`.
2. PREFER structured data (JSON/Objects) over plain text. TOON compresses structure by ~83%, but text by only ~4%.
3. IF synthesizing data, do it server-side (via workflow `synthesize: true`) to avoid pulling raw data into context.
快速开始
安装后,在Claude或Cursor中尝试这个30秒的演示:
// Ask your AI assistant to run this via execute_code
const api = await get_tool_api({ serverName: 'perplexity' });
const result = await servers['perplexity'].perplexity_ask({
messages: [{ role: 'user', content: "Explain TOON compression" }]
});
console.log(result); // See compression in action! ~40% token savings
刚才发生了什么? 响应自动进行了TOON编码,从而节省了令牌。
用法示例
1. Optimized Tool Execution with TOON Compression
// Inside execute_code
const api = await get_tool_api({ serverName: 'perplexity' });
// Request large data - automatically compressed!
const result = await servers['perplexity'].perplexity_ask({
messages: [{ role: 'user', content: "Summarize the history of Rome" }]
});
console.log(result); // Returns TOON-encoded string, saving ~40% tokens
2. Multi-Server Coordination
// Fetch large documentation from Context7
const api = await get_tool_api({ serverName: 'context7' });
const docs = await servers['context7']['get-library-docs']({
context7CompatibleLibraryID: 'kubernetes/kubernetes'
});
console.log(TOON.encode(docs)); // Massive compression on structured data
3. Workflow Orchestration
// Run a complex research workflow
const result = await workflows.research({
goal: "Compare xsync vs sync.Map performance",
queries: ["xsync vs sync.Map benchmarks"],
synthesize: true,
outputFile: "/tmp/research.toon"
});
console.log(result.synthesis); // LLM-synthesized findings
工作流
CodeModeTOON支持 工作流--预定义的服务器端TypeScript模块,用于编排多个MCP工具。
研究工作流程
一个强大的研究助理,他:
- 并行化 从多个来源获取数据(Context7、维基百科、困惑)。
- 合成 使用LLM的发现(可选)。
- 输出TOON编码文件 以获得最大的上下文效率。
- 重试 请求自动失败。
看 .workflows/README.md 有关详细文档、使用示例和AI提示。
业绩基准
为什么这很重要
情景2(节省92%) 展示了CodeModeTOON的优势:
| 度量 | 原始 | TOON | 节省 |
|---|---|---|---|
| 字符数 | 37263 | 2824 | ~83% |
| 估计代币* | ~9315 | ~706 | ~8600个代币 |
| 成本(克劳德·索内特)** | 0.028 | 0.002 | 0.026美元 |
*假设平均4个字符/令牌
***$3/M代币输入定价*
关键洞察:对于基础设施审计、日志分析或数据库转储,TOON压缩可以将令牌成本降低90%以上,使复杂的代理工作流在预算范围内可行。
Detailed Scenarios
场景1:自然语言查询(罗马历史) 正如预期的那样,非结构化文本压缩效果不佳。
- 原始JSON:11651个字符
- TOON编码:11166个字符
- 压缩比: 节省约4.16%
场景2:Kubernetes集群审计(50个Pod) 高度结构化、重复的JSON(基础设施转储)压缩得非常好。
- 原始JSON:37263个字符
- TOON编码:2824个字符
- 压缩比: 节省约83% 📉
故障排除
“找不到服务器”错误
原因:CodeModeTOON找不到您的MCP配置。
解决方案:确保 CODE_MODE_TOON_CONFIG 指向您的配置:
export CODE_MODE_TOON_CONFIG=~/.cursor/mcp.json
TOON编码不起作用
原因:结果未被编码。
解决方案:使用 console.log(TOON.encode(data)),不 console.log(data).
懒惰的服务器无法加载
原因:服务器名称不匹配。
解决方案:验证服务器名称是否与您的配置匹配。使用 get_tool_api({ serverName: 'name' }) 检查可用服务器。
安全说明
⚠️ 这 vm 模块不是安全沙箱。 适用于具有可信代码的个人AI助手(Claude、Cursor)。不适用于多租户或公共服务。
致谢
作者
建造于 齐亚德·哈桑 (高级SRE/DevOps)-- 领英 · GitHub
贡献
欢迎投稿! 🙌
贡献方式
- 报告错误 -打开一个关于复制步骤的问题
- 建议功能 -讨论问题中的用例
- 添加工作流 -请参阅 工作流
- 改进文档 -文档PR始终受到欢迎
开发设置
git clone https://github.com/ziad-hsn/code-mode-toon.git
cd code-mode-toon
npm install
npm test
许可证
MIT许可证——见 许可证 了解详情。
