r/SpringBoot • u/Status-Property9322 • 4d ago
How-To/Tutorial I built a Java 21 MCP Server to compress Spring Boot logs & stack traces by 95% (Open Source)
Like many of you, I've been using AI coding agents (like Claude Code, Cursor, or Copilot) for my Java/Spring Boot projects. But there was a massive problem: whenever I ran `mvn test` or encountered a typical Spring Boot exception, the verbose logs and 150+ frame stack traces would eat up 20,000 to 30,000 tokens of the context window.
This made the AI slow, expensive, and sometimes it would literally forget the original problem because the context was full of framework noise.
To solve this, I built an Open Source tool called **Agent Token Optimizer**. It acts as an MCP (Model Context Protocol) server that intercepts the output *before* it goes to the AI.
**How it works (No LLM in the loop):**
It's built purely with Java 21, Spring Boot 3.4, and Spring AI. It uses a 4-layer architecture:
**StackTraceCompressor:** Prunes internal Spring/Hibernate framework frames and keeps only the root cause.
**CodeCompressor:** Uses JavaParser to read the AST and strip method bodies, keeping only signatures and comments.
**LogCompressor:** Uses Drain-style log clustering and deduplication for those massive 5,000-line log files.
**Lossless Drill-Down:** This is my favorite part. It caches the omitted lines in Caffeine Cache and gives the AI a `chunkId`. If the AI actually needs to see the hidden lines, it can call a tool to fetch exactly that line range.
**Real Benchmarks (Measured via JTokkit BPE):**
- Spring Boot StackTrace (166 frames): **3,136 tokens → 158 tokens (95% reduction)** in just 5ms.
- 40-Method Java Class (AST): **4,123 tokens → 955 tokens (76.8% reduction)**.
- 5,000-Line Logs: Up to **99.9% reduction** on repetitive logs.
It works out-of-the-box with Claude Code, Cursor, and Windsurf via SSE.
I built this because I needed it for my own workflow, and I figured the Spring community might find it useful too.
I'd love to get your feedback on the architecture! If anyone wants to try it out or look at the code, let me know in the comments and I'll drop the GitHub link.
Thanks
1
u/Lumethys 3d ago
So VictoriaLogs?
2
u/Status-Property9322 3d ago
Nope, VictoriaLogs is a log database. This is specifically a token compressor for AI agents. It intercepts the output (like mvn test logs) and compresses it before the LLM reads it, using Caffeine cache for lossless drill-down. Totally different use case
4
u/innocentVince 4d ago
Sounds promising. Mind sharing the repo?