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 TerminalToolkit MCP server and examples (#2937)

Sarthak Bhardwaj275e4bdbf30d
diff --git a/examples/toolkits/mcp/mcp_terminal_toolkit/client.py b/examples/toolkits/mcp/mcp_terminal_toolkit/client.py
new file mode 100644
index 000000000..264e739fb
--- /dev/null
+++ b/examples/toolkits/mcp/mcp_terminal_toolkit/client.py
@@ -0,0 +1,97 @@
+# ========= 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
+import sys
+
+from mcp.types import CallToolResult
+
+from camel.toolkits.mcp_toolkit import MCPClient, MCPToolkit
+
+
+async def run_example_async():
+ """Example of using Terminal Toolkit MCP server asynchronously."""
+ config = {
+ "mcpServers": {
+ "terminal_toolkit": {
+ "command": sys.executable,
+ "args": [
+ "examples/toolkits/mcp/mcp_terminal_toolkit/terminal_toolkit_server.py",
+ "--mode",
+ "stdio",
+ ],
+ }
+ }
+ }
+ mcp_toolkit = MCPToolkit(config_dict=config, timeout=60)
+ async with mcp_toolkit:
+ mcp_client: MCPClient = mcp_toolkit.clients[0]
+ res = await mcp_client.list_mcp_tools()
+ if isinstance(res, str):
+ raise Exception(res)
+
+ tools = [tool.name for tool in res.tools]
+ print(f"Available tools: {tools}")
+
+ res1: CallToolResult = await mcp_client.session.call_tool(
+ "shell_exec", {"id": "test_session", "command": "ls -l"}
+ )
+ print(res1.content[0].text[:1000])
+
+
+def run_example_sync():
+ """Example of using Terminal Toolkit MCP server synchronously."""
+ config = {
+ "mcpServers": {
+ "terminal_toolkit": {
+ "command": sys.executable,
+ "args": [
+ "examples/toolkits/mcp/mcp_terminal_toolkit/terminal_toolkit_server.py",
+ "--mode",
+ "stdio",
+ ],
+ }
+ }
+ }
+ mcp_toolkit = MCPToolkit(config_dict=config, timeout=60)
+ with mcp_toolkit:
+ mcp_client: MCPClient = mcp_toolkit.clients[0]
+ res = mcp_client.list_mcp_tools_sync()
+ if isinstance(res, str):
+ raise Exception(res)
+
+ tools = [tool.name for tool in res.tools]
+ print(f"Available tools: {tools}")
+
+ res1: CallToolResult = mcp_client.call_tool_sync(
+ "shell_exec", {"id": "test_session_sync", "command": "ls -a"}
+ )
+ print(res1.content[0].text[:1000])
+
+
+if __name__ == "__main__":
+ print("Choose mode:")
+ print("1. Async example")
+ print("2. Sync example")
+
+ choice = input("Enter your choice (1 or 2): ").strip()
+
+ if choice == "1":
+ print("Running async example...")
+ asyncio.run(run_example_async())
+ elif choice == "2":
+ print("Running sync example...")
+ run_example_sync()
+ else:
+ print("Invalid choice. Running async example by default...")
+ asyncio.run(run_example_async())
diff --git a/examples/toolkits/mcp/mcp_terminal_toolkit/terminal_toolkit_server.py b/examples/toolkits/mcp/mcp_terminal_toolkit/terminal_toolkit_server.py
new file mode 100644
index 000000000..037865a71
--- /dev/null
+++ b/examples/toolkits/mcp/mcp_terminal_toolkit/terminal_toolkit_server.py
@@ -0,0 +1,41 @@
+# ========= 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 argparse
+import sys
+
+from camel.toolkits import TerminalToolkit
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(
+ description="Run Terminal Toolkit with MCP server mode.",
+ usage=f"python {sys.argv[0]} [--mode MODE] [--timeout TIMEOUT]",
+ )
+ parser.add_argument(
+ "--mode",
+ choices=["stdio", "sse", "streamable-http"],
+ default="stdio",
+ help="MCP server mode (default: 'stdio')",
+ )
+ parser.add_argument(
+ "--timeout",
+ type=float,
+ default=None,
+ help="Timeout for the MCP server (default: None)",
+ )
+
+ args = parser.parse_args()
+
+ toolkit = TerminalToolkit(timeout=args.timeout)
+
+ toolkit.run_mcp_server(mode=args.mode)