diff --git a/backend/app/utils/agent.py b/backend/app/utils/agent.py index 64ac7486f..aac74f7f9 100644 --- a/backend/app/utils/agent.py +++ b/backend/app/utils/agent.py @@ -1,4 +1,5 @@ import asyncio +import copy import json import os import platform @@ -130,6 +131,11 @@ class ListenChatAgent(ChatAgent): process_task_id: str = "" + def _get_full_tool_schemas(self) -> List[Dict[str, Any]]: + """Return deep copies of tool schemas to prevent downstream mutation.""" + schemas = super()._get_full_tool_schemas() + return copy.deepcopy(schemas) + @traceroot.trace() def step( self, @@ -427,7 +433,7 @@ class ListenChatAgent(ChatAgent): # Clone tools and collect toolkits that need registration cloned_tools, toolkits_to_register = self._clone_tools() - + new_agent = ListenChatAgent( api_task_id=self.api_task_id, agent_name=self.agent_name, @@ -443,7 +449,6 @@ class ListenChatAgent(ChatAgent): response_terminators=self.response_terminators, scheduling_strategy=self.model_backend.scheduling_strategy.__name__, max_iteration=self.max_iteration, - agent_id=self.agent_id, stop_event=self.stop_event, tool_execution_timeout=self.tool_execution_timeout, mask_tool_output=self.mask_tool_output, diff --git a/backend/app/utils/toolkit/notion_mcp_toolkit.py b/backend/app/utils/toolkit/notion_mcp_toolkit.py index 36928aa0a..02eb68472 100644 --- a/backend/app/utils/toolkit/notion_mcp_toolkit.py +++ b/backend/app/utils/toolkit/notion_mcp_toolkit.py @@ -1,6 +1,4 @@ import os -from typing import Any, Dict, List -from loguru import logger from camel.toolkits import FunctionTool from app.component.environment import env from app.utils.toolkit.abstract_toolkit import AbstractToolkit @@ -35,67 +33,6 @@ class NotionMCPToolkit(MCPToolkit, AbstractToolkit): } super().__init__(config_dict=config_dict, timeout=timeout) - def get_tools(self) -> List[FunctionTool]: - r"""Returns a list of tools provided by the NotionMCPToolkit. - - Returns: - List[FunctionTool]: List of available tools. - """ - all_tools = [] - for client in self.clients: - try: - original_build_schema = client._build_tool_schema - - def create_wrapper(orig_func): - def wrapper(mcp_tool): - return self._build_custom_tool_schema( - mcp_tool, orig_func - ) - - return wrapper - - client._build_tool_schema = create_wrapper( # type: ignore[method-assign] - original_build_schema - ) - - client_tools = client.get_tools() - all_tools.extend(client_tools) - - client._build_tool_schema = original_build_schema # type: ignore[method-assign] - - except Exception as e: - logger.error(f"Failed to get tools from client: {e}") - return all_tools - - def _build_custom_tool_schema(self, mcp_tool, original_build_schema): - r"""Build tool schema with custom modifications.""" - schema = original_build_schema(mcp_tool) - self._customize_function_parameters(schema) - return schema - - def _customize_function_parameters(self, schema: Dict[str, Any]) -> None: - r"""Customize function parameters for specific functions. - - This method allows modifying parameter descriptions or other schema - attributes for specific functions. - """ - function_info = schema.get("function", {}) - function_name = function_info.get("name", "") - parameters = function_info.get("parameters", {}) - properties = parameters.get("properties", {}) - - # Modify the notion-create-pages function to make parent optional - if function_name == "notion-create-pages": - if "parent" in properties: - # Update the parent parameter description - properties["parent"]["description"] = ( - "Optional. The parent under which the new pages will be created. " - "This can be a page (page_id), a database page (database_id), or " - "a data source/collection under a database (data_source_id). " - "If omitted, the new pages will be created as private pages at the workspace level. " - "Use data_source_id when you have a collection:// URL from the fetch tool." - ) - @classmethod async def get_can_use_tools(cls, api_task_id: str) -> list[FunctionTool]: tools = []