🐫 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: sync mcp_toolkit (#2431)
Saed Bhati5a2e6a5d5753
diff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.pyindex 502e8e500..8a46cb1c5 100644--- a/camel/toolkits/mcp_toolkit.py+++ b/camel/toolkits/mcp_toolkit.py@@ -37,6 +37,7 @@ if TYPE_CHECKING:from camel.logger import get_loggerfrom camel.toolkits import BaseToolkit, FunctionTool+from camel.utils.commons import run_asynclogger = get_logger(__name__)@@ -220,6 +221,10 @@ class MCPClient(BaseToolkit):logger.error(f"Failed to connect to MCP server: {e}")raise e+ def connect_sync(self):+ r"""Synchronously connect to the MCP server."""+ return run_async(self.connect)()+async def disconnect(self):r"""Explicitly disconnect from the MCP server."""# If the server is not connected, do nothing@@ -235,6 +240,10 @@ class MCPClient(BaseToolkit):self._exit_stack = AsyncExitStack()self._session = None+ def disconnect_sync(self):+ r"""Synchronously disconnect from the MCP server."""+ return run_async(self.disconnect)()+@asynccontextmanagerasync def connection(self):r"""Async context manager for establishing and managing the connection@@ -254,6 +263,10 @@ class MCPClient(BaseToolkit):except Exception as e:logger.warning(f"Error: {e}")+ def connection_sync(self):+ r"""Synchronously connect to the MCP server."""+ return run_async(self.connection)()+async def list_mcp_tools(self) -> Union[str, "ListToolsResult"]:r"""Retrieves the list of available tools from the connected MCPserver.@@ -269,6 +282,11 @@ class MCPClient(BaseToolkit):logger.exception("Failed to list MCP tools")raise e+ def list_mcp_tools_sync(self) -> Union[str, "ListToolsResult"]:+ r"""Synchronously list the available tools from the connected MCP+ server."""+ return run_async(self.list_mcp_tools)()+def generate_function_from_mcp_tool(self, mcp_tool: "Tool") -> Callable:r"""Dynamically generates a Python callable function corresponding toa given MCP tool.@@ -390,6 +408,10 @@ class MCPClient(BaseToolkit):return dynamic_function+ def generate_function_from_mcp_tool_sync(self, mcp_tool: "Tool") -> Any:+ r"""Synchronously generate a function from an MCP tool."""+ return run_async(self.generate_function_from_mcp_tool)(mcp_tool)+def _build_tool_schema(self, mcp_tool: "Tool") -> Dict[str, Any]:input_schema = mcp_tool.inputSchemaproperties = input_schema.get("properties", {})@@ -463,6 +485,10 @@ class MCPClient(BaseToolkit):return await self._session.call_tool(tool_name, tool_args)+ def call_tool_sync(self, tool_name: str, tool_args: Dict[str, Any]) -> Any:+ r"""Synchronously call a tool."""+ return run_async(self.call_tool)(tool_name, tool_args)+@propertydef session(self) -> Optional["ClientSession"]:return self._session@@ -521,6 +547,21 @@ class MCPClient(BaseToolkit):logger.error(f"Failed to initialize MCPClient: {e}")raise RuntimeError(f"Failed to initialize MCPClient: {e}") from e+ @classmethod+ def create_sync(+ self,+ command_or_url: str,+ args: Optional[List[str]] = None,+ env: Optional[Dict[str, str]] = None,+ timeout: Optional[float] = None,+ headers: Optional[Dict[str, str]] = None,+ mode: Optional[str] = None,+ ) -> "MCPClient":+ r"""Synchronously create and connect to the MCP server."""+ return run_async(self.create)(+ command_or_url, args, env, timeout, headers, mode+ )+async def __aenter__(self) -> "MCPClient":r"""Async context manager entry point. Automatically connects to theMCP server when used in an async with statement.@@ -531,12 +572,35 @@ class MCPClient(BaseToolkit):await self.connect()return self- async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:+ def __enter__(self) -> "MCPClient":+ r"""Synchronously enter the async context manager."""+ return run_async(self.__aenter__)()++ async def __aexit__(self) -> None:r"""Async context manager exit point. Automatically disconnects fromthe MCP server when exiting an async with statement.++ Returns:+ None"""await self.disconnect()+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:+ r"""Synchronously exit the async context manager.++ Args:+ exc_type (Optional[Type[Exception]]): The type of exception that+ occurred during the execution of the with statement.+ exc_val (Optional[Exception]): The exception that occurred during+ the execution of the with statement.+ exc_tb (Optional[TracebackType]): The traceback of the exception+ that occurred during the execution of the with statement.++ Returns:+ None+ """+ return run_async(self.__aexit__)()+class MCPToolkit(BaseToolkit):r"""MCPToolkit provides a unified interface for managing multiple@@ -749,6 +813,10 @@ class MCPToolkit(BaseToolkit):logger.error(f"Failed to connect to one or more MCP servers: {e}")raise e+ def connect_sync(self):+ r"""Synchronously connect to all MCP servers."""+ return run_async(self.connect)()+async def disconnect(self):r"""Explicitly disconnect from all MCP servers."""if not self._connected:@@ -758,6 +826,10 @@ class MCPToolkit(BaseToolkit):await server.disconnect()self._connected = False+ def disconnect_sync(self):+ r"""Synchronously disconnect from all MCP servers."""+ return run_async(self.disconnect)()+@asynccontextmanagerasync def connection(self) -> AsyncGenerator["MCPToolkit", None]:r"""Async context manager that simultaneously establishes connections@@ -772,6 +844,10 @@ class MCPToolkit(BaseToolkit):finally:await self.disconnect()+ def connection_sync(self):+ r"""Synchronously connect to all MCP servers."""+ return run_async(self.connection)()+def is_connected(self) -> bool:r"""Checks if all the managed servers are connected.diff --git a/camel/utils/__init__.py b/camel/utils/__init__.pyindex f226edabd..1f873216f 100644--- a/camel/utils/__init__.py+++ b/camel/utils/__init__.py@@ -35,6 +35,7 @@ from .commons import (json_to_function_code,print_text_animated,retry_on_error,+ run_async,text_extract_from_web,to_pascal,track_agent,@@ -95,4 +96,5 @@ __all__ = ["model_from_json_schema","sanitize_filename","browser_toolkit_save_auth_cookie",+ "run_async",]diff --git a/camel/utils/commons.py b/camel/utils/commons.pyindex 6d7db4d3e..94f627568 100644--- a/camel/utils/commons.py+++ b/camel/utils/commons.py@@ -1103,3 +1103,30 @@ def browser_toolkit_save_auth_cookie(context.storage_state(path=cookie_json_path)browser.close() # Close the browser when finished+++def run_async(func: Callable[..., Any]) -> Callable[..., Any]:+ r"""Helper function to run async functions in synchronous context.++ Args:+ func (Callable[..., Any]): The async function to wrap.++ Returns:+ Callable[..., Any]: A synchronous wrapper for the async function.+ """++ @wraps(func)+ def wrapper(*args: Any, **kwargs: Any) -> Any:+ try:+ loop = asyncio.get_event_loop()+ except RuntimeError:+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)++ if loop.is_closed():+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)++ return loop.run_until_complete(func(*args, **kwargs))++ return wrapperdiff --git a/examples/mcp_arxiv_toolkit/arxiv_toolkit_server.py b/examples/toolkits/mcp/mcp_arxiv_toolkit/arxiv_toolkit_server.pysimilarity index 100%rename from examples/mcp_arxiv_toolkit/arxiv_toolkit_server.pyrename to examples/toolkits/mcp/mcp_arxiv_toolkit/arxiv_toolkit_server.pydiff --git a/examples/mcp_arxiv_toolkit/client.py b/examples/toolkits/mcp/mcp_arxiv_toolkit/client.pysimilarity index 100%rename from examples/mcp_arxiv_toolkit/client.pyrename to examples/toolkits/mcp/mcp_arxiv_toolkit/client.pydiff --git a/examples/toolkits/mcp/mcp_arxiv_toolkit/client_sync.py b/examples/toolkits/mcp/mcp_arxiv_toolkit/client_sync.pynew file mode 100644index 000000000..3275fb25b--- /dev/null+++ b/examples/toolkits/mcp/mcp_arxiv_toolkit/client_sync.py@@ -0,0 +1,45 @@+# ========= 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.types import CallToolResult++from camel.toolkits.mcp_toolkit import MCPClient, MCPToolkit+++def run_example():+ mcp_toolkit = MCPToolkit(+ config_path="examples/mcp_arxiv_toolkit/mcp_servers_config.json"+ )++ mcp_toolkit.connect_sync()++ # call the server to list the available tools+ mcp_client: MCPClient = mcp_toolkit.servers[0]+ res = mcp_client.list_mcp_tools_sync()+ if isinstance(res, str):+ raise Exception(res)++ tools = [tool.name for tool in res]+ print(tools)++ res1: CallToolResult = mcp_client.call_tool_sync(+ "search_papers", {"query": "attention is all you need"}+ )+ print(res1.content[0].text[:1000])++ mcp_toolkit.disconnect_sync()+++if __name__ == "__main__":+ run_example()diff --git a/examples/mcp_arxiv_toolkit/mcp_servers_config.json b/examples/toolkits/mcp/mcp_arxiv_toolkit/mcp_servers_config.jsonsimilarity index 100%rename from examples/mcp_arxiv_toolkit/mcp_servers_config.jsonrename to examples/toolkits/mcp/mcp_arxiv_toolkit/mcp_servers_config.json