TOON MCP服务器
](https://www.npmjs.com/package/toon-mcp-server) 
MCP(模型上下文协议)服务器 TOON(面向令牌的对象表示法) 编码。通过以下方式减少LLM令牌的使用 50-70% 在发送结构化数据时。
什么是TOON?
TOON是一种针对LLM输入优化的紧凑数据格式。它使用基于标头的格式,而不是为每个对象重复字段名:
JSON(1041个令牌):
[
{"id": 1, "name": "Product A", "price": 99.99},
{"id": 2, "name": "Product B", "price": 149.99}
]TOON(389个代币):
[id,name,price]
1,Product A,99.99
2,Product B,149.99结果:减少62%的代币=节省62%的成本
安装
快速启动(npx-无需安装)
添加到MCP设置中:
克劳德桌面 (~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"toon": {
"command": "npx",
"args": ["-y", "toon-mcp-server"]
}
}
}克劳德代码 (~/.claude/settings.json):
{
"mcpServers": {
"toon": {
"command": "npx",
"args": ["-y", "toon-mcp-server"]
}
}
}全局安装
npm install -g toon-mcp-server然后添加到MCP设置中:
{
"mcpServers": {
"toon": {
"command": "toon-mcp"
}
}
}作为克劳德代码技能
# Download the skill
curl -o ~/.claude/skills/toon.md https://raw.githubusercontent.com/elminson/toon-mcp/main/skills/toon.md然后使用 /toon 克劳德密码。
可用工具
toon_encode
将数据转换为TOON格式。
支持的格式: JSON、CSV、TSV、XML、HTML表格、YAML
Input: [{"name":"Alice","age":30},{"name":"Bob","age":25}]
Output: [name,age]
Alice,30
Bob,25toon_decode
将TOON转换回JSON。
toon_analyze
分析数据并显示潜在的代币/成本节约。
toon_optimize_prompt
在提示中查找数据段并将其自动转换为TOON。
使用示例
克劳德桌面/代码(带MCP)
请克劳德使用这些工具:
- “将此JSON编码为TOON:\[…\]”
- “分析将此数据转换为TOON可以节省多少”
- “优化此提示以提高令牌效率”
程序化(Node.js)
const { ToonEncoder } = require('toon-mcp-server/src/toon-encoder');
// Encode
const data = [
{ id: 1, name: 'Test', price: 99.99 },
{ id: 2, name: 'Test 2', price: 149.99 },
];
const toon = ToonEncoder.encode(data);
// Get stats
const json = JSON.stringify(data);
const stats = ToonEncoder.getStats(json, toon);
console.log(stats.savings.percent); // "64.5%"
// Decode
const decoded = ToonEncoder.decode(toon);基准测试
使用OpenAI GPT-4o-mini进行测试:
| 数据集大小 | JSON令牌 | TOON令牌 | 节省 |
|---|---|---|---|
| 5项 | 383项 | 192项 | 49.9% |
| 20个项目 | 1394 | 530 | 62% |
| 50个项目 | 3412 | 1204 | 64.7% |
| 100项 | 6800 | 2400 | ~65% |
大规模节省成本
| 交易量 | GPT-4o-mini | GPT-40 | 克劳德·索内特 |
|---|---|---|---|
| 100万个请求 | 节省489美元 | 节省8158美元 | 节省9789美元 |
| 1000万个请求 | 节省4890美元 | 节省81580美元 | 节省97890美元 |
何时使用TOON
✅ 最适合:
- 具有相同结构的对象数组(表、列表、记录)
- API响应、数据库结果
- 发送给LLM的大型数据集
- 大规模成本优化
⚠️ 对以下方面效果较差:
- 深度嵌套的非均匀数据
- 小有效载荷(\<5个项目)
- 具有许多独特字段结构的数据
贡献
欢迎拉取请求!请先打开一个问题来讨论更改。
许可证
麻省理工学院

