Skip to content

Drop indent=2 from JSON responses to reduce token usage#34

Merged
oleksandr-nc merged 1 commit into
mainfrom
feature/compact-json-responses
Mar 30, 2026
Merged

Drop indent=2 from JSON responses to reduce token usage#34
oleksandr-nc merged 1 commit into
mainfrom
feature/compact-json-responses

Conversation

@bigcat88
Copy link
Copy Markdown
Contributor

@bigcat88 bigcat88 commented Mar 30, 2026

Summary

  • Remove indent=2 from all 50 json.dumps calls across 16 tool modules
  • LLMs parse compact JSON as well as indented JSON — the whitespace is pure overhead

Benchmark results (against live Nextcloud with realistic data)

Tool Before After Saved
list_conversations 8,168 chars 6,890 chars -15.6%
get_activity 10,011 chars 7,955 chars -20.5%
list_notifications 6,002 chars 5,300 chars -11.7%
list_calendars 389 chars 303 chars -22.1%
list_directory 1,744 chars 1,488 chars -14.7%
Total 27,906 chars 23,394 chars -16.2%

Savings scale with data volume — tools returning nested objects (activity, conversations) see 15-22% reduction.

Test plan

  • 520/520 non-mail integration tests pass locally (mail failures are pre-existing)
  • All unit tests pass
  • Lint + pyright clean
  • CI

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Optimized JSON response formatting across multiple tools (activity, announcements, calendar, mail, notifications, shares, talk, and more) by removing pretty-printing. Responses now use compact JSON serialization for improved efficiency while maintaining the same data structures and functionality.

LLMs parse compact JSON just as well as indented JSON. Removing
indent=2 from all 50 json.dumps calls across 16 tool modules reduces
response sizes by 11-22% depending on nesting depth.

Benchmarked against live Nextcloud with realistic data:
- list_conversations: 8,168 -> 6,890 chars (-15.6%)
- get_activity: 10,011 -> 7,955 chars (-20.5%)
- list_notifications: 6,002 -> 5,300 chars (-11.7%)
- Overall: 27,906 -> 23,394 chars (-16.2%, ~1,289 tokens saved)
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 08f74d60-303b-4fab-9f98-44500d7864eb

📥 Commits

Reviewing files that changed from the base of the PR and between 59e38a3 and aa1f5ef.

📒 Files selected for processing (16)
  • src/nc_mcp_server/tools/activity.py
  • src/nc_mcp_server/tools/announcements.py
  • src/nc_mcp_server/tools/app_management.py
  • src/nc_mcp_server/tools/calendar.py
  • src/nc_mcp_server/tools/collectives.py
  • src/nc_mcp_server/tools/comments.py
  • src/nc_mcp_server/tools/files.py
  • src/nc_mcp_server/tools/mail.py
  • src/nc_mcp_server/tools/notifications.py
  • src/nc_mcp_server/tools/shares.py
  • src/nc_mcp_server/tools/system_tags.py
  • src/nc_mcp_server/tools/talk.py
  • src/nc_mcp_server/tools/trashbin.py
  • src/nc_mcp_server/tools/user_status.py
  • src/nc_mcp_server/tools/users.py
  • src/nc_mcp_server/tools/versions.py

📝 Walkthrough

Walkthrough

This pull request removes pretty-printing from JSON responses across 16 tool modules in the MCP server. All changes replace json.dumps(..., indent=2, default=str) with json.dumps(..., default=str), converting output from multi-line indented to compact single-line JSON format. No functional logic, control flow, or data structures are altered.

Changes

Cohort / File(s) Summary
JSON Serialization Formatting
src/nc_mcp_server/tools/activity.py, announcements.py, app_management.py, calendar.py, collectives.py, comments.py, files.py, mail.py, notifications.py, shares.py, system_tags.py, talk.py, trashbin.py, user_status.py, users.py, versions.py
Removed indent=2 parameter from json.dumps() calls across 16 tool modules. JSON responses now return compact single-line format instead of pretty-printed multi-line format. All tools affected include read/write operations (e.g., list_*, get_*, create_*, update_*); no logic, control flow, data structures, or error handling modified.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

Indents are gone, the JSON flows so tight,
Compact and sleek, no extra white,
Sixteen files, one pattern—clean and bright,
The formatting hops to a leaner flight! 🐰✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and directly summarizes the main change: removing indent=2 from JSON responses to reduce token usage, which matches the comprehensive refactoring across 16 tool modules.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/compact-json-responses

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.88%. Comparing base (59e38a3) to head (aa1f5ef).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #34   +/-   ##
=======================================
  Coverage   95.88%   95.88%           
=======================================
  Files          24       24           
  Lines        1848     1848           
=======================================
  Hits         1772     1772           
  Misses         76       76           
Flag Coverage Δ
integration 94.53% <100.00%> (ø)
nc32 94.53% <100.00%> (ø)
nc33 94.53% <100.00%> (ø)
py3.12 9.90% <0.00%> (ø)
py3.13 9.90% <0.00%> (ø)
py3.14 9.90% <0.00%> (ø)
session-cache 26.19% <0.00%> (ø)
unit 9.90% <0.00%> (ø)
user-permissions 48.16% <14.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oleksandr-nc oleksandr-nc marked this pull request as ready for review March 30, 2026 12:34
@oleksandr-nc oleksandr-nc merged commit 957b18a into main Mar 30, 2026
12 checks passed
@oleksandr-nc oleksandr-nc deleted the feature/compact-json-responses branch March 30, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants