-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Labels
good first issueGood for newcomersGood for newcomers
Description
Rationale
Large JSON responses from MCP tools consume significant tokens when passed to LLMs, impacting three critical dimensions:
- Accuracy: Token limits force truncation of valuable data, reducing context completeness
- Cost: More tokens = higher API costs (especially with large context windows)
- Latency: Larger payloads increase transmission and processing time
By compressing JSON responses, we can reduce token consumption while preserving all data, leading to better accuracy, lower costs, and faster response times.
Expected Token Size Improvement
Based on analysis of typical tool responses, we expect 30%-60% token reduction depending on the tool and output size. Tools returning arrays of uniform objects with larger datasets see the highest compression ratios.
Tools with Highest Impact:
list_llm_modelssearch_tracessearch_spansfind_errors(nested arrays)get_llm_expensive_tracesget_llm_slow_traceslist_llm_toolsget_trace(nested spans array)
Implementation Approach
Column/Row JSON Format (Recommended)
Convert arrays of objects with identical keys into tabular format:
// Before
{"models": [
{"model": "gpt-4", "provider": "openai", "count": 48},
{"model": "gpt-3.5", "provider": "openai", "count": 12}
]}
// After
{"models": {
"columns": ["model", "provider", "count"],
"rows": [["gpt-4", "openai", 48], ["gpt-3.5", "openai", 12]]
}}Benefits:
- ✅ Returns valid JSON (MCP-compatible)
- ✅ Human-readable
- ✅ No client-side decoding needed
- ✅ Works with existing JSON parsers
Alternative: TOON Format
See TOON format specification for details.
Implementation Plan
Where to Apply Changes
Apply compression to 8 tool response functions:
src/opentelemetry_mcp/tools/list_models.py-list_models()src/opentelemetry_mcp/tools/search.py-search_traces()src/opentelemetry_mcp/tools/search_spans.py-search_spans()src/opentelemetry_mcp/tools/errors.py-find_errors()src/opentelemetry_mcp/tools/expensive_traces.py-get_expensive_traces()src/opentelemetry_mcp/tools/slow_traces.py-get_slow_traces()src/opentelemetry_mcp/tools/list_llm_tools.py-list_llm_tools()src/opentelemetry_mcp/tools/trace.py-get_trace()(compress nested spans array)
Steps
- Add compression utility: Implement tabular compression function (convert arrays of uniform objects to column/row format)
- Apply in tool functions: Compress arrays before returning JSON (only if compression ratio > threshold, e.g., 5%)
- Optional: Add config option to enable/disable compression
Example Pattern
from opentelemetry_mcp.compression import compact_json
import json
async def list_models(...) -> str:
# ... existing logic ...
result = {
"count": len(models_list),
"models": models_list,
}
compressed_result = compact_json(result)
return json.dumps(compressed_result, indent=2)Open Questions
- Should compression be enabled by default or opt-in?
- What minimum compression ratio threshold should be used?
- add a configuration option to disable compression?
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomers