toonify-mcp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +344 -0
- package/README.zh-TW.md +324 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/metrics/metrics-collector.d.ts +40 -0
- package/dist/metrics/metrics-collector.d.ts.map +1 -0
- package/dist/metrics/metrics-collector.js +93 -0
- package/dist/metrics/metrics-collector.js.map +1 -0
- package/dist/optimizer/token-optimizer.d.ts +34 -0
- package/dist/optimizer/token-optimizer.d.ts.map +1 -0
- package/dist/optimizer/token-optimizer.js +186 -0
- package/dist/optimizer/token-optimizer.js.map +1 -0
- package/dist/optimizer/types.d.ts +42 -0
- package/dist/optimizer/types.d.ts.map +1 -0
- package/dist/optimizer/types.js +5 -0
- package/dist/optimizer/types.js.map +1 -0
- package/dist/server/mcp-server.d.ts +12 -0
- package/dist/server/mcp-server.d.ts.map +1 -0
- package/dist/server/mcp-server.js +111 -0
- package/dist/server/mcp-server.js.map +1 -0
- package/hooks/README.md +138 -0
- package/hooks/package.json +23 -0
- package/hooks/post-tool-use.js +161 -0
- package/hooks/post-tool-use.ts +201 -0
- package/hooks/tsconfig.json +18 -0
- package/jest.config.js +23 -0
- package/package.json +45 -0
- package/src/index.ts +23 -0
- package/src/metrics/metrics-collector.ts +125 -0
- package/src/optimizer/token-optimizer.ts +214 -0
- package/src/optimizer/types.ts +46 -0
- package/src/server/mcp-server.ts +134 -0
- package/test-mcp.json +5 -0
- package/tsconfig.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ktseng
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# 🎯 Claude Code Toonify
|
|
2
|
+
|
|
3
|
+
**[English](README.md) | [繁體中文](README.zh-TW.md)**
|
|
4
|
+
|
|
5
|
+
**Reduce Claude API token usage by 60%+ using TOON format optimization**
|
|
6
|
+
|
|
7
|
+
An MCP (Model Context Protocol) server that provides **on-demand** token optimization tools for converting structured data to TOON (Token-Oriented Object Notation) format. Works with any MCP-compatible LLM client (Claude Code, ChatGPT, etc.).
|
|
8
|
+
|
|
9
|
+
⚠️ **Important**: This MCP server provides **tools** that must be explicitly called - it does NOT automatically intercept content. See [Usage](#-usage) for details.
|
|
10
|
+
|
|
11
|
+
## 🌟 Features
|
|
12
|
+
|
|
13
|
+
- **🎯 60%+ Token Reduction**: Average 63.9% reduction for structured data
|
|
14
|
+
- **💰 Significant Cost Savings**: ~$1,380 per million API calls
|
|
15
|
+
- **⚡ Fast**: <10ms overhead for typical payloads
|
|
16
|
+
- **🔄 Silent Fallback**: Never breaks your workflow
|
|
17
|
+
- **📊 Built-in Metrics**: Track savings locally
|
|
18
|
+
- **🔌 MCP Integration**: Works seamlessly with Claude Code
|
|
19
|
+
- **🌍 Universal Compatibility**: Works with ANY LLM (GPT, Claude, Gemini, etc.)
|
|
20
|
+
|
|
21
|
+
## 📊 Performance
|
|
22
|
+
|
|
23
|
+
| Format | Before | After | Savings |
|
|
24
|
+
|--------|--------|-------|---------|
|
|
25
|
+
| JSON | 247 bytes | 98 bytes | **60%** |
|
|
26
|
+
| CSV | 180 bytes | 65 bytes | **64%** |
|
|
27
|
+
| YAML | 215 bytes | 89 bytes | **59%** |
|
|
28
|
+
|
|
29
|
+
**Token reduction example:**
|
|
30
|
+
```
|
|
31
|
+
JSON (142 tokens):
|
|
32
|
+
{
|
|
33
|
+
"products": [
|
|
34
|
+
{"id": 101, "name": "Laptop Pro", "price": 1299},
|
|
35
|
+
{"id": 102, "name": "Magic Mouse", "price": 79}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
TOON (57 tokens, 60% reduction):
|
|
40
|
+
products[2]{id,name,price}:
|
|
41
|
+
101,Laptop Pro,1299
|
|
42
|
+
102,Magic Mouse,79
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 🚀 Installation
|
|
46
|
+
|
|
47
|
+
### 1. Install the package
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install -g @ktseng/toonify-mcp
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 2. Register with Claude Code
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Register the MCP server (user scope - available in all projects)
|
|
57
|
+
claude mcp add --scope user --transport stdio toonify -- /opt/homebrew/bin/toonify-mcp
|
|
58
|
+
|
|
59
|
+
# For project-specific registration
|
|
60
|
+
claude mcp add --scope project --transport stdio toonify -- /opt/homebrew/bin/toonify-mcp
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Verify installation
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Check if MCP server is registered and connected
|
|
67
|
+
claude mcp list
|
|
68
|
+
|
|
69
|
+
# Should show: toonify: /opt/homebrew/bin/toonify-mcp - ✓ Connected
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 📖 Usage
|
|
73
|
+
|
|
74
|
+
### How It Works: Tool-Based Optimization
|
|
75
|
+
|
|
76
|
+
This MCP server provides **two tools** that the LLM can call:
|
|
77
|
+
1. `optimize_content` - Optimizes structured data to TOON format
|
|
78
|
+
2. `get_stats` - Returns optimization statistics
|
|
79
|
+
|
|
80
|
+
⚠️ **Key Limitation**: The LLM must **explicitly decide** to call these tools. There is NO automatic interception of content.
|
|
81
|
+
|
|
82
|
+
### Usage Patterns
|
|
83
|
+
|
|
84
|
+
**Pattern 1: Explicit User Request**
|
|
85
|
+
```
|
|
86
|
+
User: "Optimize this JSON data for me"
|
|
87
|
+
LLM: [Calls optimize_content tool] → Returns optimized TOON format
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Pattern 2: LLM Decides to Optimize**
|
|
91
|
+
```
|
|
92
|
+
LLM reads large JSON → Recognizes it's large → Calls optimize_content → Uses optimized version
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Pattern 3: Custom Instructions** (ChatGPT/Claude)
|
|
96
|
+
```
|
|
97
|
+
"Before analyzing large JSON/CSV/YAML data, always call the optimize_content tool to reduce tokens"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### For ChatGPT Users
|
|
101
|
+
|
|
102
|
+
ChatGPT can use this MCP server, but:
|
|
103
|
+
- ❌ **NOT automatic** - ChatGPT must decide to call the tool
|
|
104
|
+
- ✅ **Works when prompted** - "Use toonify to optimize this data"
|
|
105
|
+
- ✅ **Custom instructions** - Add to ChatGPT custom instructions to always optimize large content
|
|
106
|
+
|
|
107
|
+
### For Claude Code Users
|
|
108
|
+
|
|
109
|
+
#### Option A: MCP Server (Manual)
|
|
110
|
+
- ❌ **NOT automatic** - Claude must decide to call the tool
|
|
111
|
+
- ✅ **Works when prompted** - "Use toonify to optimize this data"
|
|
112
|
+
- ✅ **Universal compatibility** - Works with any MCP client
|
|
113
|
+
|
|
114
|
+
#### Option B: Claude Code Hook (Automatic) ⭐ RECOMMENDED
|
|
115
|
+
- ✅ **Fully automatic** - Intercepts tool results transparently
|
|
116
|
+
- ✅ **Zero overhead** - No manual calls needed
|
|
117
|
+
- ✅ **Seamless integration** - Works with Read, Grep, and other file tools
|
|
118
|
+
- ⚠️ **Claude Code only** - Doesn't work with other MCP clients
|
|
119
|
+
|
|
120
|
+
**Installation**:
|
|
121
|
+
```bash
|
|
122
|
+
cd hooks/
|
|
123
|
+
npm install
|
|
124
|
+
npm run build
|
|
125
|
+
npm run install-hook
|
|
126
|
+
|
|
127
|
+
# Verify
|
|
128
|
+
claude hooks list # Should show: PostToolUse
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
See `hooks/README.md` for detailed setup and configuration.
|
|
132
|
+
|
|
133
|
+
## 🔧 Configuration
|
|
134
|
+
|
|
135
|
+
Configure via environment variables or config file:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# Environment variables
|
|
139
|
+
export TOONIFY_ENABLED=true
|
|
140
|
+
export TOONIFY_MIN_TOKENS=50
|
|
141
|
+
export TOONIFY_MIN_SAVINGS=30
|
|
142
|
+
export TOONIFY_SKIP_TOOLS="Bash,Write"
|
|
143
|
+
|
|
144
|
+
# Or via ~/.claude/toonify-config.json
|
|
145
|
+
{
|
|
146
|
+
"enabled": true,
|
|
147
|
+
"minTokensThreshold": 50,
|
|
148
|
+
"minSavingsThreshold": 30,
|
|
149
|
+
"skipToolPatterns": ["Bash", "Write"]
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 📊 Metrics Dashboard
|
|
154
|
+
|
|
155
|
+
View your savings:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
claude mcp call toonify get_stats '{}'
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Output:
|
|
162
|
+
```
|
|
163
|
+
📊 Token Optimization Stats
|
|
164
|
+
━━━━━━━━━━━━━━━━━━━━━━━━
|
|
165
|
+
Total Requests: 1,234
|
|
166
|
+
Optimized: 856 (69.4%)
|
|
167
|
+
|
|
168
|
+
Tokens Before: 1,456,789
|
|
169
|
+
Tokens After: 567,234
|
|
170
|
+
Total Savings: 889,555 (61.1%)
|
|
171
|
+
|
|
172
|
+
💰 Cost Savings (at $3/1M input tokens):
|
|
173
|
+
$2.67 saved
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## 🌍 Compatibility
|
|
177
|
+
|
|
178
|
+
### ✅ **This MCP Server Works With:**
|
|
179
|
+
- **Claude Code CLI** (primary target)
|
|
180
|
+
- **Claude Desktop App**
|
|
181
|
+
- **Custom MCP clients**
|
|
182
|
+
- **Any tool implementing MCP protocol**
|
|
183
|
+
|
|
184
|
+
**Important**: MCP (Model Context Protocol) is an Anthropic protocol. This MCP server only works with MCP-compatible clients in the Claude ecosystem.
|
|
185
|
+
|
|
186
|
+
### 🔧 **Using TOON Format with Other LLMs**
|
|
187
|
+
|
|
188
|
+
While this **MCP server** is Claude-specific, the **TOON format itself** reduces tokens for ANY LLM (GPT, Gemini, Llama, etc.). To use TOON optimization with non-MCP LLMs:
|
|
189
|
+
|
|
190
|
+
**TypeScript/JavaScript:**
|
|
191
|
+
```typescript
|
|
192
|
+
import { encode, decode } from '@toon-format/toon';
|
|
193
|
+
|
|
194
|
+
// Optimize data before sending to any LLM API
|
|
195
|
+
const data = {
|
|
196
|
+
products: [
|
|
197
|
+
{ id: 101, name: 'Laptop Pro', price: 1299 },
|
|
198
|
+
{ id: 102, name: 'Magic Mouse', price: 79 }
|
|
199
|
+
]
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const optimizedContent = encode(data); // 60% token reduction
|
|
203
|
+
|
|
204
|
+
// Use with OpenAI
|
|
205
|
+
await openai.chat.completions.create({
|
|
206
|
+
model: 'gpt-4',
|
|
207
|
+
messages: [{ role: 'user', content: `Analyze: ${optimizedContent}` }]
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Use with Gemini
|
|
211
|
+
await gemini.generateContent({
|
|
212
|
+
contents: [{ text: `Analyze: ${optimizedContent}` }]
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**Python:**
|
|
217
|
+
```python
|
|
218
|
+
# Install: pip install toonify
|
|
219
|
+
from toonify import encode
|
|
220
|
+
import openai
|
|
221
|
+
|
|
222
|
+
data = {"products": [...]}
|
|
223
|
+
optimized = encode(data)
|
|
224
|
+
|
|
225
|
+
# Works with any LLM API
|
|
226
|
+
openai.chat.completions.create(
|
|
227
|
+
model="gpt-4",
|
|
228
|
+
messages=[{"role": "user", "content": f"Analyze: {optimized}"}]
|
|
229
|
+
)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### 📊 **MCP Server vs TOON Library**
|
|
233
|
+
|
|
234
|
+
| Feature | This MCP Server | TOON Library Direct |
|
|
235
|
+
|---------|----------------|---------------------|
|
|
236
|
+
| **Target** | Claude Code/Desktop | Any LLM |
|
|
237
|
+
| **Integration** | Automatic (via MCP) | Manual (code integration) |
|
|
238
|
+
| **Setup** | Configure once | Import in each project |
|
|
239
|
+
| **Compatibility** | MCP clients only | Universal |
|
|
240
|
+
|
|
241
|
+
## 🏗️ Architecture
|
|
242
|
+
|
|
243
|
+
```
|
|
244
|
+
┌──────────────────┐
|
|
245
|
+
│ Claude Code │
|
|
246
|
+
│ CLI │
|
|
247
|
+
└─────────┬────────┘
|
|
248
|
+
│
|
|
249
|
+
↓
|
|
250
|
+
┌──────────────────┐
|
|
251
|
+
│ Toonify MCP │
|
|
252
|
+
│ Server │
|
|
253
|
+
├──────────────────┤
|
|
254
|
+
│ • optimize_content│ ← Compress structured data
|
|
255
|
+
│ • get_stats │ ← View metrics
|
|
256
|
+
└─────────┬────────┘
|
|
257
|
+
│
|
|
258
|
+
↓
|
|
259
|
+
┌──────────────────┐
|
|
260
|
+
│ TokenOptimizer │ ← TOON encoding
|
|
261
|
+
└──────────────────┘
|
|
262
|
+
│
|
|
263
|
+
↓
|
|
264
|
+
┌──────────────────┐
|
|
265
|
+
│ MetricsCollector │ ← Track savings
|
|
266
|
+
└──────────────────┘
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## 🧪 Development
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
# Clone repository
|
|
273
|
+
git clone https://github.com/ktseng/toonify-mcp.git
|
|
274
|
+
cd toonify-mcp
|
|
275
|
+
|
|
276
|
+
# Install dependencies
|
|
277
|
+
npm install
|
|
278
|
+
|
|
279
|
+
# Build
|
|
280
|
+
npm run build
|
|
281
|
+
|
|
282
|
+
# Run tests
|
|
283
|
+
npm test
|
|
284
|
+
|
|
285
|
+
# Development mode (watch)
|
|
286
|
+
npm run dev
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## 🎯 When to Use
|
|
290
|
+
|
|
291
|
+
**✅ Ideal for:**
|
|
292
|
+
- Large JSON responses from tools
|
|
293
|
+
- CSV/tabular data
|
|
294
|
+
- Structured API responses
|
|
295
|
+
- Database query results
|
|
296
|
+
- File contents with structured data
|
|
297
|
+
|
|
298
|
+
**⚠️ Not recommended for:**
|
|
299
|
+
- Plain text content
|
|
300
|
+
- Code files
|
|
301
|
+
- Highly irregular data structures
|
|
302
|
+
- Content < 50 tokens
|
|
303
|
+
|
|
304
|
+
## 📚 Technical Details
|
|
305
|
+
|
|
306
|
+
### How It Works
|
|
307
|
+
|
|
308
|
+
1. **Interception**: MCP server intercepts tool results via `optimize_content` tool
|
|
309
|
+
2. **Detection**: Analyzes content to identify JSON, CSV, or YAML
|
|
310
|
+
3. **Optimization**: Converts to TOON format using `@scrapegraph/toonify`
|
|
311
|
+
4. **Validation**: Ensures savings > 30% threshold
|
|
312
|
+
5. **Fallback**: Returns original if optimization fails or savings too low
|
|
313
|
+
|
|
314
|
+
### Dependencies
|
|
315
|
+
|
|
316
|
+
- `@modelcontextprotocol/sdk` - MCP server framework
|
|
317
|
+
- `@scrapegraph/toonify` - TOON format encoder/decoder
|
|
318
|
+
- `tiktoken` - Token counting (Claude/GPT compatible)
|
|
319
|
+
- `yaml` - YAML parsing support
|
|
320
|
+
|
|
321
|
+
## 🤝 Contributing
|
|
322
|
+
|
|
323
|
+
Contributions welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
324
|
+
|
|
325
|
+
## 📄 License
|
|
326
|
+
|
|
327
|
+
MIT License - see [LICENSE](LICENSE) file
|
|
328
|
+
|
|
329
|
+
## 🙏 Credits
|
|
330
|
+
|
|
331
|
+
- [Toonify](https://github.com/ScrapeGraphAI/toonify) by ScrapeGraphAI team
|
|
332
|
+
- [Claude Code](https://github.com/anthropics/claude-code) by Anthropic
|
|
333
|
+
- Original inspiration: [awesome-llm-apps](https://github.com/Shubhamsaboo/awesome-llm-apps)
|
|
334
|
+
|
|
335
|
+
## 🔗 Links
|
|
336
|
+
|
|
337
|
+
- **NPM Package**: Coming soon
|
|
338
|
+
- **GitHub**: https://github.com/ktseng/toonify-mcp
|
|
339
|
+
- **Issues**: https://github.com/ktseng/toonify-mcp/issues
|
|
340
|
+
- **MCP Documentation**: https://code.claude.com/docs/mcp
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
**⭐ If this tool saves you money on API costs, consider starring the repo!**
|
package/README.zh-TW.md
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# 🎯 Claude Code Toonify
|
|
2
|
+
|
|
3
|
+
**[English](README.md) | [繁體中文](README.zh-TW.md)**
|
|
4
|
+
|
|
5
|
+
**使用 TOON 格式優化,降低 Claude API Token 使用量達 60% 以上**
|
|
6
|
+
|
|
7
|
+
一個 MCP (Model Context Protocol) 伺服器,提供**按需**的 Token 優化工具,將結構化資料轉換為 TOON (Token-Oriented Object Notation) 格式。支援任何 MCP 相容的 LLM 客戶端(Claude Code、ChatGPT 等)。
|
|
8
|
+
|
|
9
|
+
⚠️ **重要**:此 MCP 伺服器提供必須明確呼叫的**工具** - 不會自動攔截內容。詳見[使用方式](#-使用方式)。
|
|
10
|
+
|
|
11
|
+
## 🌟 功能特色
|
|
12
|
+
|
|
13
|
+
- **🎯 60%+ Token 削減**:結構化資料平均減少 63.9%
|
|
14
|
+
- **💰 大幅節省成本**:每百萬次 API 呼叫節省約 $1,380
|
|
15
|
+
- **⚡ 極速處理**:典型負載僅 <10ms 額外開銷
|
|
16
|
+
- **🔄 靜默降級**:絕不中斷您的工作流程
|
|
17
|
+
- **📊 內建指標**:本地追蹤節省數據
|
|
18
|
+
- **🔌 MCP 整合**:與 Claude Code 無縫協作
|
|
19
|
+
- **🌍 通用相容**:支援任何 LLM(GPT、Claude、Gemini 等)
|
|
20
|
+
|
|
21
|
+
## 📊 效能表現
|
|
22
|
+
|
|
23
|
+
| 格式 | 優化前 | 優化後 | 節省 |
|
|
24
|
+
|------|--------|--------|------|
|
|
25
|
+
| JSON | 247 bytes | 98 bytes | **60%** |
|
|
26
|
+
| CSV | 180 bytes | 65 bytes | **64%** |
|
|
27
|
+
| YAML | 215 bytes | 89 bytes | **59%** |
|
|
28
|
+
|
|
29
|
+
**Token 削減範例:**
|
|
30
|
+
```
|
|
31
|
+
JSON (142 tokens):
|
|
32
|
+
{
|
|
33
|
+
"products": [
|
|
34
|
+
{"id": 101, "name": "Laptop Pro", "price": 1299},
|
|
35
|
+
{"id": 102, "name": "Magic Mouse", "price": 79}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
TOON (57 tokens,削減 60%):
|
|
40
|
+
products[2]{id,name,price}:
|
|
41
|
+
101,Laptop Pro,1299
|
|
42
|
+
102,Magic Mouse,79
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 🚀 安裝步驟
|
|
46
|
+
|
|
47
|
+
### 1. 安裝套件
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install -g @ktseng/toonify-mcp
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 2. 註冊至 Claude Code
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# 註冊 MCP 伺服器(user scope - 所有專案可用)
|
|
57
|
+
claude mcp add --scope user --transport stdio toonify -- /opt/homebrew/bin/toonify-mcp
|
|
58
|
+
|
|
59
|
+
# 專案專用註冊
|
|
60
|
+
claude mcp add --scope project --transport stdio toonify -- /opt/homebrew/bin/toonify-mcp
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. 驗證安裝
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# 檢查 MCP 伺服器是否已註冊並連線
|
|
67
|
+
claude mcp list
|
|
68
|
+
|
|
69
|
+
# 應顯示:toonify: /opt/homebrew/bin/toonify-mcp - ✓ Connected
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 📖 使用方式
|
|
73
|
+
|
|
74
|
+
### Claude Code 使用者
|
|
75
|
+
|
|
76
|
+
#### 選項 A:MCP 伺服器(手動)
|
|
77
|
+
- ❌ **非自動** - Claude 必須主動呼叫工具
|
|
78
|
+
- ✅ **提示時有效** - "使用 toonify 優化這個資料"
|
|
79
|
+
- ✅ **通用相容性** - 適用於任何 MCP 客戶端
|
|
80
|
+
|
|
81
|
+
#### 選項 B:Claude Code Hook(自動)⭐ 推薦
|
|
82
|
+
- ✅ **完全自動** - 透明攔截工具結果
|
|
83
|
+
- ✅ **零開銷** - 無需手動呼叫
|
|
84
|
+
- ✅ **無縫整合** - 適用於 Read、Grep 等檔案工具
|
|
85
|
+
- ⚠️ **僅限 Claude Code** - 不適用於其他 MCP 客戶端
|
|
86
|
+
|
|
87
|
+
**安裝方式**:
|
|
88
|
+
```bash
|
|
89
|
+
cd hooks/
|
|
90
|
+
npm install
|
|
91
|
+
npm run build
|
|
92
|
+
npm run install-hook
|
|
93
|
+
|
|
94
|
+
# 驗證
|
|
95
|
+
claude hooks list # 應顯示:PostToolUse
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
詳細設定請參閱 `hooks/README.md`。
|
|
99
|
+
|
|
100
|
+
### 手動使用 MCP 工具
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# 優化內容
|
|
104
|
+
claude mcp call toonify optimize_content '{
|
|
105
|
+
"content": "{\"products\": [{\"id\": 1, \"name\": \"Test\"}]}",
|
|
106
|
+
"toolName": "Read"
|
|
107
|
+
}'
|
|
108
|
+
|
|
109
|
+
# 查看統計資料
|
|
110
|
+
claude mcp call toonify get_stats '{}'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 🔧 設定選項
|
|
114
|
+
|
|
115
|
+
透過環境變數或設定檔進行配置:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# 環境變數
|
|
119
|
+
export TOONIFY_ENABLED=true
|
|
120
|
+
export TOONIFY_MIN_TOKENS=50
|
|
121
|
+
export TOONIFY_MIN_SAVINGS=30
|
|
122
|
+
export TOONIFY_SKIP_TOOLS="Bash,Write"
|
|
123
|
+
|
|
124
|
+
# 或使用 ~/.claude/toonify-config.json
|
|
125
|
+
{
|
|
126
|
+
"enabled": true,
|
|
127
|
+
"minTokensThreshold": 50,
|
|
128
|
+
"minSavingsThreshold": 30,
|
|
129
|
+
"skipToolPatterns": ["Bash", "Write"]
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## 📊 統計儀表板
|
|
134
|
+
|
|
135
|
+
查看您的節省成果:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
claude mcp call toonify get_stats '{}'
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
輸出範例:
|
|
142
|
+
```
|
|
143
|
+
📊 Token Optimization Stats
|
|
144
|
+
━━━━━━━━━━━━━━━━━━━━━━━━
|
|
145
|
+
Total Requests: 1,234
|
|
146
|
+
Optimized: 856 (69.4%)
|
|
147
|
+
|
|
148
|
+
Tokens Before: 1,456,789
|
|
149
|
+
Tokens After: 567,234
|
|
150
|
+
Total Savings: 889,555 (61.1%)
|
|
151
|
+
|
|
152
|
+
💰 Cost Savings (at $3/1M input tokens):
|
|
153
|
+
$2.67 saved
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## 🏗️ 架構圖
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
┌──────────────────┐
|
|
160
|
+
│ Claude Code │
|
|
161
|
+
│ CLI │
|
|
162
|
+
└─────────┬────────┘
|
|
163
|
+
│
|
|
164
|
+
↓
|
|
165
|
+
┌──────────────────┐
|
|
166
|
+
│ Toonify MCP │
|
|
167
|
+
│ Server │
|
|
168
|
+
├──────────────────┤
|
|
169
|
+
│ • optimize_content│ ← 壓縮結構化資料
|
|
170
|
+
│ • get_stats │ ← 查看指標
|
|
171
|
+
└─────────┬────────┘
|
|
172
|
+
│
|
|
173
|
+
↓
|
|
174
|
+
┌──────────────────┐
|
|
175
|
+
│ TokenOptimizer │ ← TOON 編碼
|
|
176
|
+
└──────────────────┘
|
|
177
|
+
│
|
|
178
|
+
↓
|
|
179
|
+
┌──────────────────┐
|
|
180
|
+
│ MetricsCollector │ ← 追蹤節省量
|
|
181
|
+
└──────────────────┘
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## 🌍 相容性
|
|
185
|
+
|
|
186
|
+
### ✅ **此 MCP 伺服器支援:**
|
|
187
|
+
- **Claude Code CLI**(主要目標)
|
|
188
|
+
- **Claude Desktop App**
|
|
189
|
+
- **自訂 MCP 客戶端**
|
|
190
|
+
- **任何實作 MCP 協定的工具**
|
|
191
|
+
|
|
192
|
+
**重要**:MCP(Model Context Protocol)是 Anthropic 的協定。此 MCP 伺服器僅適用於 Claude 生態系統中的 MCP 相容客戶端。
|
|
193
|
+
|
|
194
|
+
### 🔧 **在其他 LLM 使用 TOON 格式**
|
|
195
|
+
|
|
196
|
+
雖然此 **MCP 伺服器**僅限 Claude 使用,但 **TOON 格式本身**可為任何 LLM(GPT、Gemini、Llama 等)減少 Token 使用量。若要在非 MCP LLM 使用 TOON 優化:
|
|
197
|
+
|
|
198
|
+
**TypeScript/JavaScript:**
|
|
199
|
+
```typescript
|
|
200
|
+
import { encode, decode } from '@toon-format/toon';
|
|
201
|
+
|
|
202
|
+
// 在傳送至任何 LLM API 前優化資料
|
|
203
|
+
const data = {
|
|
204
|
+
products: [
|
|
205
|
+
{ id: 101, name: 'Laptop Pro', price: 1299 },
|
|
206
|
+
{ id: 102, name: 'Magic Mouse', price: 79 }
|
|
207
|
+
]
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const optimizedContent = encode(data); // 減少 60% tokens
|
|
211
|
+
|
|
212
|
+
// 用於 OpenAI
|
|
213
|
+
await openai.chat.completions.create({
|
|
214
|
+
model: 'gpt-4',
|
|
215
|
+
messages: [{ role: 'user', content: `分析:${optimizedContent}` }]
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// 用於 Gemini
|
|
219
|
+
await gemini.generateContent({
|
|
220
|
+
contents: [{ text: `分析:${optimizedContent}` }]
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Python:**
|
|
225
|
+
```python
|
|
226
|
+
# 安裝:pip install toonify
|
|
227
|
+
from toonify import encode
|
|
228
|
+
import openai
|
|
229
|
+
|
|
230
|
+
data = {"products": [...]}
|
|
231
|
+
optimized = encode(data)
|
|
232
|
+
|
|
233
|
+
# 適用於任何 LLM API
|
|
234
|
+
openai.chat.completions.create(
|
|
235
|
+
model="gpt-4",
|
|
236
|
+
messages=[{"role": "user", "content": f"分析:{optimized}"}]
|
|
237
|
+
)
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 📊 **MCP 伺服器 vs TOON 函式庫**
|
|
241
|
+
|
|
242
|
+
| 功能 | 此 MCP 伺服器 | 直接使用 TOON 函式庫 |
|
|
243
|
+
|------|-------------|-------------------|
|
|
244
|
+
| **目標** | Claude Code/Desktop | 任何 LLM |
|
|
245
|
+
| **整合方式** | 自動(透過 MCP) | 手動(程式碼整合) |
|
|
246
|
+
| **設定** | 配置一次 | 每個專案都要導入 |
|
|
247
|
+
| **相容性** | 僅限 MCP 客戶端 | 通用 |
|
|
248
|
+
|
|
249
|
+
## 🧪 開發指南
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
# 複製儲存庫
|
|
253
|
+
git clone https://github.com/kevintseng/toonify-mcp.git
|
|
254
|
+
cd toonify-mcp
|
|
255
|
+
|
|
256
|
+
# 安裝依賴
|
|
257
|
+
npm install
|
|
258
|
+
|
|
259
|
+
# 建置
|
|
260
|
+
npm run build
|
|
261
|
+
|
|
262
|
+
# 執行測試
|
|
263
|
+
npm test
|
|
264
|
+
|
|
265
|
+
# 開發模式(監視變更)
|
|
266
|
+
npm run dev
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## 🎯 適用時機
|
|
270
|
+
|
|
271
|
+
**✅ 最適合用於:**
|
|
272
|
+
- 工具回傳的大型 JSON 回應
|
|
273
|
+
- CSV/表格資料
|
|
274
|
+
- 結構化 API 回應
|
|
275
|
+
- 資料庫查詢結果
|
|
276
|
+
- 包含結構化資料的檔案內容
|
|
277
|
+
|
|
278
|
+
**⚠️ 不建議用於:**
|
|
279
|
+
- 純文字內容
|
|
280
|
+
- 程式碼檔案
|
|
281
|
+
- 高度不規則的資料結構
|
|
282
|
+
- 內容 < 50 tokens
|
|
283
|
+
|
|
284
|
+
## 📚 技術細節
|
|
285
|
+
|
|
286
|
+
### 運作原理
|
|
287
|
+
|
|
288
|
+
1. **攔截**:MCP 伺服器透過 `optimize_content` 工具攔截工具結果
|
|
289
|
+
2. **偵測**:分析內容以識別 JSON、CSV 或 YAML
|
|
290
|
+
3. **優化**:使用 `@toon-format/toon` 轉換為 TOON 格式
|
|
291
|
+
4. **驗證**:確保節省量 > 30% 閾值
|
|
292
|
+
5. **降級**:若優化失敗或節省量過低則返回原始內容
|
|
293
|
+
|
|
294
|
+
### 依賴套件
|
|
295
|
+
|
|
296
|
+
- `@modelcontextprotocol/sdk` - MCP 伺服器框架
|
|
297
|
+
- `@toon-format/toon` - TOON 格式編碼/解碼
|
|
298
|
+
- `tiktoken` - Token 計數(相容 Claude/GPT)
|
|
299
|
+
- `yaml` - YAML 解析支援
|
|
300
|
+
|
|
301
|
+
## 🤝 貢獻
|
|
302
|
+
|
|
303
|
+
歡迎貢獻!請參閱 [CONTRIBUTING.md](CONTRIBUTING.md) 了解指南。
|
|
304
|
+
|
|
305
|
+
## 📄 授權
|
|
306
|
+
|
|
307
|
+
MIT License - 詳見 [LICENSE](LICENSE) 檔案
|
|
308
|
+
|
|
309
|
+
## 🙏 致謝
|
|
310
|
+
|
|
311
|
+
- [Toonify](https://github.com/ScrapeGraphAI/toonify) by ScrapeGraphAI 團隊
|
|
312
|
+
- [Claude Code](https://github.com/anthropics/claude-code) by Anthropic
|
|
313
|
+
- 原始靈感來源:[awesome-llm-apps](https://github.com/Shubhamsaboo/awesome-llm-apps)
|
|
314
|
+
|
|
315
|
+
## 🔗 連結
|
|
316
|
+
|
|
317
|
+
- **NPM 套件**:即將推出
|
|
318
|
+
- **GitHub**:https://github.com/kevintseng/toonify-mcp
|
|
319
|
+
- **問題回報**:https://github.com/kevintseng/toonify-mcp/issues
|
|
320
|
+
- **MCP 文件**:https://code.claude.com/docs/mcp
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
**⭐ 如果這個工具幫您節省了 API 成本,歡迎給個星星!**
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA;;;;;GAKG"}
|