feat: preserve JSON structure in MCP tool results#26
Merged
Conversation
- Attempt to parse tool text content as JSON before wrapping - If content is valid JSON, return it directly to preserve structure - Only fall back to simplified format for non-JSON content - Complements PR #25's smart truncation by ensuring structured data flows cleanly This allows tools to return complex structured data like: {"files": [...], "stats": {...}, "metadata": {...}} Instead of flattening everything to: {"success": true, "content": "...stringified JSON..."} The combination of this change with PR #25's smart truncation ensures that structured tool outputs are preserved and intelligently truncated throughout the entire decision tracking pipeline.
Log a debug message when tool results fall back to simplified format, showing a preview of the content. This helps developers understand: - When tools are returning plain text vs structured data - Why certain results aren't being preserved as JSON - What the actual content looks like (first 100 chars)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR enhances MCP tool result handling to preserve JSON structure when tools return structured data, complementing PR #25's smart truncation feature.
Problem
Previously, all tool results were flattened into a simplified format:
{ "success": true, "content": "{\"files\": [...], \"stats\": {...}}" // JSON as string }This lost the structure of complex tool outputs, making them harder to process and analyze.
Solution
The MCP client now:
Example
Before:
{ "success": true, "content": "{\"files\": [\"test.py\", \"main.py\"], \"lines\": 500}" }After:
{ "files": ["test.py", "main.py"], "lines": 500 }Benefits
Testing
Related