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: enable MCP to models without function calling (same as #1978, fix for a clear git history) (#2128)

Xiaotian Jinca5ef369eb88
diff --git a/.gitignore b/.gitignore
index f3e8aa18f..304d67d83 100644
--- a/.gitignore
+++ b/.gitignore
@@ -430,4 +430,4 @@ benchmark/gaia/Dataset
benchmark/gaia/results.jsonl
# Secret files for docker
-.container/.env
+.container/.env
\ No newline at end of file
diff --git a/camel/agents/__init__.py b/camel/agents/__init__.py
index 4206da9a9..ee9bad7c8 100644
--- a/camel/agents/__init__.py
+++ b/camel/agents/__init__.py
@@ -16,6 +16,7 @@ from .chat_agent import ChatAgent
from .critic_agent import CriticAgent
from .embodied_agent import EmbodiedAgent
from .knowledge_graph_agent import KnowledgeGraphAgent
+from .mcp_agent import MCPAgent
from .repo_agent import RepoAgent
from .role_assignment_agent import RoleAssignmentAgent
from .search_agent import SearchAgent
@@ -42,5 +43,6 @@ __all__ = [
'RoleAssignmentAgent',
'SearchAgent',
'KnowledgeGraphAgent',
+ 'MCPAgent',
'RepoAgent',
]
diff --git a/camel/agents/mcp_agent.py b/camel/agents/mcp_agent.py
new file mode 100644
index 000000000..427c77256
--- /dev/null
+++ b/camel/agents/mcp_agent.py
@@ -0,0 +1,233 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+import json
+import re
+from typing import Optional
+
+from camel.agents import ChatAgent
+from camel.logger import get_logger
+from camel.messages import BaseMessage
+from camel.models import BaseModelBackend
+from camel.prompts import TextPrompt
+from camel.responses import ChatAgentResponse
+from camel.toolkits import MCPToolkit
+from camel.types import RoleType
+
+# AgentOps decorator setting
+try:
+ import os
+
+ if os.getenv("AGENTOPS_API_KEY") is not None:
+ from agentops import track_agent
+ else:
+ raise ImportError
+except (ImportError, AttributeError):
+ from camel.utils import track_agent
+
+logger = get_logger(__name__)
+
+SYS_MSG_CONTENT = """
+You are a helpful assistant, and you prefer to use tools provided by the user
+to solve problems.
+Using a tool, you will tell the user `server_idx`, `tool_name` and
+`tool_args` formatted in JSON as following:
+```json
+{
+ "server_idx": idx,
+ "tool_name": "tool_name",
+ "tool_args": {
+ "arg1": value1,
+ "arg2": value2,
+ ...
+ }
+}
+```
+For example:
+```json
+{
+ "server_idx": 0,
+ "tool_name": "multiply",
+ "tool_args": {"a": 5, "b": 50}
+}
+```
+Otherwise, you should respond to the user directly.
+"""
+
+
+TOOLS_PROMPT = """
+## Available Tools:
+
+{tools}
+"""
+
+FINAL_RESPONSE_PROMPT = """
+The result `{results}` is provided by tools you proposed.
+Please answer me according to the result directly.
+"""
+
+
+@track_agent(name="MCPAgent")
+class MCPAgent(ChatAgent):
+ def __init__(
+ self,
+ config_path: str,
+ model: Optional[BaseModelBackend] = None,
+ function_calling_available: bool = False,
+ ) -> None:
+ r"""A class for the MCP agent that assists using MCP tools.
+
+ Args:
+ config_path (str): Path to the MCP configuration file.
+ model (Optional[BaseModelBackend]): Model backend for the agent.
+ (default: :obj:`None`)
+ function_calling_available (bool): Flag indicating whether the
+ model is equipped with the function calling ability.
+ (default: :obj:`False`)
+ """
+ if function_calling_available:
+ sys_msg_content = "You are a helpful assistant, and you prefer "
+ "to use tools provided by the user to solve problems."
+ else:
+ sys_msg_content = SYS_MSG_CONTENT
+
+ system_message = BaseMessage(
+ role_name="MCPRouter",
+ role_type=RoleType.ASSISTANT,
+ meta_dict=None,
+ content=sys_msg_content,
+ )
+
+ super().__init__(system_message, model=model)
+
+ self._mcp_toolkit = MCPToolkit(config_path=config_path)
+ self._function_calling_available = function_calling_available
+ self._text_tools = None
+
+ async def connect(self):
+ r"""Explicitly connect to all MCP servers."""
+ await self._mcp_toolkit.connect()
+
+ async def close(self):
+ r"""Explicitly disconnect from all MCP servers."""
+ await self._mcp_toolkit.disconnect()
+
+ def add_mcp_tools(self):
+ r"""Get the MCP tools and wrap into the models"""
+
+ if not self._mcp_toolkit.is_connected():
+ raise RuntimeError("The MCP server is not connected")
+
+ prompt = TextPrompt(TOOLS_PROMPT)
+ self._text_tools = prompt.format(
+ tools=self._mcp_toolkit.get_text_tools()
+ )
+ if self._function_calling_available:
+ tools = self._mcp_toolkit.get_tools()
+ for tool in tools:
+ self.add_tool(tool)
+
+ async def run(
+ self,
+ prompt: str,
+ ) -> ChatAgentResponse:
+ r"""Run the agent to interact with the MCP tools.
+
+ Args:
+ prompt (str): The user's input prompt or query to be processed by
+ the agent.
+
+ Returns:
+ ChatAgentResponse: The agent's response after processing the
+ prompt and potentially executing MCP tool calls.
+
+ Raises:
+ RuntimeError: If the MCP server is not connected when attempting
+ to run.
+ """
+
+ if not self._mcp_toolkit.is_connected():
+ raise RuntimeError("The MCP server is not connected")
+
+ if self._function_calling_available:
+ response = await self.astep(prompt)
+ return response
+ else:
+ task = f"## Task:\n {prompt}"
+ response = await self.astep(str(self._text_tools) + task)
+ content = response.msgs[0].content.lower()
+
+ tool_calls = []
+ while "```json" in content:
+ json_match = re.search(r'```json', content)
+ if not json_match:
+ break
+ json_start = json_match.span()[1]
+
+ end_match = re.search(r'```', content[json_start:])
+ if not end_match:
+ break
+ json_end = end_match.span()[0] + json_start
+
+ tool_json = content[json_start:json_end].strip('\n')
+ try:
+ tool_calls.append(json.loads(tool_json))
+ except json.JSONDecodeError:
+ logger.warning(f"Failed to parse JSON: {tool_json}")
+ continue
+ content = content[json_end:]
+
+ if not tool_calls:
+ return response
+ else:
+ tools_results = []
+ for tool_call in tool_calls:
+ server_idx = tool_call['server_idx']
+ tool_name = tool_call['tool_name']
+ tool_args = tool_call['tool_args']
+ server = self._mcp_toolkit.servers[server_idx]
+ result = await server.call_tool(tool_name, tool_args)
+ tools_results.append({tool_name: result.content[0].text})
+ results = json.dumps(tools_results)
+ final_prompt = TextPrompt(FINAL_RESPONSE_PROMPT).format(
+ results=results
+ )
+ response = await self.astep(final_prompt)
+ return response
+
+ @classmethod
+ async def create(
+ cls,
+ config_path: str,
+ model: Optional[BaseModelBackend] = None,
+ function_calling_available: bool = False,
+ ) -> "MCPAgent":
+ r"""Factory method to create and initialize an MCPAgent.
+
+ Args:
+ config_path (str): Path to the MCP configuration file that contains
+ server settings and other configuration parameters.
+ model (Optional[BaseModelBackend]): Model backend for the agent.
+ If None, the default model will be used. (default: :obj:`None`)
+ function_calling_available (bool): Flag indicating whether the
+ model is equipped with function calling ability. This affects
+ the system message content. (default: :obj:`False`)
+
+ Returns:
+ MCPAgent: A fully initialized MCPAgent instance ready for use.
+ """
+ agent = cls(config_path, model, function_calling_available)
+ await agent.connect()
+ agent.add_mcp_tools()
+ return agent
diff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.py
index 9375608f3..f8c37a3e7 100644
--- a/camel/toolkits/mcp_toolkit.py
+++ b/camel/toolkits/mcp_toolkit.py
@@ -227,7 +227,7 @@ class MCPClient(BaseToolkit):
func_params.append(param_name)
- async def dynamic_function(**kwargs):
+ async def dynamic_function(**kwargs) -> str:
r"""Auto-generated function for MCP Tool interaction.
Args:
@@ -351,6 +351,39 @@ class MCPClient(BaseToolkit):
for mcp_tool in self._mcp_tools
]
+ def get_text_tools(self) -> str:
+ r"""Returns a string containing the descriptions of the tools
+ in the toolkit.
+
+ Returns:
+ str: A string containing the descriptions of the tools
+ in the toolkit.
+ """
+ return "\n".join(
+ f"tool_name: {tool.name}\n"
+ + f"description: {tool.description or 'No description'}\n"
+ + f"input Schema: {tool.inputSchema}\n"
+ for tool in self._mcp_tools
+ )
+
+ async def call_tool(
+ self, tool_name: str, tool_args: Dict[str, Any]
+ ) -> Any:
+ r"""Calls the specified tool with the provided arguments.
+
+ Args:
+ tool_name (str): Name of the tool to call.
+ tool_args (Dict[str, Any]): Arguments to pass to the tool
+ (default: :obj:`{}`).
+
+ Returns:
+ Any: The result of the tool call.
+ """
+ if self._session is None:
+ raise RuntimeError("Session is not initialized.")
+
+ return await self._session.call_tool(tool_name, tool_args)
+
@property
def session(self) -> Optional["ClientSession"]:
return self._session
@@ -548,3 +581,13 @@ class MCPToolkit(BaseToolkit):
for server in self.servers:
all_tools.extend(server.get_tools())
return all_tools
+
+ def get_text_tools(self) -> str:
+ r"""Returns a string containing the descriptions of the tools
+ in the toolkit.
+
+ Returns:
+ str: A string containing the descriptions of the tools
+ in the toolkit.
+ """
+ return "\n".join(server.get_text_tools() for server in self.servers)
diff --git a/examples/agents/mcp_agent/calculator_server.py b/examples/agents/mcp_agent/calculator_server.py
new file mode 100644
index 000000000..002a98405
--- /dev/null
+++ b/examples/agents/mcp_agent/calculator_server.py
@@ -0,0 +1,85 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+from mcp.server.fastmcp import FastMCP
+
+# Create an MCP server
+mcp = FastMCP("Demo")
+
+
+# Add an addition tool
+def add(a: int, b: int) -> int:
+ r"""Add two numbers.
+
+ Args:
+ a (int): First number to add.
+ b (int): Second number to add.
+
+ Returns:
+ int: The sum of a and b.
+ """
+ return a + b
+
+
+def multiply(a: int, b: int) -> int:
+ r"""Multiply two numbers.
+
+ Args:
+ a (int): First number to multiply.
+ b (int): Second number to multiply.
+
+ Returns:
+ int: The product of a and b.
+ """
+ return a * b
+
+
+def compare(a: float, b: float) -> str:
+ r"""Compare two numbers and determine their relationship.
+
+ Args:
+ a (float): First number to compare.
+ b (float): Second number to compare.
+
+ Returns:
+ str: 'equal' if a equals b, 'greater' if a is greater than b,
+ 'less' if a is less than b.
+ """
+ if a == b:
+ return "equal"
+ elif a > b:
+ return "greater"
+ else:
+ return "less"
+
+
+# Add a dynamic greeting resource
[email protected]("greeting://{name}")
+def get_greeting(name: str) -> str:
+ r"""Get a personalized greeting message.
+
+ Args:
+ name (str): The name to include in the greeting.
+
+ Returns:
+ str: A personalized greeting string.
+ """
+ return f"Hello, {name}!"
+
+
+if __name__ == "__main__":
+ mcp.run()
diff --git a/examples/agents/mcp_agent/mcp_agent.py b/examples/agents/mcp_agent/mcp_agent.py
new file mode 100644
index 000000000..ad331806c
--- /dev/null
+++ b/examples/agents/mcp_agent/mcp_agent.py
@@ -0,0 +1,56 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import asyncio
+from pathlib import Path
+
+from camel.agents import MCPAgent
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType, ModelType
+
+
+async def main():
+ config_path = Path(__file__).parent / "mcp_servers_config.json"
+
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+
+ # Initialize the agent using the factory method (recommended)
+ mcp_agent = await MCPAgent.create(
+ config_path=str(config_path),
+ model=model,
+ function_calling_available=False,
+ )
+
+ user_msg = (
+ "I have 5 boxes, each of them containing 50 apples, "
+ "how many apples in total."
+ )
+ response = await mcp_agent.run(user_msg)
+
+ print(response.msgs[0].content)
+
+ # Disconnect from all MCP servers and clean up resources.
+ await mcp_agent.close()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
+
+"""
+==============================================================================
+The total number of apples in the 5 boxes is 250.
+==============================================================================
+"""
diff --git a/examples/agents/mcp_agent/mcp_servers_config.json b/examples/agents/mcp_agent/mcp_servers_config.json
new file mode 100644
index 000000000..c0649fef8
--- /dev/null
+++ b/examples/agents/mcp_agent/mcp_servers_config.json
@@ -0,0 +1,11 @@
+{
+ "mcpServers": {
+ "calculator": {
+ "command": "python",
+ "args": [
+ "examples/agents/mcp_agent/calculator_server.py"
+ ]
+ }
+ },
+ "mcpWebServers": {}
+}
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 967b184f8..7cc800ce3 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -32,6 +32,7 @@ dependencies = [
"psutil>=5.9.8,<6",
]
+
[project.optional-dependencies]
test = [
"pytest>=7,<8",
diff --git a/uv.lock b/uv.lock
index 95db79f92..9cf62fe38 100644
--- a/uv.lock
+++ b/uv.lock
@@ -3665,6 +3665,12 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/10/30/20a7f33b0b884a9d14dd3aa94ff1ac9da1479fe2ad66dd9e2736075d2506/mcp-1.6.0-py3-none-any.whl", hash = "sha256:7bd24c6ea042dbec44c754f100984d186620d8b841ec30f1b19eda9b93a634d0", size = 76077 },
]
+[package.optional-dependencies]
+cli = [
+ { name = "python-dotenv" },
+ { name = "typer" },
+]
+
[[package]]
name = "mcp-server-fetch"
version = "2025.1.17"