4141from .messages import InputHistory , OutputMessage
4242from ..model import default_model , Model
4343from .converter import Converter
44+ from .subagent import Subagent , SubagentTool
4445
4546FAKE_ID = "__fake_id__"
4647
@@ -98,15 +99,36 @@ def model_settings(self) -> ModelSettings:
9899 return self ._model_settings
99100
100101 async def started (self ) -> None :
101- # Extract MCP clients from tools list
102- mcp_clients = [tool for tool in self ._tools if isinstance (tool , mcputil .Client )]
102+ final_tools : list [Callable ] = []
103103
104- # Filter out MCP clients from tools list
105- self ._tools = [
106- tool for tool in self ._tools if not isinstance (tool , mcputil .Client )
104+ mcp_clients : list [mcputil .Client ] = []
105+ subagents : list [Subagent ] = []
106+
107+ for tool in self ._tools :
108+ if isinstance (tool , mcputil .Client ):
109+ mcp_clients .append (tool )
110+ elif isinstance (tool , Subagent ):
111+ subagents .append (tool )
112+ else :
113+ final_tools .append (tool )
114+
115+ # Load tools from MCP clients
116+ mcp_tools = await self ._load_mcp_tools (mcp_clients )
117+ final_tools .extend (mcp_tools )
118+
119+ # Create tools from subagents
120+ subagent_tools = [
121+ SubagentTool (self , subagent ).as_tool () for subagent in subagents
107122 ]
123+ final_tools .extend (subagent_tools )
124+
125+ self ._tools = final_tools
126+
127+ async def _load_mcp_tools (
128+ self , mcp_clients : list [mcputil .Client ]
129+ ) -> list [Callable ]:
130+ """Load tools from all MCP clients concurrently."""
108131
109- # Load tools from all MCP clients concurrently
110132 async def get_client_tools (client ):
111133 try :
112134 return await client .get_tools ()
@@ -120,9 +142,7 @@ async def get_client_tools(client):
120142 * [get_client_tools (client ) for client in mcp_clients ]
121143 )
122144
123- # Add all tools to the tools list
124- for mcp_tools in all_mcp_tools :
125- self ._tools .extend (mcp_tools )
145+ return [tool for sublist in all_mcp_tools for tool in sublist ]
126146
127147 @handler
128148 async def handle_history (
0 commit comments