See llms.txt for all machine-readable content.

Back to Templates

Compress MCP tool outputs into token-efficient LLM context via webhook

Quick overview

This workflow exposes a POST webhook that accepts large tool/MCP outputs, runs parallel compression strategies, enforces a target token budget, and returns an optimized context package with before/after metrics and an audit trail, using HTTP Request calls plus custom JavaScript processing.

How it works

  1. Receives a POST request on the /mcp-context-optimize webhook containing context/tool results plus optional token budget, strategy, and field include/exclude hints.
  2. Validates the payload, estimates baseline size (chars/tokens), creates an optimization session, and signs a short-lived session token.
  3. Builds three candidate artifacts (prune/deduplicate, summarize long text, and structure-preserving key extraction) and sends each candidate to an external HTTP endpoint in parallel.
  4. Waits for the candidate responses and also runs a hard timeout path to ensure the workflow continues if responses are slow.
  5. Scores the candidates, selects the best option that fits the token budget (or trims as a fallback), and records the decision in the session audit.
  6. Assembles the final optimized context package with savings metrics and returns it as the webhook response.

Setup

  1. Set a strong signingSecret value in the first code step to securely sign session tokens.
  2. Review and adjust limits such as defaultTokenBudget, maxInputChars, and charsPerTokenEstimate to match your LLM and expected payload sizes.
  3. Replace the https://httpbin.org/post URLs in the three HTTP Request steps with your own compression/summarization endpoints (or keep them as-is for echo/testing).
  4. Activate the workflow and copy the production webhook URL for /mcp-context-optimize into the system that will send tool/MCP outputs.