|
| 1 | +from typing import Dict, List, Optional |
| 2 | + |
| 3 | +from pydantic import Field, model_validator |
| 4 | + |
| 5 | +from app.agent.browser import BrowserContextHelper |
| 6 | +from app.agent.toolcall import ToolCallAgent |
| 7 | +from app.config import config |
| 8 | +from app.daytona.sandbox import create_sandbox, delete_sandbox |
| 9 | +from app.daytona.tool_base import SandboxToolsBase |
| 10 | +from app.logger import logger |
| 11 | +from app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT |
| 12 | +from app.tool import Terminate, ToolCollection |
| 13 | +from app.tool.ask_human import AskHuman |
| 14 | +from app.tool.mcp import MCPClients, MCPClientTool |
| 15 | +from app.tool.sandbox.sb_browser_tool import SandboxBrowserTool |
| 16 | +from app.tool.sandbox.sb_files_tool import SandboxFilesTool |
| 17 | +from app.tool.sandbox.sb_shell_tool import SandboxShellTool |
| 18 | +from app.tool.sandbox.sb_vision_tool import SandboxVisionTool |
| 19 | + |
| 20 | + |
| 21 | +class SandboxManus(ToolCallAgent): |
| 22 | + """A versatile general-purpose agent with support for both local and MCP tools.""" |
| 23 | + |
| 24 | + name: str = "SandboxManus" |
| 25 | + description: str = "A versatile agent that can solve various tasks using multiple sandbox-tools including MCP-based tools" |
| 26 | + |
| 27 | + system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root) |
| 28 | + next_step_prompt: str = NEXT_STEP_PROMPT |
| 29 | + |
| 30 | + max_observe: int = 10000 |
| 31 | + max_steps: int = 20 |
| 32 | + |
| 33 | + # MCP clients for remote tool access |
| 34 | + mcp_clients: MCPClients = Field(default_factory=MCPClients) |
| 35 | + |
| 36 | + # Add general-purpose tools to the tool collection |
| 37 | + available_tools: ToolCollection = Field( |
| 38 | + default_factory=lambda: ToolCollection( |
| 39 | + # PythonExecute(), |
| 40 | + # BrowserUseTool(), |
| 41 | + # StrReplaceEditor(), |
| 42 | + AskHuman(), |
| 43 | + Terminate(), |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + special_tool_names: list[str] = Field(default_factory=lambda: [Terminate().name]) |
| 48 | + browser_context_helper: Optional[BrowserContextHelper] = None |
| 49 | + |
| 50 | + # Track connected MCP servers |
| 51 | + connected_servers: Dict[str, str] = Field( |
| 52 | + default_factory=dict |
| 53 | + ) # server_id -> url/command |
| 54 | + _initialized: bool = False |
| 55 | + sandbox_link: Optional[dict[str, dict[str, str]]] = Field(default_factory=dict) |
| 56 | + |
| 57 | + @model_validator(mode="after") |
| 58 | + def initialize_helper(self) -> "SandboxManus": |
| 59 | + """Initialize basic components synchronously.""" |
| 60 | + self.browser_context_helper = BrowserContextHelper(self) |
| 61 | + return self |
| 62 | + |
| 63 | + @classmethod |
| 64 | + async def create(cls, **kwargs) -> "SandboxManus": |
| 65 | + """Factory method to create and properly initialize a Manus instance.""" |
| 66 | + instance = cls(**kwargs) |
| 67 | + await instance.initialize_mcp_servers() |
| 68 | + await instance.initialize_sandbox_tools() |
| 69 | + instance._initialized = True |
| 70 | + return instance |
| 71 | + |
| 72 | + async def initialize_sandbox_tools( |
| 73 | + self, |
| 74 | + password: str = config.daytona.VNC_password, |
| 75 | + ) -> None: |
| 76 | + try: |
| 77 | + # 创建新沙箱 |
| 78 | + if password: |
| 79 | + sandbox = create_sandbox(password=password) |
| 80 | + self.sandbox = sandbox |
| 81 | + else: |
| 82 | + raise ValueError("password must be provided") |
| 83 | + vnc_link = sandbox.get_preview_link(6080) |
| 84 | + website_link = sandbox.get_preview_link(8080) |
| 85 | + vnc_url = vnc_link.url if hasattr(vnc_link, "url") else str(vnc_link) |
| 86 | + website_url = ( |
| 87 | + website_link.url if hasattr(website_link, "url") else str(website_link) |
| 88 | + ) |
| 89 | + |
| 90 | + # Get the actual sandbox_id from the created sandbox |
| 91 | + actual_sandbox_id = sandbox.id if hasattr(sandbox, "id") else "new_sandbox" |
| 92 | + if not self.sandbox_link: |
| 93 | + self.sandbox_link = {} |
| 94 | + self.sandbox_link[actual_sandbox_id] = { |
| 95 | + "vnc": vnc_url, |
| 96 | + "website": website_url, |
| 97 | + } |
| 98 | + logger.info(f"VNC URL: {vnc_url}") |
| 99 | + logger.info(f"Website URL: {website_url}") |
| 100 | + SandboxToolsBase._urls_printed = True |
| 101 | + sb_tools = [ |
| 102 | + SandboxBrowserTool(sandbox), |
| 103 | + SandboxFilesTool(sandbox), |
| 104 | + SandboxShellTool(sandbox), |
| 105 | + SandboxVisionTool(sandbox), |
| 106 | + ] |
| 107 | + self.available_tools.add_tools(*sb_tools) |
| 108 | + |
| 109 | + except Exception as e: |
| 110 | + logger.error(f"Error initializing sandbox tools: {e}") |
| 111 | + raise |
| 112 | + |
| 113 | + async def initialize_mcp_servers(self) -> None: |
| 114 | + """Initialize connections to configured MCP servers.""" |
| 115 | + for server_id, server_config in config.mcp_config.servers.items(): |
| 116 | + try: |
| 117 | + if server_config.type == "sse": |
| 118 | + if server_config.url: |
| 119 | + await self.connect_mcp_server(server_config.url, server_id) |
| 120 | + logger.info( |
| 121 | + f"Connected to MCP server {server_id} at {server_config.url}" |
| 122 | + ) |
| 123 | + elif server_config.type == "stdio": |
| 124 | + if server_config.command: |
| 125 | + await self.connect_mcp_server( |
| 126 | + server_config.command, |
| 127 | + server_id, |
| 128 | + use_stdio=True, |
| 129 | + stdio_args=server_config.args, |
| 130 | + ) |
| 131 | + logger.info( |
| 132 | + f"Connected to MCP server {server_id} using command {server_config.command}" |
| 133 | + ) |
| 134 | + except Exception as e: |
| 135 | + logger.error(f"Failed to connect to MCP server {server_id}: {e}") |
| 136 | + |
| 137 | + async def connect_mcp_server( |
| 138 | + self, |
| 139 | + server_url: str, |
| 140 | + server_id: str = "", |
| 141 | + use_stdio: bool = False, |
| 142 | + stdio_args: List[str] = None, |
| 143 | + ) -> None: |
| 144 | + """Connect to an MCP server and add its tools.""" |
| 145 | + if use_stdio: |
| 146 | + await self.mcp_clients.connect_stdio( |
| 147 | + server_url, stdio_args or [], server_id |
| 148 | + ) |
| 149 | + self.connected_servers[server_id or server_url] = server_url |
| 150 | + else: |
| 151 | + await self.mcp_clients.connect_sse(server_url, server_id) |
| 152 | + self.connected_servers[server_id or server_url] = server_url |
| 153 | + |
| 154 | + # Update available tools with only the new tools from this server |
| 155 | + new_tools = [ |
| 156 | + tool for tool in self.mcp_clients.tools if tool.server_id == server_id |
| 157 | + ] |
| 158 | + self.available_tools.add_tools(*new_tools) |
| 159 | + |
| 160 | + async def disconnect_mcp_server(self, server_id: str = "") -> None: |
| 161 | + """Disconnect from an MCP server and remove its tools.""" |
| 162 | + await self.mcp_clients.disconnect(server_id) |
| 163 | + if server_id: |
| 164 | + self.connected_servers.pop(server_id, None) |
| 165 | + else: |
| 166 | + self.connected_servers.clear() |
| 167 | + |
| 168 | + # Rebuild available tools without the disconnected server's tools |
| 169 | + base_tools = [ |
| 170 | + tool |
| 171 | + for tool in self.available_tools.tools |
| 172 | + if not isinstance(tool, MCPClientTool) |
| 173 | + ] |
| 174 | + self.available_tools = ToolCollection(*base_tools) |
| 175 | + self.available_tools.add_tools(*self.mcp_clients.tools) |
| 176 | + |
| 177 | + async def delete_sandbox(self, sandbox_id: str) -> None: |
| 178 | + """Delete a sandbox by ID.""" |
| 179 | + try: |
| 180 | + await delete_sandbox(sandbox_id) |
| 181 | + logger.info(f"Sandbox {sandbox_id} deleted successfully") |
| 182 | + if sandbox_id in self.sandbox_link: |
| 183 | + del self.sandbox_link[sandbox_id] |
| 184 | + except Exception as e: |
| 185 | + logger.error(f"Error deleting sandbox {sandbox_id}: {e}") |
| 186 | + raise e |
| 187 | + |
| 188 | + async def cleanup(self): |
| 189 | + """Clean up Manus agent resources.""" |
| 190 | + if self.browser_context_helper: |
| 191 | + await self.browser_context_helper.cleanup_browser() |
| 192 | + # Disconnect from all MCP servers only if we were initialized |
| 193 | + if self._initialized: |
| 194 | + await self.disconnect_mcp_server() |
| 195 | + await self.delete_sandbox(self.sandbox.id if self.sandbox else "unknown") |
| 196 | + self._initialized = False |
| 197 | + |
| 198 | + async def think(self) -> bool: |
| 199 | + """Process current state and decide next actions with appropriate context.""" |
| 200 | + if not self._initialized: |
| 201 | + await self.initialize_mcp_servers() |
| 202 | + self._initialized = True |
| 203 | + |
| 204 | + original_prompt = self.next_step_prompt |
| 205 | + recent_messages = self.memory.messages[-3:] if self.memory.messages else [] |
| 206 | + browser_in_use = any( |
| 207 | + tc.function.name == SandboxBrowserTool().name |
| 208 | + for msg in recent_messages |
| 209 | + if msg.tool_calls |
| 210 | + for tc in msg.tool_calls |
| 211 | + ) |
| 212 | + |
| 213 | + if browser_in_use: |
| 214 | + self.next_step_prompt = ( |
| 215 | + await self.browser_context_helper.format_next_step_prompt() |
| 216 | + ) |
| 217 | + |
| 218 | + result = await super().think() |
| 219 | + |
| 220 | + # Restore original prompt |
| 221 | + self.next_step_prompt = original_prompt |
| 222 | + |
| 223 | + return result |
0 commit comments