Skip to content

Stop using writeIndented to reduce token usage#1226

Merged
alzimmermsft merged 4 commits intomicrosoft:mainfrom
alzimmermsft:StopUsingWriteIndented
Nov 21, 2025
Merged

Stop using writeIndented to reduce token usage#1226
alzimmermsft merged 4 commits intomicrosoft:mainfrom
alzimmermsft:StopUsingWriteIndented

Conversation

@alzimmermsft
Copy link
Copy Markdown
Contributor

@alzimmermsft alzimmermsft commented Nov 20, 2025

What does this PR do?

Removes usage of writeIndented = true in 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.

image

Minifying that reduces the token count to 340, or about a 35% reduction in token usage.

image

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md and/or servers/Fabric.Mcp.Server/CHANGELOG.md for product changes (features, bug fixes, UI/UX, updated dependencies)
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes using script at eng/scripts/Process-PackageReadMe.ps1. See Package README
    • Updated command list in /servers/Azure.Mcp.Server/docs/azmcp-commands.md and/or /docs/fabric-commands.md
    • Run .\eng\scripts\Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated test prompts in /servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 = true from 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

Copy link
Copy Markdown
Member

@joshfree joshfree left a comment

Choose a reason for hiding this comment

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

  1. add a changelog entry
  2. add a test to ensure we don't regress this by accidently re-enabling prettyPrint

@joshfree joshfree moved this from Untriaged to In Progress in Azure MCP Server Nov 20, 2025
@joshfree joshfree added this to the 2025-12 milestone Nov 20, 2025
@joshfree joshfree added server-Azure.Mcp Azure.Mcp.Server tools-Core AZMCP Core functionality that all other tools build on top of labels Nov 20, 2025
@alzimmermsft alzimmermsft enabled auto-merge (squash) November 21, 2025 16:48
@alzimmermsft alzimmermsft merged commit 39478b9 into microsoft:main Nov 21, 2025
37 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Azure MCP Server Nov 21, 2025
@alzimmermsft alzimmermsft deleted the StopUsingWriteIndented branch November 21, 2025 17:30
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server-Azure.Mcp Azure.Mcp.Server tools-Core AZMCP Core functionality that all other tools build on top of

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants