Skip to content

Commit de82989

Browse files
committed
Remove implementation code from TOON blog post
1 parent 5894ade commit de82989

1 file changed

Lines changed: 3 additions & 62 deletions

File tree

blog/toon-format-support.md

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,11 @@ Adding TOON support seemed simple: set an `Accept: text/toon` header when callin
5151

5252
**The MCP SDK overwrites the Accept header.**
5353

54-
The `mcp` Python library's `_prepare_headers()` method sets:
55-
```python
56-
"Accept": "application/json, text/event-stream"
57-
```
58-
59-
This happens after user headers are merged, meaning our `Accept: text/toon` gets overwritten before the request is sent. No amount of header configuration could fix this.
54+
The `mcp` Python library's `_prepare_headers()` method forces `Accept: application/json, text/event-stream`, and this happens after user headers are merged. No amount of header configuration could fix this.
6055

6156
## The Solution: Custom Header
6257

63-
Instead of fighting the SDK, we introduced a custom header that the SDK doesn't touch:
64-
65-
```
66-
X-Prefer-Format: toon
67-
```
68-
69-
Armory checks this header first, falling back to the standard `Accept` header for non-MCP clients:
70-
71-
```python
72-
def get_preferred_format(request: Request) -> OutputFormat:
73-
# Check custom header first (for MCP clients)
74-
prefer_format = request.headers.get("x-prefer-format", "").lower()
75-
if prefer_format == "toon":
76-
return OutputFormat.TOON
77-
78-
# Fallback to Accept header (for direct HTTP clients)
79-
accept = request.headers.get("accept", "")
80-
if "text/toon" in accept:
81-
return OutputFormat.TOON
82-
83-
return OutputFormat.JSON
84-
```
58+
Instead of fighting the SDK, we introduced a custom header that the SDK doesn't touch: `X-Prefer-Format: toon`. Armory checks this header first, falling back to the standard `Accept` header for non-MCP clients.
8559

8660
## The Full Flow
8761

@@ -100,47 +74,14 @@ The key insight: **conversion happens in Armory, not in backends**. MCP servers
10074

10175
## Smart Conversion
10276

103-
TOON isn't always smaller than JSON. For deeply nested objects or non-uniform arrays, JSON might actually be more compact. Armory's conversion logic handles this:
104-
105-
```python
106-
def to_json_or_toon(data: Any, format: OutputFormat) -> str | Any:
107-
if format == OutputFormat.TOON:
108-
toon_str = to_toon(data)
109-
json_str = json.dumps(data, default=str)
110-
111-
# Only use TOON if it's actually smaller
112-
if len(toon_str) < len(json_str):
113-
return toon_str
114-
115-
return data # Return as object for JSON serialization
116-
```
117-
118-
This ensures TOON is only used when it provides actual benefit.
77+
TOON isn't always smaller than JSON. For deeply nested objects or non-uniform arrays, JSON might actually be more compact. Armory compares both formats and only uses TOON when it provides actual benefit.
11978

12079
## UI Display
12180

12281
Tool results now display differently based on format. JSON results get pretty-printed with indentation. TOON results display as plain text with proper line breaks.
12382

12483
![TOON in forge-ui](/screens/toon-format-ui.png)
12584

126-
The `ToolCallCard` component detects the format:
127-
128-
```typescript
129-
const isStringResult = computed(() => {
130-
return typeof props.toolCall.result === 'string'
131-
})
132-
133-
const formattedResult = computed(() => {
134-
if (isStringResult.value) {
135-
// TOON: display as-is with line breaks
136-
return props.toolCall.result as string
137-
} else {
138-
// JSON: pretty-print
139-
return JSON.stringify(props.toolCall.result, null, 2)
140-
}
141-
})
142-
```
143-
14485
## Results
14586

14687
With TOON enabled, we see 30-60% token reduction on tool results with tabular data (weather forecasts, search results, database queries). The format is especially effective for:

0 commit comments

Comments
 (0)