Skip to content

feat: add content flattener for improved token management#144

Open
vijaygovindaraja wants to merge 1 commit intouscensusbureau:mainfrom
vijaygovindaraja:feat/70-content-flattener-token-management
Open

feat: add content flattener for improved token management#144
vijaygovindaraja wants to merge 1 commit intouscensusbureau:mainfrom
vijaygovindaraja:feat/70-content-flattener-token-management

Conversation

@vijaygovindaraja
Copy link
Copy Markdown

Summary

Adds a content-flattener utility 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.ts with two exports:

  • flattenJson(data, options?) — compact JSON serialization with optional field stripping and array truncation
  • flattenResponse(prefix, data, options?) — builds a complete tool response string with count summary

Features:

  • Minifies JSON output (no whitespace)
  • Strips fields not useful for LLM reasoning (created_at, updated_at, parent_geography_level_id)
  • Filters null values from response objects
  • Supports maxItems for truncating large result sets with a count summary

Also incorporates the quick fix suggested by @annav-gov in the issue comments.

Files changed

File Change
src/helpers/content-flattener.ts New utility (104 lines)
src/tools/search-data-tables.tool.ts Use flattenResponse() instead of JSON.stringify(data, null, 2)
src/tools/fetch-dataset-geography.tool.ts Same
src/tools/resolve-geography-fips.tool.ts Same

Fixes #70

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>
@vijaygovindaraja
Copy link
Copy Markdown
Author

@luke-keller-census this one adds a content flattener for token management. Pairs with #145.

@vijaygovindaraja
Copy link
Copy Markdown
Author

@luke-keller-census @curt-mitch-gov same for this one — independent from #145.

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.

Create a function to flatten content for improved token management

1 participant