diff --git a/astrbot/core/agent/runners/tool_loop_agent_runner.py b/astrbot/core/agent/runners/tool_loop_agent_runner.py index 14013c090..33298e895 100644 --- a/astrbot/core/agent/runners/tool_loop_agent_runner.py +++ b/astrbot/core/agent/runners/tool_loop_agent_runner.py @@ -209,9 +209,38 @@ async def _handle_function_tools( ) continue + valid_params = {} # 参数过滤:只传递函数实际需要的参数 + + # 获取实际的 handler 函数 + if func_tool.handler: + logger.debug( + f"工具 {func_tool_name} 期望的参数: {func_tool.parameters}" + ) + if func_tool.parameters and func_tool.parameters.get("properties"): + expected_params = set(func_tool.parameters["properties"].keys()) + + valid_params = { + k: v + for k, v in func_tool_args.items() + if k in expected_params + } + + # 记录被忽略的参数 + ignored_params = set(func_tool_args.keys()) - set( + valid_params.keys() + ) + if ignored_params: + logger.warning( + f"工具 {func_tool_name} 忽略非期望参数: {ignored_params}" + ) + else: + # 如果没有 handler(如 MCP 工具),使用所有参数 + valid_params = func_tool_args + logger.warning(f"工具 {func_tool_name} 没有 handler,使用所有参数") + try: await self.agent_hooks.on_tool_start( - self.run_context, func_tool, func_tool_args + self.run_context, func_tool, valid_params ) except Exception as e: logger.error(f"Error in on_tool_start hook: {e}", exc_info=True) @@ -219,7 +248,7 @@ async def _handle_function_tools( executor = self.tool_executor.execute( tool=func_tool, run_context=self.run_context, - **func_tool_args, + **valid_params, # 只传递有效的参数 ) _final_resp: CallToolResult | None = None