Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions astrbot/core/agent/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ def empty(self) -> bool:
return len(self.tools) == 0

def add_tool(self, tool: FunctionTool) -> None:
"""Add a tool to the set."""
# 检查是否已存在同名工具
"""Add a tool to the set.

If a tool with the same name already exists:
- Prefer the one that is active (active=True)
- If both have the same active state, use the new one (overwrite)
"""
for i, existing_tool in enumerate(self.tools):
if existing_tool.name == tool.name:
self.tools[i] = tool
# Overwrite unless existing is active and new is not
if not (existing_tool.active and not tool.active):
Comment thread
whatevertogo marked this conversation as resolved.
Outdated
Comment thread
whatevertogo marked this conversation as resolved.
Outdated
self.tools[i] = tool
return
self.tools.append(tool)

Expand Down
16 changes: 14 additions & 2 deletions astrbot/core/provider/func_tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,25 @@ def remove_func(self, name: str) -> None:
break

def get_func(self, name) -> FuncTool | None:
# 优先返回已激活的工具
for f in self.func_list:
if f.name == name and getattr(f, "active", False):
return f
# 退化则拿最后一个覆盖的(后加载的覆盖前面的,通常是 MCP 工具)
for f in reversed(self.func_list):
if f.name == name:
return f
Comment thread
whatevertogo marked this conversation as resolved.
Outdated
Comment thread
whatevertogo marked this conversation as resolved.
return None

def get_full_tool_set(self) -> ToolSet:
"""获取完整工具集"""
tool_set = ToolSet(self.func_list.copy())
"""获取完整工具集

使用 add_tool 进行填充,确保同名工具只保留最后一个(后加载的覆盖前面的)。
这样 MCP 工具可以正确覆盖被禁用的内置工具。
Comment thread
whatevertogo marked this conversation as resolved.
Outdated
"""
tool_set = ToolSet()
for tool in self.func_list:
tool_set.add_tool(copy.deepcopy(tool))
Comment thread
whatevertogo marked this conversation as resolved.
Outdated
return tool_set

@staticmethod
Expand Down
Loading