-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
I am creating an agent where I want to set the FunctionTool.needs_approval dynamically based on the description of the tool. e.g. if the tool has an explicit [ALWAYS ASK FOR APPROVAL] string inside its description.
Currently I can not do it because the RequireApprovalSetting does not accept a callable. It only accepts static parameters which does not help my use case because I want this field set dynamically per tool.
There is already a precedent for it in the form of ToolFilter that runs per tool. I want something similar for needs_approval that is executed per tool with the callable that I provide.
Note that there is a function _get_needs_approval_for_tool that does run per tool but the code that would run a callable would never get executed because self._needs_approval_policy is never a callable.
def _get_needs_approval_for_tool(
self,
tool: MCPTool,
agent: AgentBase | None,
) -> bool | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]]:
"""Return a FunctionTool.needs_approval value for a given MCP tool."""
policy = self._needs_approval_policy
if callable(policy):
if agent is None:
return False
async def _needs_approval(
run_context: RunContextWrapper[Any], _args: dict[str, Any], _call_id: str
) -> bool:
result = policy(run_context, agent, tool)
if inspect.isawaitable(result):
result = await result
return bool(result)
return _needs_approval
if isinstance(policy, dict):
return bool(policy.get(tool.name, False))
return bool(policy)