Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Add 'strict' parameter to constructors of MCPClient and MCPToolkit classes (#2290)

Lei Zhang9746a8c108b5
diff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.py
index b71042ef7..9375608f3 100644
--- a/camel/toolkits/mcp_toolkit.py
+++ b/camel/toolkits/mcp_toolkit.py
@@ -60,6 +60,8 @@ class MCPClient(BaseToolkit):
timeout (Optional[float]): Connection timeout. (default: :obj:`'None'`)
headers (Dict[str, str]): Headers for the HTTP request.
(default: :obj:`'None'`)
+ strict (Optional[bool]): Whether to enforce strict mode for the
+ function call. (default: :obj:`False`)
"""
def __init__(
@@ -69,6 +71,7 @@ class MCPClient(BaseToolkit):
env: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
headers: Optional[Dict[str, str]] = None,
+ strict: Optional[bool] = False,
):
from mcp import Tool
@@ -78,6 +81,7 @@ class MCPClient(BaseToolkit):
self.args = args or []
self.env = env or {}
self.headers = headers or {}
+ self.strict = strict
self._mcp_tools: List[Tool] = []
self._session: Optional['ClientSession'] = None
@@ -317,13 +321,15 @@ class MCPClient(BaseToolkit):
"additionalProperties": False,
}
+ # Because certain parameters in MCP may include keywords that are not
+ # supported by OpenAI, it is essential to set "strict" to False.
return {
"type": "function",
"function": {
"name": mcp_tool.name,
"description": mcp_tool.description
or "No description provided.",
- "strict": True,
+ "strict": self.strict,
"parameters": parameters,
},
}
@@ -363,6 +369,8 @@ class MCPToolkit(BaseToolkit):
instances to manage.
config_path (Optional[str]): Path to a JSON configuration file
defining MCP servers.
+ strict (Optional[bool]): Whether to enforce strict mode for the
+ function call. (default: :obj:`False`)
Note:
Either `servers` or `config_path` must be provided. If both are
@@ -397,6 +405,7 @@ class MCPToolkit(BaseToolkit):
self,
servers: Optional[List[MCPClient]] = None,
config_path: Optional[str] = None,
+ strict: Optional[bool] = False,
):
super().__init__()
@@ -409,16 +418,22 @@ class MCPToolkit(BaseToolkit):
self.servers: List[MCPClient] = servers or []
if config_path:
- self.servers.extend(self._load_servers_from_config(config_path))
+ self.servers.extend(
+ self._load_servers_from_config(config_path, strict)
+ )
self._exit_stack = AsyncExitStack()
self._connected = False
- def _load_servers_from_config(self, config_path: str) -> List[MCPClient]:
+ def _load_servers_from_config(
+ self, config_path: str, strict: Optional[bool] = False
+ ) -> List[MCPClient]:
r"""Loads MCP server configurations from a JSON file.
Args:
config_path (str): Path to the JSON configuration file.
+ strict (bool): Whether to enforce strict mode for the
+ function call. (default: :obj:`False`)
Returns:
List[MCPClient]: List of configured MCPClient instances.
@@ -462,6 +477,7 @@ class MCPToolkit(BaseToolkit):
args=cfg.get("args", []),
env={**os.environ, **cfg.get("env", {})},
timeout=cfg.get("timeout", None),
+ strict=strict,
)
all_servers.append(server)
diff --git a/test/toolkits/test_mcp_toolkit.py b/test/toolkits/test_mcp_toolkit.py
index b232f8994..6526f5f4b 100644
--- a/test/toolkits/test_mcp_toolkit.py
+++ b/test/toolkits/test_mcp_toolkit.py
@@ -264,7 +264,7 @@ class TestMCPClient:
"function": {
"name": "test_function",
"description": "Test function description",
- "strict": True,
+ "strict": False,
"parameters": {
"type": "object",
"properties": {
@@ -286,7 +286,7 @@ class TestMCPClient:
"function": {
"name": "test_function",
"description": "No description provided.",
- "strict": True,
+ "strict": False,
"parameters": {
"type": "object",
"properties": {