🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Set tool schema follow strict mode (#2625)
Wendong-Fandd88eef89e4f
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex 631a03027..226e16061 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -26,7 +26,7 @@ body:attributes:label: What version of camel are you using?description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.- placeholder: E.g., 0.2.65+ placeholder: E.g., 0.2.66validations:required: truediff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.ymlindex 6201bf956..266eb226d 100644--- a/.github/workflows/build_package.yml+++ b/.github/workflows/build_package.yml@@ -141,7 +141,20 @@ jobs:CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"run: |source test_venv/bin/activate- pytest --fast-test-mode ./test+ pytest --fast-test-mode -m "not heavy_dependency" \+ --ignore=test/datagen/test_evol_instruct.py \+ --ignore=test/datagen/test_self_improving_cot_pipeline.py \+ --ignore=test/datasets/test_base_dataset.py \+ --ignore=test/datasets/test_self_instruct_generator.py \+ --ignore=test/embeddings/test_open_source_embeddings.py \+ --ignore=test/environments/test_multi_step_env.py \+ --ignore=test/environments/test_single_step_env.py \+ --ignore=test/environments/test_tictactoe_env.py \+ --ignore=test/interpreters/test_python_interpreter.py \+ --ignore=test/models/reward/test_nemotron_model.py \+ --ignore=test/toolkits/test_video_analysis_toolkit.py \+ --ignore=test/toolkits/test_jina_reranker_toolkit.py \+ ./test- name: Clean up after buildif: always()diff --git a/camel/__init__.py b/camel/__init__.pyindex d3d9f31cb..2421cfcc3 100644--- a/camel/__init__.py+++ b/camel/__init__.py@@ -14,7 +14,7 @@from camel.logger import disable_logging, enable_logging, set_log_level-__version__ = '0.2.65'+__version__ = '0.2.66'__all__ = ['__version__',diff --git a/camel/agents/mcp_agent.py b/camel/agents/mcp_agent.pyindex ea780a013..c1d13c45c 100644--- a/camel/agents/mcp_agent.py+++ b/camel/agents/mcp_agent.py@@ -125,7 +125,6 @@ class MCPAgent(ChatAgent):local_config_path: Optional[str] = None,tools: Optional[List[Union[FunctionTool, Callable]]] = None,function_calling_available: bool = True,- strict: bool = False,**kwargs,):if model is None:@@ -145,7 +144,6 @@ class MCPAgent(ChatAgent):self.local_config = local_configself.function_calling_available = function_calling_available- self.strict = strictif function_calling_available:sys_msg_content = "You are a helpful assistant, and you prefer "@@ -179,7 +177,7 @@ class MCPAgent(ChatAgent):if self.local_config:config_dict.update(self.local_config)- return MCPToolkit(config_dict=config_dict, strict=self.strict)+ return MCPToolkit(config_dict=config_dict)def add_registry(self, registry_config: BaseMCPRegistryConfig) -> None:r"""Add a new registry configuration to the agent.@@ -213,7 +211,6 @@ class MCPAgent(ChatAgent):] = None,model: Optional[BaseModelBackend] = None,function_calling_available: bool = False,- strict: bool = False,**kwargs,) -> "MCPAgent":r"""Create and connect an MCPAgent instance.@@ -279,7 +276,6 @@ class MCPAgent(ChatAgent):registry_configs=final_registry_configs,model=model,function_calling_available=function_calling_available,- strict=strict,**kwargs,)diff --git a/camel/toolkits/function_tool.py b/camel/toolkits/function_tool.pyindex 2c3621a04..77c76bf73 100644--- a/camel/toolkits/function_tool.py+++ b/camel/toolkits/function_tool.py@@ -190,7 +190,9 @@ def sanitize_and_enforce_required(parameters_dict):r"""Cleans and updates the function schema to conform with OpenAI'srequirements:- Removes invalid 'default' fields from the parameters schema.- - Ensures all fields or function parameters are marked as required.+ - Ensures all fields are marked as required or have null type for optional+ fields.+ - Recursively adds additionalProperties: false to all nested objects.Args:parameters_dict (dict): The dictionary representing the function@@ -198,8 +200,38 @@ def sanitize_and_enforce_required(parameters_dict):Returns:dict: The updated dictionary with invalid defaults removed and all- fields set as required.+ fields properly configured for strict mode."""++ def _add_additional_properties_false(obj):+ r"""Recursively add additionalProperties: false to all objects."""+ if isinstance(obj, dict):+ if (+ obj.get("type") == "object"+ and "additionalProperties" not in obj+ ):+ obj["additionalProperties"] = False++ # Process nested structures+ for key, value in obj.items():+ if key == "properties" and isinstance(value, dict):+ for prop_value in value.values():+ _add_additional_properties_false(prop_value)+ elif key in [+ "items",+ "allOf",+ "oneOf",+ "anyOf",+ ] and isinstance(value, (dict, list)):+ if isinstance(value, dict):+ _add_additional_properties_false(value)+ elif isinstance(value, list):+ for item in value:+ _add_additional_properties_false(item)+ elif key == "$defs" and isinstance(value, dict):+ for def_value in value.values():+ _add_additional_properties_false(def_value)+# Check if 'function' and 'parameters' existif ('function' in parameters_dict@@ -209,12 +241,52 @@ def sanitize_and_enforce_required(parameters_dict):parameters = parameters_dict['function']['parameters']properties = parameters.get('properties', {})- # Remove 'default' key from each property- for field in properties.values():- field.pop('default', None)+ # Track which fields should be required vs optional+ required_fields = []++ # Process each property+ for field_name, field_schema in properties.items():+ # Check if this field had a default value (making it optional)+ had_default = 'default' in field_schema++ # Remove 'default' key from field schema as required by OpenAI+ field_schema.pop('default', None)++ if had_default:+ # This field is optional - add null to its type+ current_type = field_schema.get('type')+ has_ref = '$ref' in field_schema++ if has_ref:+ # Fields with $ref shouldn't have additional type field+ # The $ref itself defines the type structure+ pass+ elif current_type:+ if isinstance(current_type, str):+ # Single type - convert to array with null+ field_schema['type'] = [current_type, 'null']+ elif (+ isinstance(current_type, list)+ and 'null' not in current_type+ ):+ # Array of types - add null if not present+ field_schema['type'] = [*current_type, 'null']+ else:+ # No type specified, add null type+ field_schema['type'] = ['null']++ # Optional fields are still marked as required in strict mode+ # but with null type to indicate they can be omitted+ required_fields.append(field_name)+ else:+ # This field is required+ required_fields.append(field_name)++ # Set all fields as required (strict mode requirement)+ parameters['required'] = required_fields- # Mark all keys in 'properties' as required- parameters['required'] = list(properties.keys())+ # Recursively add additionalProperties: false to all objects+ _add_additional_properties_false(parameters)return parameters_dictdiff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.pyindex 8c6f1de3b..3e306968d 100644--- a/camel/toolkits/mcp_toolkit.py+++ b/camel/toolkits/mcp_toolkit.py@@ -98,8 +98,6 @@ class MCPToolkit(BaseToolkit):timeout (Optional[float], optional): Timeout for connection attemptsin seconds. This timeout applies to individual client connections.(default: :obj:`None`)- strict (Optional[bool], optional): Flag to indicate strict mode.- (default: :obj:`False`)Note:At least one of :obj:`clients`, :obj:`config_path`, or@@ -148,7 +146,6 @@ class MCPToolkit(BaseToolkit):config_path: Optional[str] = None,config_dict: Optional[Dict[str, Any]] = None,timeout: Optional[float] = None,- strict: Optional[bool] = False,):# Call parent constructor firstsuper().__init__(timeout=timeout)@@ -165,7 +162,6 @@ class MCPToolkit(BaseToolkit):raise ValueError(error_msg)self.clients: List[MCPClient] = clients or []- self.strict = strict # Store strict parameterself._is_connected = Falseself._exit_stack: Optional[AsyncExitStack] = None@@ -313,7 +309,6 @@ class MCPToolkit(BaseToolkit):config_path: Optional[str] = None,config_dict: Optional[Dict[str, Any]] = None,timeout: Optional[float] = None,- strict: Optional[bool] = False,) -> "MCPToolkit":r"""Factory method that creates and connects to all MCP servers.@@ -331,8 +326,6 @@ class MCPToolkit(BaseToolkit):config file. (default: :obj:`None`)timeout (Optional[float], optional): Timeout for connectionattempts in seconds. (default: :obj:`None`)- strict (Optional[bool], optional): Flag to indicate strict mode.- (default: :obj:`False`)Returns:MCPToolkit: A fully initialized and connected :obj:`MCPToolkit`@@ -361,7 +354,6 @@ class MCPToolkit(BaseToolkit):config_path=config_path,config_dict=config_dict,timeout=timeout,- strict=strict,)try:await toolkit.connect()@@ -381,11 +373,10 @@ class MCPToolkit(BaseToolkit):config_path: Optional[str] = None,config_dict: Optional[Dict[str, Any]] = None,timeout: Optional[float] = None,- strict: Optional[bool] = False,) -> "MCPToolkit":r"""Synchronously create and connect to all MCP servers."""return run_async(cls.create)(- clients, config_path, config_dict, timeout, strict+ clients, config_path, config_dict, timeout)def _load_clients_from_config(self, config_path: str) -> List[MCPClient]:@@ -442,12 +433,10 @@ class MCPToolkit(BaseToolkit):try:# Use the new mcp_client factory function- # Pass timeout and strict from toolkit if available+ # Pass timeout from toolkit if availablekwargs = {}if hasattr(self, "timeout") and self.timeout is not None:kwargs["timeout"] = self.timeout- if hasattr(self, "strict") and self.strict is not None:- kwargs["strict"] = self.strictclient = create_mcp_client(cfg, **kwargs)return client@@ -455,17 +444,68 @@ class MCPToolkit(BaseToolkit):error_msg = f"Failed to create client for server '{name}': {e}"raise ValueError(error_msg) from e+ def _ensure_strict_tool_schema(self, tool: FunctionTool) -> FunctionTool:+ r"""Ensure a tool has a strict schema compatible with OpenAI's+ requirements.++ Args:+ tool (FunctionTool): The tool to check and update if necessary.++ Returns:+ FunctionTool: The tool with a strict schema.+ """+ try:+ schema = tool.get_openai_tool_schema()++ # Check if the tool already has strict mode enabled+ if schema.get("function", {}).get("strict") is True:+ return tool++ # Update the schema to be strict+ if "function" in schema:+ schema["function"]["strict"] = True++ # Ensure parameters have proper strict mode configuration+ parameters = schema["function"].get("parameters", {})+ if parameters:+ # Ensure additionalProperties is false+ parameters["additionalProperties"] = False++ # Process properties to handle optional fields+ properties = parameters.get("properties", {})+ parameters["required"] = list(properties.keys())++ # Apply the sanitization function from function_tool+ from camel.toolkits.function_tool import (+ sanitize_and_enforce_required,+ )++ schema = sanitize_and_enforce_required(schema)++ tool.set_openai_tool_schema(schema)+ logger.debug(+ f"Updated tool '{tool.get_function_name()}' to strict mode"+ )++ except Exception as e:+ logger.warning(f"Failed to ensure strict schema for tool: {e}")++ return tool+def get_tools(self) -> List[FunctionTool]:r"""Aggregates all tools from the managed MCP client instances.Collects and combines tools from all connected MCP clients into asingle unified list. Each tool is converted to a CAMEL-compatible- :obj:`FunctionTool` that can be used with CAMEL agents.+ :obj:`FunctionTool` that can be used with CAMEL agents. All tools+ are ensured to have strict schemas compatible with OpenAI's+ requirements.Returns:List[FunctionTool]: Combined list of all available function tools- from all connected MCP servers. Returns an empty list if no- clients are connected or if no tools are available.+ from all connected MCP servers with strict schemas. Returns an+ empty list if no clients are connected or if no tools are+ available.Note:This method can be called even when the toolkit is not connected,@@ -492,14 +532,25 @@ class MCPToolkit(BaseToolkit):for i, client in enumerate(self.clients):try:client_tools = client.get_tools()- all_tools.extend(client_tools)++ # Ensure all tools have strict schemas+ strict_tools = []+ for tool in client_tools:+ strict_tool = self._ensure_strict_tool_schema(tool)+ strict_tools.append(strict_tool)++ all_tools.extend(strict_tools)logger.debug(- f"Client {i+1} contributed {len(client_tools)} tools"+ f"Client {i+1} contributed {len(strict_tools)} "+ f"tools (strict mode enabled)")except Exception as e:logger.error(f"Failed to get tools from client {i+1}: {e}")- logger.info(f"Total tools available: {len(all_tools)}")+ logger.info(+ f"Total tools available: {len(all_tools)} (all with strict "+ f"schemas)"+ )return all_toolsdef get_text_tools(self) -> str:diff --git a/camel/utils/mcp_client.py b/camel/utils/mcp_client.pyindex ebd7914fb..5f1f6911a 100644--- a/camel/utils/mcp_client.py+++ b/camel/utils/mcp_client.py@@ -173,8 +173,6 @@ class MCPClient:initialization. (default: :obj:`None`)timeout (Optional[float], optional): Timeout for waiting for messagesfrom the server in seconds. (default: :obj:`10.0`)- strict (Optional[bool], optional): Strict mode for generating- FunctionTool objects. (default: :obj:`False`)Examples:STDIO server:@@ -209,20 +207,6 @@ class MCPClient:async with MCPClient({"url": "ws://localhost:8080/mcp"}) as client:tools = client.get_tools()- With strict mode enabled:-- .. code-block:: python-- async with MCPClient({- "command": "npx",- "args": [- "-y",- "@modelcontextprotocol/server-filesystem",- "/path"- ]- }, strict=True) as client:- tools = client.get_tools()-Attributes:config (ServerConfig): The server configuration object.client_info (Optional[types.Implementation]): Client implementation@@ -235,14 +219,12 @@ class MCPClient:config: Union[ServerConfig, Dict[str, Any]],client_info: Optional[types.Implementation] = None,timeout: Optional[float] = 10.0,- strict: Optional[bool] = False,):# Convert dict config to ServerConfig if neededif isinstance(config, dict):config = ServerConfig(**config)self.config = config- self.strict = strict# Validate transport type early (this will raise ValueError if invalid)_ = self.config.transport_type@@ -766,7 +748,6 @@ class MCPClient:"name": mcp_tool.name,"description": mcp_tool.descriptionor "No description provided.",- "strict": self.strict,"parameters": parameters,},}@@ -932,8 +913,7 @@ def create_mcp_client(dictionary is provided, it will be automatically converted toa :obj:`ServerConfig`.**kwargs: Additional keyword arguments passed to the :obj:`MCPClient`- constructor, such as :obj:`client_info`, :obj:`timeout`, and- :obj:`strict`.+ constructor, such as :obj:`client_info`, :obj:`timeout`.Returns:MCPClient: A configured :obj:`MCPClient` instance ready for use as@@ -972,20 +952,6 @@ def create_mcp_client("url": "ws://localhost:8080/mcp"}) as client:tools = client.get_tools()-- With strict mode enabled:-- .. code-block:: python-- async with create_mcp_client({- "command": "npx",- "args": [- "-y",- "@modelcontextprotocol/server-filesystem",- "/path",- ],- }, strict=True) as client:- tools = client.get_tools()"""return MCPClient(config, **kwargs)diff --git a/docs/conf.py b/docs/conf.pyindex ef6214dab..b3bb4fc74 100644--- a/docs/conf.py+++ b/docs/conf.py@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))project = 'CAMEL'copyright = '2024, CAMEL-AI.org'author = 'CAMEL-AI.org'-release = '0.2.65'+release = '0.2.66'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/examples/toolkits/playwright_mcp_toolkit.py b/examples/toolkits/playwright_mcp_toolkit.pyindex f9581e566..1606c470a 100644--- a/examples/toolkits/playwright_mcp_toolkit.py+++ b/examples/toolkits/playwright_mcp_toolkit.py@@ -13,6 +13,8 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import asyncio+from pydantic import BaseModel, Field+from camel.agents import ChatAgentfrom camel.models import ModelFactoryfrom camel.toolkits import PlaywrightMCPToolkit@@ -30,13 +32,19 @@ async def main():model_type=ModelType.DEFAULT,)+ # pydantic basemodel as input params format+ class AgentResponse(BaseModel):+ url: str = Field(description="Url of the repository")+ stars: int = Field(description="Stars of the repository")+chat_agent = ChatAgent(model=model,tools=playwright_mcp_toolkit.get_tools(),)response = await chat_agent.astep(- "search for camel-ai using google search, how many stars does it have?"+ "search for camel-ai using google search, how many stars does it has?",+ response_format=AgentResponse,)print(response.msg.content)@@ -50,7 +58,6 @@ if __name__ == "__main__":"""==============================================================================-The GitHub repository for **camel-ai** has **12,338 stars**. If you need more-information or details about the project, feel free to ask!+{"url":"https://github.com/camel-ai/camel","stars":12900}=============================================================================="""diff --git a/pyproject.toml b/pyproject.tomlindex c73143962..6d1a2e503 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "camel-ai"-version = "0.2.65"+version = "0.2.66"description = "Communicative Agents for AI Society Study"authors = [{ name = "CAMEL-AI.org" }]requires-python = ">=3.10,<3.13"diff --git a/test/toolkits/test_generate_openai_tool_schema.py b/test/toolkits/test_generate_openai_tool_schema.pyindex f6f3c2ba9..9de6b8095 100644--- a/test/toolkits/test_generate_openai_tool_schema.py+++ b/test/toolkits/test_generate_openai_tool_schema.py@@ -214,7 +214,7 @@ def test_function_tool_generate_schema_with_retries('description': 'The integer value to check.',},'b': {- 'type': 'string',+ 'type': ['string', 'null'],'description': ("The string to verify. Default is 'default'."),diff --git a/uv.lock b/uv.lockindex a31f88654..158fdfb8a 100644--- a/uv.lock+++ b/uv.lock@@ -547,7 +547,7 @@ wheels = [[[package]]name = "camel-ai"-version = "0.2.65"+version = "0.2.66"source = { editable = "." }dependencies = [{ name = "colorama" },@@ -4103,11 +4103,11 @@ wheels = [[[package]]name = "narwhals"-version = "1.42.0"+version = "1.42.1"source = { registry = "https://pypi.org/simple" }-sdist = { url = "https://files.pythonhosted.org/packages/a2/7e/9484c2427453bd0024fd36cf7923de4367d749f0b216b9ca56b9dfc3c516/narwhals-1.42.0.tar.gz", hash = "sha256:a5e554782446d1197593312651352cd39b2025e995053d8e6bdfaa01a70a91d3", size = 490671 }+sdist = { url = "https://files.pythonhosted.org/packages/df/d6/168a787b7800d6c89846b791e4f5ee6b94998a80c8c2838a019d3d71984d/narwhals-1.42.1.tar.gz", hash = "sha256:50a5635b11aeda98cf9c37e839fd34b0a24159f59a4dfae930290ad698320494", size = 492865 }wheels = [- { url = "https://files.pythonhosted.org/packages/8d/0f/f9ae7c8c55f9078c852b13ea4a6e92e5f4d6d4c8fc0781ec2882957006bb/narwhals-1.42.0-py3-none-any.whl", hash = "sha256:ef6cedf7700dc22c09d17973b9ede11b53e25331e238b24ac73884a8c5e27c19", size = 359033 },+ { url = "https://files.pythonhosted.org/packages/79/3f/8d450588206b437dd239a6d44230c63095e71135bd95d5a74347d07adbd5/narwhals-1.42.1-py3-none-any.whl", hash = "sha256:7a270d44b94ccdb277a799ae890c42e8504c537c1849f195eb14717c6184977a", size = 359888 },][[package]]