Skip to content

feat(wordpress): strip yoast_head fields from REST responses#16

Open
kleintech wants to merge 1 commit into
InstaWP:mainfrom
kleintech:feature/response-trimming
Open

feat(wordpress): strip yoast_head fields from REST responses#16
kleintech wants to merge 1 commit into
InstaWP:mainfrom
kleintech:feature/response-trimming

Conversation

@kleintech
Copy link
Copy Markdown

Problem

Every WordPress REST API response that includes Yoast SEO data carries a yoast_head (escaped HTML) and a yoast_head_json field. Together these add roughly 10KB of pre-rendered schema markup to every single post / page response. When the MCP client is an LLM, that markup is paid for in tokens on every read and every write, but the LLM almost never needs it — it's SEO output meant for HTTP clients rendering pages, not for AI tools that are creating or editing content.

Solution

Strip configured top-level fields from WP REST responses at the single chokepoint where every tool receives them — makeWordPressRequest() in src/wordpress.ts.

  • Pure helper trimResponseFields(data, fields) handles both single objects and arrays of objects. Only top-level fields are stripped; nested objects are left intact.
  • resolveStripFields(envValue) reads MCP_WP_STRIP_FIELDS (comma-separated) and falls back to the default ["yoast_head", "yoast_head_json"]. Setting it to an empty string disables trimming.
  • The existing rawResponse: true escape hatch on makeWordPressRequest bypasses trimming, so any caller that needs the raw axios response is unaffected.
  • Trimming is outbound only — requests to WP are unchanged.

What's tested

No automated tests in this PR — there's no test framework set up in the repo yet. The trim function was designed as a pure, exported helper so it can be covered cleanly in a follow-up PR that adds vitest. Behavior was sanity-checked against the six cases from the spec (single object, array of objects, nested objects untouched, empty input, missing fields, env-var override).

Out of scope

  • Adding a test framework — planned as a separate PR to keep this change small and reviewable.
  • A verbose / raw-mode flag to selectively re-include trimmed fields per call. Easy to add later if there's demand.
  • Two pre-existing TypeScript errors in src/server.ts (TS2589) and src/tools/media.ts (TS2345). Both exist on main already and are unrelated to this change.

🤖 Generated with Claude Code

Adds a pure trimResponseFields() helper applied inside makeWordPressRequest
right before the response is returned to the MCP client. By default it strips
the top-level yoast_head and yoast_head_json fields, which Yoast SEO adds to
every post/page response and which contribute ~10KB of pre-rendered schema
markup the LLM never needs.

The strip list is overridable via the MCP_WP_STRIP_FIELDS environment variable
(comma-separated). Set it to an empty string to disable trimming.

Only top-level fields are stripped; nested objects are left intact. Both
single-object responses and arrays of objects are handled. The escape hatch
on makeWordPressRequest (rawResponse: true) bypasses trimming.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

1 participant