Skip to content

fix(claude): tool-savings PostToolUse matcher never matches MCP tools - #850

Draft
hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/834-tool-savings-mcp-matcher
Draft

hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/834-tool-savings-mcp-matcher

Conversation

@hr1juldey

Copy link
Copy Markdown

Fixes #834 (part 1).

Problem

Sessions driven through MCP tools never move graftReads/savedTokens: the tool-savings PostToolUse matcher written to settings.json is a plain-character value,

Bash|mcp__graft__|mcp__trail__|Read|Grep|Glob|Skill

and Claude Code compares such a matcher as a list of exact tool names (hooks docs, "Match MCP tools"). mcp__trail__ can only match a tool literally named that — never a real mcp__<server>__trail_find_code — so the hook never fires for MCP calls under any server key.

Fix

The handler side already classifies MCP names across every host prefix (isGraftMcpTool strips mcp__<server>__ / MCP: and checks GRAFT_MCP_TOOL_NAMES), so only the matcher string moves, in src/claude/settings-merge.ts:

^(Bash|Read|Grep|Glob|Skill)$|^mcp__.+__(graft|trail)_
  • Anchors keep the native names exact once the value is a regex (Bash no longer leaks onto BashOutput).
  • .+ matches any server key, including plugin-scoped servers (mcp__plugin_<name>_<server>__…).
  • The handler still filters by the real tool-name list, so third-party MCP tools classify as neither and cost only a stdin read.
  • Re-wiring stays idempotent: isGraftEntry marks by the shim command, not the matcher, so a 0.23-era install is rewritten in place on the next init (covered by the upgrade test).

Tests

  • New test asserts the emitted matcher is regex and fires for mcp__trail__trail_find_code, mcp__graft__graft_find_code, a hyphenated server key (mcp__trail-map__…), and a plugin-scoped name, while BashOutput / bare mcp__trail__ / unrelated MCP tools stay quiet.
  • The expected matcher in the init-upgrade test moves to the new string, proving an old install re-wires on upgrade.
  • Full suite before/after: 4548 → 4549 tests, the only delta is the new test; the 6 failures present on clean main (49d36b7) are unchanged and pre-date this branch.

Note for the changelog

Once this matcher ships, users who worked around the bug with a repo-level mcp__<server>__.* PostToolUse entry should drop it, or each MCP call will count twice.

Validated against the built dist (tsc clean) and the full node scripts/run-tests.mjs suite on Node 26; the reproduction follows the report in #834 on a repo registered under a non-trail server key.

Claude Code reads a PostToolUse matcher made of plain characters as a
list of exact tool names, so `Bash|mcp__graft__|mcp__trail__|…` never
fired for a real `mcp__<server>__trail_find_code` (or graft) call, and
graftReads/savedTokens stayed flat after MCP-only sessions. The handler
already classifies MCP names across every host prefix, so only the
matcher moved: anchored regex — exact for the native tools, MCP matched
by tool-name suffix so any server key fires (plugin-scoped included).
The upgrade test proves a 0.23-era install re-wires to the new matcher
on the next init.

Users with a repo-level `mcp__<server>__.*` workaround entry should drop
it after upgrading, or each MCP call will count twice.

Fixes trailhq#834
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🌱 graft blast radius

1 area changed → 1 area can be affected. 1 dependent symbol, depth 2.
Tests: no test reaches Settings Merge.
Tag: @anirudhkumar-nanonets — Settings Merge, Trail Initialization · @Frankie-Xu — Settings Merge · @vidithsalla — Trail Initialization

flowchart TB
  A0(("Trail Initialization<br/>1 symbol"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Trail Initialization 1 src/claude/init.ts:L197-L256 runTrailInit — calls, depth 2 Settings Merge
Who knows this code — 3 people across 2 areas
Area Who knows it
Settings Merge · changed @anirudhkumar-nanonets — 2 commits, last 2d ago · @Frankie-Xu — 1 commit, last 9d ago
Trail Initialization · affected @anirudhkumar-nanonets — 1 commit, last 10d ago · @vidithsalla — 1 commit, last 10d ago

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 1 dependent symbol, grouped by area

Trail Initialization — 1 symbol in 1 file

  • src/claude/init.ts:L197-L256 — runTrailInit (calls, depth 2)
Test signal per changed area — 1 ✗

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✗ Settings Merge — 0 of 1 reached · no test file reaches it
    • not reached: hookBlocks
3 test suites also reference this code

3 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.

  • test/claude-hook-timeouts.test.ts
  • test/claude-pre-bash-wiring.test.ts
  • test/trail-init-layout.test.ts

graft blast · refs/graft/base...HEAD · depth 2 · 3 changed files

Open the interactive graph → — click an area to see the code that changed, and the line that reaches it.

github-actions Bot added a commit that referenced this pull request Nov 18, 2026
@hr1juldey
hr1juldey marked this pull request as ready for review November 18, 2026 14:30
@hr1juldey
hr1juldey marked this pull request as draft November 18, 2026 15:58
@hr1juldey

hr1juldey commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

cc @anirudhkumar-nanonets @shhdwi — no rush on any of these; whenever review slots free up. Thirteen small drafts are up, each tied to its open issue with tests and the full-suite baseline documented in the PR body: #850 (834, this one), #851 (817), #852 (762), #853 (624), #854 (832), #855 (827), #856 (816), #857 (438), #858 (433), #859 (833), #860 (829), #861 (635), #862 (425). Happy to rework anything or fold them together if you would rather review fewer.

This branch has not been deployed

No deployments
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.

tool-savings PostToolUse matcher never matches MCP tools; ToolSearch hint hardcodes the server key

1 participant