feat: add before_tool_call_hooks for pre-execution tool-call gating (fixes #2557)#2560
Open
kuangmi-bit wants to merge 1 commit into
Open
feat: add before_tool_call_hooks for pre-execution tool-call gating (fixes #2557)#2560kuangmi-bit wants to merge 1 commit into
kuangmi-bit wants to merge 1 commit into
Conversation
Add a supported extension point that lets host applications enforce policy before a tool with side effects executes — block, allow, or log — without subclassing or overriding internal methods. Changes: - Add AgentToolCallBlockedError to utils.py - Add before_tool_call_hooks parameter to MultiStepAgent.__init__() - Invoke hooks in process_single_tool_call() before execute_tool_call() - Each hook receives (tool_name, arguments, agent) and returns False to block or True/truthy to allow. Hooks that raise exceptions are caught and logged as warnings so one faulty hook doesn't break the agent loop. This mirrors CrewAI's before_tool_call hook and LangChain's wrap_tool_call middleware pattern, giving smolagents a first-class security boundary for MCP server connections, package installs, and other side-effecting tools. Fixes huggingface#2557
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a supported
before_tool_call_hooksextension point toMultiStepAgent, enabling pre-execution policy gates before tool calls with side effects. Fixes #2557.Problem
smolagentshasstep_callbacksbut they fire after tool execution via_finalize_step(). There is no way to intercept and potentially block a tool call before it runs — only post-hoc observation of what already happened.This is a gap compared to CrewAI (
before_tool_callhook) and LangChain (wrap_tool_callmiddleware), both of which let a host application enforce a policy boundary before tools with real-world side effects execute.Solution
Three changes across 2 files:
AgentToolCallBlockedError(new exception inutils.py) — raised when a hook blocks execution.before_tool_call_hooksparameter onMultiStepAgent.__init__()— a list of callables, each receiving(tool_name, arguments, agent)and returningFalseto block orTrue/truthy to allow.Hook invocation in
process_single_tool_call()beforeexecute_tool_call()— hooks are iterated before any tool runs. Exceptions from hooks (other thanAgentToolCallBlockedError) are caught and logged as warnings so one faulty hook doesn't break the agent loop.Usage
Design decisions
step_callbacks/final_answer_checkspattern (simple callable list, no registry class)None, no behavior change for existing users