Skip to content

[Server] Use compact JSON in result formatters - #647

Merged
chr-hertel merged 4 commits into
modelcontextprotocol:mainfrom
steffans:compact-formatter-json
Nov 2, 2026
Merged

chr-hertel merged 4 commits into
modelcontextprotocol:mainfrom
steffans:compact-formatter-json

Conversation

@steffans

@steffans steffans commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Use compact JSON instead of pretty-printed JSON from tool, prompt, and resource result formatters, reducing whitespace in content that may be sent to models. Optionally the TextContent class could also be changed to not use pretty-printed JSON.

Motivation and Context

Compact JSON preserves the same data while reducing its size.

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The JSON fallback lacks a regression test asserting exact compact output for untyped array or object content, so restoring pretty-printed JSON would go undetected.

1 open issue
What changed in this PR

This PR makes the server’s tool, prompt, and resource result formatters emit compact JSON, reducing whitespace in content sent to models while preserving the encoded data.

Changes:

  • Remove JSON_PRETTY_PRINT from the three result formatters, retaining other encoding options.
  • Update HTTP and stdio interoperability snapshots to expect compact JSON.
  • Update client-example snapshots to match the new output formatting.
File Description
tests/​Interop/​Server/​Stdio/​snapshots/​StdioExplicitRegistrationTest-resources_read-read_item_ABC_details.json Expect compact item ABC details.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioExplicitRegistrationTest-resources_read-read_item_123_details.json Expect compact item 123 details.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioEnvVariablesTest-tools_call-process_data_production.json Expect compact production-mode output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioEnvVariablesTest-tools_call-process_data_default.json Expect compact default-mode output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioEnvVariablesTest-tools_call-process_data_debug.json Expect compact debug-mode output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioDiscoveryCalculatorTest-tools_call-update_setting.json Expect compact setting-update output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioDiscoveryCalculatorTest-resources_read-read_config.json Expect compact calculator configuration.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioCustomDependenciesTest-tools_call-complete_task.json Expect compact task-completion output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioCustomDependenciesTest-tools_call-add_task.json Expect compact task-creation output.
tests/​Interop/​Server/​Stdio/​snapshots/​StdioCustomDependenciesTest-resources_read-read_system_stats.json Expect compact system statistics.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-validate_profile.json Expect compact profile-validation output.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-schedule_event.json Expect compact event-scheduling output.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-manage_list.json Expect compact list-processing output.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-generate_config.json Expect compact generated configuration.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-format_text.json Expect compact text-formatting output.
tests/​Interop/​Server/​Http/​snapshots/​HttpSchemaShowcaseTest-tools_call-calculate_range.json Expect compact calculation output.
tests/​Interop/​Server/​Http/​snapshots/​HttpDiscoveryUserProfileTest-tools_call-test_tool_without_params.json Expect compact parameterless-tool output.
tests/​Interop/​Server/​Http/​snapshots/​HttpDiscoveryUserProfileTest-tools_call-send_welcome.json Expect compact welcome-message output.
tests/​Interop/​Server/​Http/​snapshots/​HttpDiscoveryUserProfileTest-resources_read-read_user_profile_102.json Expect compact user 102 profile.
tests/​Interop/​Server/​Http/​snapshots/​HttpDiscoveryUserProfileTest-resources_read-read_user_profile_101.json Expect compact user 101 profile.
tests/​Interop/​Server/​Http/​snapshots/​HttpDiscoveryUserProfileTest-resources_read-read_user_id_list.json Expect compact user ID array.
tests/​Interop/​Server/​Http/​snapshots/​HttpComplexToolSchemaTest-tools_call-schedule_event_meeting_with_time.json Expect compact timed-meeting output.
tests/​Interop/​Server/​Http/​snapshots/​HttpComplexToolSchemaTest-tools_call-schedule_event_low_priority.json Expect compact low-priority event output.
tests/​Interop/​Server/​Http/​snapshots/​HttpComplexToolSchemaTest-tools_call-schedule_event_high_priority.json Expect compact high-priority event output.
tests/​Interop/​Server/​Http/​snapshots/​HttpComplexToolSchemaTest-tools_call-schedule_event_all_day_reminder.json Expect compact all-day reminder output.
tests/​Integration/​ClientExamples/​Stdio/​snapshots/​StdioRootsTest.txt Expect compact workspace-roots output.
tests/​Integration/​ClientExamples/​Stdio/​snapshots/​StdioElicitationTest-defaults.txt Expect compact default elicitation results.
tests/​Integration/​ClientExamples/​Stdio/​snapshots/​StdioElicitationTest-answered.txt Expect compact answered elicitation results.
tests/​Integration/​ClientExamples/​Stdio/​snapshots/​StdioDiscoveryCalculatorTest.txt Expect compact displayed calculator configuration.
tests/​Integration/​ClientExamples/​Stdio/​snapshots/​StdioClientCommunicationTest.txt Expect compact stdio communication results.
tests/​Integration/​ClientExamples/​Http/​snapshots/​HttpClientCommunicationTest.txt Expect compact HTTP communication results.
src/​Capability/​Formatter/​ToolResultFormatter.php Remove pretty-printing from tool JSON output.
src/​Capability/​Formatter/​ResourceResultFormatter.php Remove pretty-printing from both resource JSON branches.
src/​Capability/​Formatter/​PromptResultFormatter.php Remove pretty-printing from the prompt JSON fallback.

Review effort: Balanced

chr-hertel
chr-hertel previously approved these changes Nov 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new classes in tests/Support/PrettyJson.php and tests/Unit/Schema/Content/TextContentTest.php are missing the class-level @author tags required by AGENTS.md.

1 open issue
1 resolved since last review

Review effort: Balanced

/**
* Keeps JSON readable in snapshots; formatter tests pin the compact wire format.
*/
final class PrettyJson
@chr-hertel
chr-hertel merged commit 210268e into modelcontextprotocol:main Nov 2, 2026
54 of 56 checks passed

@chr-hertel chr-hertel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @steffans - kept the pretty print for snapshots in tests for readability

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.

3 participants