feat: add tool response compression for token optimization (closes #11) - #49
feat: add tool response compression for token optimization (closes #11)#49Suhasrv2403 wants to merge 12 commits into
Conversation
…into feat/tool-response-compression
|
Warning Review limit reached
Next review available in: 13 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdds optional JSON response compression to all MCP tool functions. A new ChangesResponse Compression Feature
IDE Project Files
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (1)
.idea/misc.xml (1)
3-6: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMachine-specific SDK names may cause portability issues.
The SDK names
Python 3.11 (localDEEnv)andPython 3.11 (Quantum)appear to be developer-local environment names. Other contributors opening this project in IntelliJ will need to reconfigure their Python SDK. Consider using a more generic SDK name or documenting the expected local setup. If the team commits.idea/files,misc.xmlis often the most problematic for portability.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In @.idea/misc.xml around lines 3 - 6, The misc.xml configuration is using machine-specific Python SDK names, which makes the project settings hard to share across contributors. Update the Black component and ProjectRootManager entries so they do not depend on local environment-specific SDK labels, and align them with a portable project setup or a documented generic SDK name. Check the IntelliJ project settings entries for Black and ProjectRootManager to ensure the committed configuration is reusable on other machines.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/opentelemetry_mcp/config.py`:
- Around line 83-84: The COMPRESS_RESPONSES parsing in the config loader is too
permissive because it treats any non-"true" value as False, hiding typos and
malformed input. Update the configuration parsing in the relevant config
initialization code to explicitly validate the COMPRESS_RESPONSES environment
value, accept only recognized boolean strings, and raise or surface a clear
error for invalid values instead of defaulting them to False; use the existing
compression config logic around COMPRESS_RESPONSES in config.py to locate the
fix.
In `@src/opentelemetry_mcp/tools/compression.py`:
- Around line 11-24: The compression logic in the list-handling branch of the
compression helper is skipping nested arrays, so the recursive compression
contract is not being met. Update the compression flow in the function that
inspects list payloads so it does not just copy raw item values into rows;
instead, recursively process nested dict/list values before building the
compressed structure, and make sure the fallback path also preserves recursive
handling for non-uniform lists.
In `@src/opentelemetry_mcp/tools/errors.py`:
- Around line 123-125: The success payload shape is being changed in place by
calling compact_json(result), which can alter fields like error_traces and make
the tool response vary between list[object] and {columns, rows}. Update the
logic in the errors tool so compression produces an explicit alternate
representation instead of mutating the existing payload structure, and keep the
original success contract stable for callers while still honoring
config.compress_responses.
In `@tests/test_compression.py`:
- Around line 254-271: The test in test_compress_responses_disabled should
verify the environment parsing path instead of mutating ServerConfig after
creation. Update ServerConfig.from_env usage so the test sets COMPRESS_RESPONSES
in the environment first, asserts that the parsed config.compress_responses is
False, and then checks the passthrough behavior without compression; use the
ServerConfig.from_env and compact_json symbols to keep the test focused on the
env contract.
---
Nitpick comments:
In @.idea/misc.xml:
- Around line 3-6: The misc.xml configuration is using machine-specific Python
SDK names, which makes the project settings hard to share across contributors.
Update the Black component and ProjectRootManager entries so they do not depend
on local environment-specific SDK labels, and align them with a portable project
setup or a documented generic SDK name. Check the IntelliJ project settings
entries for Black and ProjectRootManager to ensure the committed configuration
is reusable on other machines.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d235e02c-27ee-4f56-94d7-08e0481b7b42
📒 Files selected for processing (18)
.env.example.idea/.gitignore.idea/inspectionProfiles/profiles_settings.xml.idea/misc.xml.idea/modules.xml.idea/opentelemetry-mcp-server.iml.idea/vcs.xmlsrc/opentelemetry_mcp/config.pysrc/opentelemetry_mcp/tools/compression.pysrc/opentelemetry_mcp/tools/errors.pysrc/opentelemetry_mcp/tools/expensive_traces.pysrc/opentelemetry_mcp/tools/list_llm_tools.pysrc/opentelemetry_mcp/tools/list_models.pysrc/opentelemetry_mcp/tools/search.pysrc/opentelemetry_mcp/tools/search_spans.pysrc/opentelemetry_mcp/tools/slow_traces.pysrc/opentelemetry_mcp/tools/trace.pytests/test_compression.py
| if config and config.compress_responses: | ||
| result = compact_json(result) | ||
|
|
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Keep the success payload shape stable.
compact_json(result) replaces fields like error_traces in place, so the same tool can return either list[object] or {columns, rows} depending on the payload and savings threshold. The PR context says the server enables this by default, which turns an existing response contract into a data-dependent one for every caller. Make the compact form explicit instead of overwriting the current arrays in place.
🧰 Tools
🪛 ast-grep (0.44.0)
[info] 125-125: use jsonify instead of json.dumps for JSON output
Context: json.dumps(result, indent=2, default=str)
Note: [CWE-116] Improper Encoding or Escaping of Output.
(use-jsonify)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/opentelemetry_mcp/tools/errors.py` around lines 123 - 125, The success
payload shape is being changed in place by calling compact_json(result), which
can alter fields like error_traces and make the tool response vary between
list[object] and {columns, rows}. Update the logic in the errors tool so
compression produces an explicit alternate representation instead of mutating
the existing payload structure, and keep the original success contract stable
for callers while still honoring config.compress_responses.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Summary
Implements tool response compression by converting uniform arrays of objects
into a column/row tabular format, reducing token consumption by an estimated
30-60% for large responses.
Problem
All tool functions return arrays of uniform objects where field names repeat
on every row. For a response with 100 traces, field names like
model,provider, andcountare duplicated 100 times — wasting tokens on everyAI agent call.
Before:
[ {"model": "gpt-4", "provider": "openai", "count": 48}, {"model": "gpt-3.5", "provider": "openai", "count": 12} ]After:
{ "columns": ["model", "provider", "count"], "rows": [["gpt-4", "openai", 48], ["gpt-3.5", "openai", 12]] }Changes
src/opentelemetry_mcp/tools/compression.py— newcompact_json()utilitythat recursively converts uniform arrays into tabular format
src/opentelemetry_mcp/config.py— addedcompress_responses: bool = Trueto
ServerConfig, reads fromCOMPRESS_RESPONSESenv varsrc/opentelemetry_mcp/server.py— passesconfigto all 8 tool callscompact_json()before finaljson.dumps()tests/test_compression.py— 17 new unit tests.env.example— documentedCOMPRESS_RESPONSESoptionConfiguration
Compression is enabled by default. To disable:
Testing
edge cases, nested structures, threshold behavior, and real tool response shapes
ruff format,ruff check,mypy,pytestall greenNotes
on small responses
Summary by CodeRabbit
New Features
Bug Fixes