Skip to content

feat: Compress MCP tool output via PostToolUse hook#535

Open
jmeiracorbal wants to merge 1 commit intortk-ai:developfrom
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:developfrom
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
All committers have signed the CLA.

@jmeiracorbal jmeiracorbal force-pushed the feat/post-tool-use-mcp-filter branch 2 times, most recently from e9e07ed to d7000c6 Compare March 23, 2026 10:41
@pszymkowiak
Copy link
Collaborator

Hi! Two things needed before we can review:

  1. Retarget to develop — this PR targets master, but all PRs should target develop. You can change the base branch in the PR settings (right sidebar).
  2. Sign the CLA — if not already done, please sign at https://cla-assistant.io/rtk-ai/rtk

Thanks!

@jmeiracorbal jmeiracorbal force-pushed the feat/post-tool-use-mcp-filter branch from d7000c6 to fff2317 Compare March 26, 2026 10:56
@jmeiracorbal jmeiracorbal changed the base branch from master to develop March 26, 2026 11:03
@aeppling
Copy link
Contributor

Hey

We are cleaning up the codebase and improving the project structure for better onboarding. As part of this effort, PR #826 reorganizes src/ from a flat layout into subfolders.

No logic changes — only file moves and import path updates.

What you need to do

Rebase your branch on develop when receiving this comment:

git fetch origin && git rebase origin/develop

Git detects renames automatically. If you get import conflicts, update the paths:

use crate::git;        // now: use crate::cmds::git::git;
use crate::tracking;   // now: use crate::core::tracking;
use crate::config;     // now: use crate::core::config;
use crate::init;       // now: use crate::hooks::init;
use crate::gain;       // now: use crate::analytics::gain;

Need help rebasing? Tag @aeppling

@jmeiracorbal jmeiracorbal force-pushed the feat/post-tool-use-mcp-filter branch from fff2317 to 69ec704 Compare March 26, 2026 19:54
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.

4 participants