Skip to content

fix: sanitize tool names in hook decorator filters#5342

Merged
greysonlalonde merged 2 commits intomainfrom
fix/hook-tool-name-sanitize
Apr 8, 2026
Merged

fix: sanitize tool names in hook decorator filters#5342
greysonlalonde merged 2 commits intomainfrom
fix/hook-tool-name-sanitize

Conversation

@greysonlalonde
Copy link
Copy Markdown
Contributor

@greysonlalonde greysonlalonde commented Apr 8, 2026

Summary

  • Auto-sanitizes the tools parameter in before_tool_call/after_tool_call decorators via sanitize_tool_name(), so users can pass BaseTool.name directly without knowing the internal normalization
  • Fixes tool_utils.py to pass the sanitized name to ToolCallHookContext, matching the behavior in crew_agent_executor.py

Closes #5335

Test plan

  • New test test_before_tool_call_tool_filter_sanitizes_names verifies unsanitized names like "Delete File" match sanitized context names like "delete_file"
  • All 14 existing hook decorator tests pass

Note

Medium Risk
Changes the ToolCallHookContext.tool_name value passed to hooks and normalizes decorator tools filters, which can alter which hooks run for projects relying on unsanitized names.

Overview
Tool hook decorators now auto-sanitize the tools filter list via sanitize_tool_name(), allowing users to pass human-readable BaseTool.name values while still matching internal normalized tool names.

Tool execution in tool_utils.py now populates ToolCallHookContext.tool_name with the sanitized name (for both before/after hooks, sync and async), aligning hook filtering and logging behavior. Adds a regression test ensuring unsanitized filter names like "Delete File" match contexts like "delete_file".

Reviewed by Cursor Bugbot for commit 14b3934. Bugbot is set up for automated code reviews on this repo. Configure here.

Addresses #5335

The tools parameter in before_tool_call/after_tool_call decorators now
auto-sanitizes names via sanitize_tool_name(), so users can pass
BaseTool.name directly without knowing the internal normalization.

Also fixes tool_utils.py to pass the sanitized name to
ToolCallHookContext for consistency with crew_agent_executor.py.

Closes #5335
@github-actions github-actions Bot added the size/S label Apr 8, 2026
Copy link
Copy Markdown
Contributor

@iris-clawd iris-clawd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Sanitize tool names in hook decorator filters

Clean bugfix. Small, focused, correct.

✅ All good

  • Decorator sanitization — sanitizes at decoration time (once), not on every hook invocation. Right call for performance.
  • tool_utils.py fixToolCallHookContext now gets sanitized_tool_name instead of tool_calling.tool_name, making both sides consistent. This was the actual bug — the decorator side could sanitize all it wants, but if the context still passes the unsanitized name, the filter comparison fails.
  • Test — covers the exact user scenario (pass "Delete File", match against "delete_file" in context). Also verifies non-matching tools are still filtered.

No issues. LGTM 🚀 💬 188

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4163547. Configure here.

Comment thread lib/crewai/src/crewai/utilities/tool_utils.py
Copy link
Copy Markdown
Contributor

@iris-clawd iris-clawd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

14b3934 applies the same sanitized_tool_name fix to the sync code path — good catch, the first commit only covered async. Both before/after hooks in the sync path now consistent. Still LGTM 🚀 💬 189

@greysonlalonde greysonlalonde merged commit 98e0d10 into main Apr 8, 2026
49 checks passed
@greysonlalonde greysonlalonde deleted the fix/hook-tool-name-sanitize branch April 8, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] [UX] before_tool_call decorator's tools parameter should auto-normalize tool names to match ToolCallHookContext.tool_name

2 participants