From baee2ea1e25698108ef6e29f6cc61c513b60b46a Mon Sep 17 00:00:00 2001 From: Yizhou Date: Tue, 15 Apr 2025 16:56:19 +0800 Subject: [PATCH] make sure tool is not repetitively added because mcp servers may provide the same tool --- app/tool/tool_collection.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/tool/tool_collection.py b/app/tool/tool_collection.py index 41c4d84..297ab6c 100644 --- a/app/tool/tool_collection.py +++ b/app/tool/tool_collection.py @@ -2,6 +2,7 @@ from typing import Any, Dict, List from app.exceptions import ToolError +from app.logger import logger from app.tool.base import BaseTool, ToolFailure, ToolResult @@ -48,11 +49,23 @@ class ToolCollection: return self.tool_map.get(name) def add_tool(self, tool: BaseTool): + """Add a single tool to the collection. + + If a tool with the same name already exists, it will be skipped and a warning will be logged. + """ + if tool.name in self.tool_map: + logger.warning(f"Tool {tool.name} already exists in collection, skipping") + return self + self.tools += (tool,) self.tool_map[tool.name] = tool return self def add_tools(self, *tools: BaseTool): + """Add multiple tools to the collection. + + If any tool has a name conflict with an existing tool, it will be skipped and a warning will be logged. + """ for tool in tools: self.add_tool(tool) return self