Skip to content

feat: Compress MCP tool output via PostToolUse hook#535

Open
jmeiracorbal wants to merge 1 commit intortk-ai:masterfrom
jmeiracorbal:feat/post-tool-use-mcp-filter
Open

feat: Compress MCP tool output via PostToolUse hook#535
jmeiracorbal wants to merge 1 commit intortk-ai:masterfrom
jmeiracorbal:feat/post-tool-use-mcp-filter

Conversation

@jmeiracorbal
Copy link

About

Adds a PostToolUse hook that compresses MCP tool responses before they reach Claude's context. Unlike Bash commands (intercepted via PreToolUse), MCP tool outputs were previously untouched by rtk.

Claude Code supports updatedMCPToolOutput in PostToolUse hooks which allows replacing a tool's response before the agent sees it. This is the only deterministic interception point available for MCP tools (it doesn't depend on agent cooperation like Read actions).

How it works

  1. rtk init --global installs rtk-post-tool-use.sh alongside the existing rtk-rewrite.sh and registers it under PostToolUse in ~/.claude/settings.json with matcher mcp__.*.
  2. On every MCP tool call, the hook pipes the PostToolUse JSON to rtk filter-mcp-output.
  3. The subcommand deduplicates repeated lines and truncates responses over 3000 chars, returning updatedMCPToolOutput only when there is actual savings — pass-through when the response is already short.
  4. Errors are logged to ~/.local/share/rtk/logs/mcp-filter.log and the hook always exits 0 so it never blocks Claude.

Changes

  • src/mcp_filter.rs: Compression logic (deduplicate + truncate).
  • hooks/rtk-post-tool-use.sh: The same behavior that ~/.claude/hooks/rtk-rewrite.sh.
  • src/main.rs: filter-mcp-output subcommand.
  • src/init.rs: Can install/uninstall PostToolUse hook alongside existing PreToolUse hook.

Test plan

  • cargo test: 615 passed, 0 failed.
  • Manually verified hook fires for mcp__.* tools in Claude Code.
  • Verified compression triggers on responses > 3000 chars.
  • Verified no output (pass-through) when response is under threshold.
  • rtk init -g --auto-patch idempotent: Can detects hook already present.
  • Errors logged to file, hook never blocks Claude on failure.

@CLAassistant
Copy link

CLAassistant commented Mar 20, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jmeiracorbal jmeiracorbal force-pushed the feat/post-tool-use-mcp-filter branch from 9e0d778 to abe7ff2 Compare March 23, 2026 09:58
@jmeiracorbal jmeiracorbal force-pushed the feat/post-tool-use-mcp-filter branch from e9e07ed to d7000c6 Compare March 23, 2026 10:41
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.

2 participants