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

fix: Current type errors in service/agent_mcp module (#2522)

Xiaotian Jin70c91cd8d984
diff --git a/examples/agent/mcp/agent_to_mcp.py b/examples/agents/agent_to_mcp.py
similarity index 100%
rename from examples/agent/mcp/agent_to_mcp.py
rename to examples/agents/agent_to_mcp.py
diff --git a/examples/agent/agents_share_modelmanager.py b/examples/agents/agents_share_modelmanager.py
similarity index 100%
rename from examples/agent/agents_share_modelmanager.py
rename to examples/agents/agents_share_modelmanager.py
diff --git a/services/__init__.py b/services/__init__.py
new file mode 100644
index 000000000..877d47ecc
--- /dev/null
+++ b/services/__init__.py
@@ -0,0 +1,14 @@
+# ========= 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. =========
+# Services package
diff --git a/services/agent_config.py b/services/agent_mcp/agent_config.py
similarity index 97%
rename from services/agent_config.py
rename to services/agent_mcp/agent_config.py
index 8d6a82f0e..f7301e92d 100644
--- a/services/agent_config.py
+++ b/services/agent_mcp/agent_config.py
@@ -74,10 +74,11 @@ The reasoning agent is a helpful assistant that can reason about the world.
# Create another agent for searching the web
+toolkit = SearchToolkit()
search_agent = ChatAgent(
model=search_model,
system_message="You are a helpful assistant.",
- tools=SearchToolkit().get_tools(), # Add search tool
+ tools=toolkit.get_tools(), # type: ignore[arg-type]
)
search_agent_description = """
diff --git a/services/agent_mcp_server.py b/services/agent_mcp/agent_mcp_server.py
similarity index 91%
rename from services/agent_mcp_server.py
rename to services/agent_mcp/agent_mcp_server.py
index 973779be2..df73aedd1 100644
--- a/services/agent_mcp_server.py
+++ b/services/agent_mcp/agent_mcp_server.py
@@ -16,7 +16,7 @@ from typing import Any, Dict, Optional
from mcp.server.fastmcp import Context, FastMCP
from camel.utils import model_from_json_schema
-from services.agent_config import agents_dict, description_dict
+from services.agent_mcp.agent_config import agents_dict, description_dict
mcp = FastMCP("ChatAgentMCP", dependencies=["camel-ai[all]"])
@@ -54,7 +54,7 @@ async def step(
"DynamicResponseFormat", response_format
)
- ctx.info(f"The agent {name} is processing the message: {message}")
+ await ctx.info(f"The agent {name} is processing the message: {message}")
response = await agent.astep(message, format_cls)
return {
@@ -66,7 +66,7 @@ async def step(
@mcp.tool()
-def reset(ctx: Context) -> Dict[str, str]:
+async def reset(ctx: Context) -> Dict[str, str]:
r"""Reset the chat agent to its initial state.
Args:
@@ -78,12 +78,12 @@ def reset(ctx: Context) -> Dict[str, str]:
"""
for agent in agents_dict.values():
agent.reset()
- ctx.info("All agents reset successfully")
+ await ctx.info("All agents reset successfully")
return {"status": "success", "message": "All agents reset successfully"}
@mcp.tool()
-def set_output_language(language: str, ctx: Context) -> Dict[str, str]:
+async def set_output_language(language: str, ctx: Context) -> Dict[str, str]:
r"""Set the output language for the chat agent.
Args:
@@ -96,7 +96,7 @@ def set_output_language(language: str, ctx: Context) -> Dict[str, str]:
"""
for agent in agents_dict.values():
agent.output_language = language
- ctx.info(f"Output language set to '{language}'")
+ await ctx.info(f"Output language set to '{language}'")
return {
"status": "success",
"message": f"Output language set to '{language}'",
@@ -133,7 +133,10 @@ def get_chat_history(name: str) -> Dict[str, Any]:
"status": "error",
"message": f"Agent with name {name} not found",
}
- return agent.chat_history
+ return {
+ "status": "success",
+ "chat_history": agent.chat_history,
+ }
@mcp.resource("agent://{name}")
@@ -158,7 +161,6 @@ def get_agent_info(name: str) -> Dict[str, Any]:
info = {
"agent_id": agent.agent_id,
"model_type": str(agent.model_type),
- "token_limit": agent.token_limit,
"output_language": agent.output_language,
"description": description_dict[name],
}
diff --git a/test/services/test_agent_mcp_server.py b/test/services/test_agent_mcp_server.py
index 94b78e60c..501b269af 100644
--- a/test/services/test_agent_mcp_server.py
+++ b/test/services/test_agent_mcp_server.py
@@ -19,8 +19,8 @@ from mcp.server.fastmcp import Context
from camel.messages import BaseMessage
from camel.responses import ChatAgentResponse
from camel.toolkits import FunctionTool
-from services.agent_config import agents_dict, description_dict
-from services.agent_mcp_server import (
+from services.agent_mcp.agent_config import agents_dict, description_dict
+from services.agent_mcp.agent_mcp_server import (
get_agent_info,
get_agents_info,
get_available_tools,
@@ -43,13 +43,14 @@ def mock_agent():
]
agent.tool_dict = {"test_tool": mock_tool}
agent.astep = AsyncMock()
+ agent.reset = Mock()
return agent
@pytest.fixture
def mock_context():
context = Mock(spec=Context)
- context.info = Mock()
+ context.info = AsyncMock()
return context
@@ -113,9 +114,10 @@ async def test_step_agent_not_found(mock_context):
assert "not found" in result["message"]
-def test_reset(mock_agent, mock_context):
+async def test_reset(mock_agent, mock_context):
with patch.dict(agents_dict, {"test": mock_agent}):
- result = reset(mock_context)
+ result = await reset(mock_context)
assert result["status"] == "success"
assert "successfully" in result["message"]
@@ -123,9 +125,10 @@ def test_reset(mock_agent, mock_context):
mock_context.info.assert_called_once()
-def test_set_output_language(mock_agent, mock_context):
+async def test_set_output_language(mock_agent, mock_context):
with patch.dict(agents_dict, {"test": mock_agent}):
- result = set_output_language("fr", mock_context)
+ result = await set_output_language("fr", mock_context)
assert result["status"] == "success"
assert "fr" in result["message"]
@@ -141,7 +144,15 @@ def test_get_agents_info():
def test_get_chat_history(mock_agent):
with patch.dict(agents_dict, {"test": mock_agent}):
result = get_chat_history("test")
- assert result == mock_agent.chat_history
+ assert result["status"] == "success"
+ assert result["chat_history"] == mock_agent.chat_history
+
+
+def test_get_chat_history_agent_not_found():
+ with patch.dict(agents_dict, {}, clear=True):
+ result = get_chat_history("nonexistent")
+ assert result["status"] == "error"
+ assert "not found" in result["message"]
def test_get_agent_info(mock_agent):
@@ -158,7 +169,21 @@ def test_get_agent_info(mock_agent):
assert result["description"] == test_description
+def test_get_agent_info_agent_not_found():
+ with patch.dict(agents_dict, {}, clear=True):
+ result = get_agent_info("nonexistent")
+ assert result["status"] == "error"
+ assert "not found" in result["message"]
+
+
def test_get_available_tools(mock_agent):
with patch.dict(agents_dict, {"test": mock_agent}):
result = get_available_tools("test")
assert result == mock_agent.tool_dict
+
+
+def test_get_available_tools_agent_not_found():
+ with patch.dict(agents_dict, {}, clear=True):
+ result = get_available_tools("nonexistent")
+ assert result["status"] == "error"
+ assert "not found" in result["message"]