Stop using writeIndented to reduce token usage#1226
Merged
alzimmermsft merged 4 commits intomicrosoft:mainfrom Nov 21, 2025
Merged
Stop using writeIndented to reduce token usage#1226alzimmermsft merged 4 commits intomicrosoft:mainfrom
alzimmermsft merged 4 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR removes WriteIndented = true from JSON serialization contexts across the codebase to reduce token usage when responses are consumed by LLMs. Pretty-printed JSON offers minimal benefit to LLMs while consuming approximately 35% more tokens according to the PR description.
- Removed
WriteIndented = truefrom all JsonSourceGenerationOptions attributes in tool serialization contexts - Cleaned up unused using statements in affected files
- Updated unit tests to expect minified JSON output (no whitespace after colons)
Reviewed Changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tools/Azure.Mcp.Tools.Sql/src/Commands/SqlJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.Speech/src/Commands/SpeechJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.ResourceHealth/src/ResourceHealthJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.Redis/src/Models/Resource.cs | Removed WriteIndented option and cleaned up unused using statements |
| tools/Azure.Mcp.Tools.Monitor/src/Commands/MonitorJsonContext.cs | Simplified JsonElement type references |
| tools/Azure.Mcp.Tools.ManagedLustre/src/Commands/ManagedLustreJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.KeyVault/src/Commands/KeyVaultJsonContext.cs | Removed unused using statement |
| tools/Azure.Mcp.Tools.EventHubs/src/Commands/EventHubsJsonContext.cs | Removed WriteIndented option and unused using statements |
| tools/Azure.Mcp.Tools.EventGrid/src/Commands/EventGridJsonContext.cs | Removed WriteIndented option and unused using statement |
| tools/Azure.Mcp.Tools.CloudArchitect/src/CloudArchitectJsonContext.cs | Removed WriteIndented and reordered attributes |
| tools/Azure.Mcp.Tools.Authorization/src/Commands/AuthorizationJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.AppService/src/Commands/AppServiceJsonContext.cs | Removed unused using statements |
| tools/Azure.Mcp.Tools.AppConfig/src/Commands/AppConfigJsonContext.cs | Removed WriteIndented option from JSON context |
| tools/Azure.Mcp.Tools.Acr/src/Commands/AcrJsonContext.cs | Removed WriteIndented option from JSON context |
| servers/Azure.Mcp.Server/docs/new-command.md | Updated documentation example to remove WriteIndented |
| core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Server/Models/RegistryRootTests.cs | Updated tests to use ServerJsonContext and expect minified JSON |
| core/Azure.Mcp.Core/src/Areas/Server/ServerJsonContext.cs | Removed WriteIndented option from JSON context |
| AGENTS.md | Updated documentation example to remove WriteIndented |
joshfree
reviewed
Nov 20, 2025
Member
joshfree
left a comment
There was a problem hiding this comment.
- add a changelog entry
- add a test to ensure we don't regress this by accidently re-enabling prettyPrint
vcolin7
approved these changes
Nov 20, 2025
vcolin7
approved these changes
Nov 21, 2025
colbytimm
pushed a commit
to colbytimm/microsoft-mcp
that referenced
this pull request
Dec 8, 2025
* Stop using writeIndented to reduce token usage * Add CHANGELOG entry * Move CHANGELOG
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.
What does this PR do?
Removes usage of
writeIndented = truein many locations as the response content is generally handled by the LLM and pretty printing doesn't add much benefit (if any at all) for LLM understanding of the response given JSON has built-in structuring and LLMs are trained on vast sets of minified JSON data.What removing pretty printing does?
For example, using this pretty printed JSON from this test: https://github.com/microsoft/mcp/blob/main/tools/Azure.Mcp.Tools.BicepSchema/tests/Azure.Mcp.Tools.BicepSchema.UnitTests/EntitySerializationTests.cs#L16
Using https://llm-calculator.com (uncertain is this is a quality site), the pretty printed JSON from that test uses 523 tokens using GPT-4o.
Minifying that reduces the token count to 340, or about a 35% reduction in token usage.
GitHub issue number?
[Link to the GitHub issue this PR addresses]Pre-merge Checklist
servers/Azure.Mcp.Server/CHANGELOG.mdand/orservers/Fabric.Mcp.Server/CHANGELOG.mdfor product changes (features, bug fixes, UI/UX, updated dependencies)servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationeng/scripts/Process-PackageReadMe.ps1. See Package README/servers/Azure.Mcp.Server/docs/azmcp-commands.mdand/or/docs/fabric-commands.md.\eng\scripts\Update-AzCommandsMetadata.ps1to update tool metadata in azmcp-commands.md (required for CI)ToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.json/servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline