@@ -32,7 +32,7 @@ class Agent {
3232 private userMessage : Message [ ] ;
3333
3434
35- constructor ( tools :any = [ ] , systemPrompt : SystemPrompt , maxRun : number = 0 , subAgents : Agent [ ] = [ ] ) {
35+ constructor ( tools : any = [ ] , systemPrompt : SystemPrompt , maxRun : number = 0 , subAgents : Agent [ ] = [ ] ) {
3636 this . tools = tools ;
3737 this . userMessage = [ ] ;
3838 this . apiConversationHistory = [ ] ;
@@ -42,10 +42,10 @@ class Agent {
4242
4343 }
4444
45- async run ( task : TaskInstruction , successCondition : ( ) => boolean = ( ) => true ) : Promise < { success : boolean ; error : string | null } > {
45+ async run ( task : TaskInstruction , successCondition : ( ) => boolean = ( ) => true ) : Promise < { success : boolean ; error : string | null , message : string | null } > {
4646
4747
48- let mentaionedMCPSTool :any [ ] = await task . userMessage . getMentionedMcpsTools ( ) ;
48+ let mentaionedMCPSTool : any [ ] = await task . userMessage . getMentionedMcpsTools ( ) ;
4949 this . tools = [
5050 ...this . tools ,
5151 ...mentaionedMCPSTool
@@ -56,7 +56,6 @@ class Agent {
5656 let userMessages = await task . toPrompt ( ) ;
5757 this . apiConversationHistory . push ( { role : "user" , content : userMessages } ) ;
5858 let runcomplete = 0 ;
59-
6059 while ( ! completed && ( runcomplete <= this . maxRun || this . maxRun === 0 ) ) {
6160 try {
6261 runcomplete ++ ;
@@ -95,14 +94,28 @@ class Agent {
9594 if ( toolName . includes ( "attempt_completion" ) ) {
9695 taskCompletedBlock = tool ;
9796 } else {
98- console . log ( "calling tool with params" , toolName , toolInput ) ;
99- const [ didUserReject , result ] = await this . executeTool ( toolName , toolInput ) ;
100- console . log ( "tool result" , result ) ;
101- toolResults . push ( this . getToolResult ( toolUseId , result ) ) ;
10297
103- if ( didUserReject ) {
104- userRejectedToolUse = true ;
98+ let [ serverName , nameOfTool ] = toolName . replace ( '--' , ':' ) . split ( ':' ) ;
99+ if ( serverName == 'subagent' ) {
100+ console . log ( "calling agent with params" , nameOfTool , toolInput ) ;
101+ const [ didUserReject , result ] = await this . startSubAgent ( toolName , toolInput ) ;
102+ console . log ( "tool result" , result ) ;
103+ toolResults . push ( this . getToolResult ( toolUseId , result ) ) ;
104+ if ( didUserReject ) {
105+ userRejectedToolUse = true ;
106+ }
107+ }
108+ else {
109+ console . log ( "calling tool with params" , toolName , toolInput ) ;
110+ const [ didUserReject , result ] = await this . executeTool ( toolName , toolInput ) ;
111+ console . log ( "tool result" , result ) ;
112+ toolResults . push ( this . getToolResult ( toolUseId , result ) ) ;
113+
114+ if ( didUserReject ) {
115+ userRejectedToolUse = true ;
116+ }
105117 }
118+
106119 }
107120 } else {
108121 toolResults . push ( this . getToolResult ( toolUseId , "Skipping tool execution due to previous tool user rejection." ) ) ;
@@ -140,14 +153,20 @@ class Agent {
140153 }
141154 }
142155 } catch ( error ) {
143- return { success : false , error : error instanceof Error ? error . message : String ( error ) } ;
156+ return { success : false , error : error instanceof Error ? error . message : String ( error ) , message : null } ;
144157 }
145158 } catch ( error ) {
146- return { success : false , error : error instanceof Error ? error . message : String ( error ) } ;
159+ return { success : false , error : error instanceof Error ? error . message : String ( error ) , message : null } ;
147160 }
148161 }
149162
150- return { success : completed , error : null } ;
163+ return {
164+ success : completed ,
165+ error : null ,
166+ message : this . apiConversationHistory
167+ . filter ( msg => msg . role === 'assistant' )
168+ . pop ( ) ?. content as string || ''
169+ } ;
151170 }
152171
153172 private async attemptLlmRequest ( apiConversationHistory : Message [ ] , tools : Record < string , any > ) : Promise < any > {
@@ -178,6 +197,9 @@ class Agent {
178197 private async executeTool ( toolName : string , toolInput : any ) : Promise < [ boolean , any ] > {
179198 return mcp . executeTool ( toolName , toolInput ) ;
180199 }
200+ private async startSubAgent ( agentName : string , params : any ) : Promise < [ boolean , any ] > {
201+ return mcp . executeTool ( agentName , params ) ;
202+ }
181203
182204 private getToolDetail ( tool : any ) : ToolDetails {
183205 return {
0 commit comments