Fix: Use JSON serialization instead of Debug format in tool returns - #63
Merged
Conversation
…ol returns Fixes #62 Changes: - Modified utils.rs generate_error_handling() to use serde_json::to_string() instead of format!("{:?}") for tool return values - Now populates structured_content field per MCP 2025-06-18 specification - Graceful fallback to Debug format for types without Serialize trait - Added comprehensive test suite (8 tests) in json_serialization_test.rs Breaking Change: - Tool return types should implement Serialize trait for optimal JSON output - Types without Serialize will fallback to Debug format Before: "SearchResult { items: [...] }" After: {"items": [...]} Version bumped to 0.13.0 due to breaking change requirement.
Code Coverage Report 📊Local Coverage: 19.90%
Coverage Details📋 Full Report: View on Codecov |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
PR Validation ResultsQuick Validation: ✅
Summary: ✅ All checks passed |
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.
Fixes #62
Problem
The
#[mcp_tools]macro was using Rust's Debug trait ({:?}) instead of JSON serialization when formatting tool results, causing tool responses to contain Rust debug format instead of proper JSON.Before:
{ "content": [{ "type": "text", "text": "SearchResult { items: [SearchItem { id: 1, title: \"Bug\" }] }" }] }After:
{ "content": [{ "type": "text", "text": "{\"items\":[{\"id\":1,\"title\":\"Bug\"}]}" }], "structured_content": {"items": [{"id": 1, "title": "Bug"}]} }Changes
Core Fix (mcp-macros/src/utils.rs)
generate_error_handling()to useserde_json::to_string()instead offormat!("{:?}")structured_contentfield per MCP 2025-06-18 specificationSerializeTesting (mcp-macros/tests/json_serialization_test.rs)
Added comprehensive test suite with 8 tests covering:
structured_contentis populatedVersion & Documentation
Breaking Change
Tool return types should implement
Serializetrait for optimal JSON output:Types without
Serializewill gracefully fall back to Debug format.Test Results
Compliance