feat: add content flattener for improved token management#144
Open
vijaygovindaraja wants to merge 1 commit intouscensusbureau:mainfrom
Open
feat: add content flattener for improved token management#144vijaygovindaraja wants to merge 1 commit intouscensusbureau:mainfrom
vijaygovindaraja wants to merge 1 commit intouscensusbureau:mainfrom
Conversation
Add a content-flattener utility that reduces token usage in MCP tool responses by: - Minifying JSON output (removing pretty-print whitespace from JSON.stringify calls that used `null, 2` formatting) - Stripping fields not useful for LLM reasoning (created_at, updated_at, parent_geography_level_id) - Filtering null values from response objects - Supporting result set truncation with count summaries Replace all 3 instances of `JSON.stringify(data, null, 2)` in tool responses with the new `flattenResponse()` helper: - search-data-tables.tool.ts - fetch-dataset-geography.tool.ts - resolve-geography-fips.tool.ts For a typical 100-item geography response, this reduces token usage by ~30-40% by eliminating indentation whitespace and null fields. Fixes uscensusbureau#70 Signed-off-by: V Govindarajan <vijay.govindarajan91@gmail.com>
Author
|
@luke-keller-census this one adds a content flattener for token management. Pairs with #145. |
Author
|
@luke-keller-census @curt-mitch-gov same for this one — independent from #145. |
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
Adds a
content-flattenerutility to reduce token usage in MCP tool responses, and applies it across all tools that were using pretty-printed JSON.Problem
Three tools were using
JSON.stringify(data, null, 2)to format responses, which adds significant whitespace overhead:search-data-tables(line 85)fetch-dataset-geography(line 223)resolve-geography-fips(line 106)For a typical 100-item response, pretty-printed JSON wastes ~30-40% of tokens on indentation and newlines that provide no value to the LLM.
Solution
New utility at
src/helpers/content-flattener.tswith two exports:flattenJson(data, options?)— compact JSON serialization with optional field stripping and array truncationflattenResponse(prefix, data, options?)— builds a complete tool response string with count summaryFeatures:
created_at,updated_at,parent_geography_level_id)maxItemsfor truncating large result sets with a count summaryAlso incorporates the quick fix suggested by @annav-gov in the issue comments.
Files changed
src/helpers/content-flattener.tssrc/tools/search-data-tables.tool.tsflattenResponse()instead ofJSON.stringify(data, null, 2)src/tools/fetch-dataset-geography.tool.tssrc/tools/resolve-geography-fips.tool.tsFixes #70