@@ -40,6 +40,10 @@ export interface ToolCallExecutionResult {
4040 finalAnswer ?: { text : string ; interrupted : boolean } ;
4141}
4242
43+ export type ToolExecutionEvent =
44+ | { type : "update" ; update : MessageUpdate }
45+ | { type : "complete" ; summary : ToolCallExecutionResult } ;
46+
4347const serverMap = ( servers : McpServerConfig [ ] ) : Map < string , McpServerConfig > => {
4448 const map = new Map < string , McpServerConfig > ( ) ;
4549 for ( const server of servers ) {
@@ -57,7 +61,7 @@ export async function* executeToolCalls({
5761 parseArgs,
5862 toPrimitive,
5963 collectToolOutputSources,
60- } : ExecuteToolCallsParams ) : AsyncGenerator < MessageUpdate , ToolCallExecutionResult , undefined > {
64+ } : ExecuteToolCallsParams ) : AsyncGenerator < ToolExecutionEvent , void , undefined > {
6165 const toolMessages : ChatCompletionMessageParam [ ] = [ ] ;
6266 const toolRuns : ToolRun [ ] = [ ] ;
6367 const serverLookup = serverMap ( servers ) ;
@@ -75,48 +79,68 @@ export async function* executeToolCalls({
7579 }
7680
7781 yield {
78- type : MessageUpdateType . Tool ,
79- subtype : MessageToolUpdateType . Call ,
80- uuid,
81- call : { name : call . name , parameters : parametersClean } ,
82+ type : "update" ,
83+ update : {
84+ type : MessageUpdateType . Tool ,
85+ subtype : MessageToolUpdateType . Call ,
86+ uuid,
87+ call : { name : call . name , parameters : parametersClean } ,
88+ } ,
8289 } ;
8390
8491 yield {
85- type : MessageUpdateType . Tool ,
86- subtype : MessageToolUpdateType . ETA ,
87- uuid,
88- eta : 10 ,
92+ type : "update" ,
93+ update : {
94+ type : MessageUpdateType . Tool ,
95+ subtype : MessageToolUpdateType . ETA ,
96+ uuid,
97+ eta : 10 ,
98+ } ,
8999 } ;
90100
91101 if ( ! mappingEntry ) {
92102 const message = `Unknown MCP function: ${ call . name } ` ;
93103 yield {
94- type : MessageUpdateType . Tool ,
95- subtype : MessageToolUpdateType . Error ,
96- uuid,
97- message,
104+ type : "update" ,
105+ update : {
106+ type : MessageUpdateType . Tool ,
107+ subtype : MessageToolUpdateType . Error ,
108+ uuid,
109+ message,
110+ } ,
98111 } ;
99- return {
100- toolMessages,
101- toolRuns,
102- finalAnswer : { text : message , interrupted : false } ,
112+ yield {
113+ type : "complete" ,
114+ summary : {
115+ toolMessages,
116+ toolRuns,
117+ finalAnswer : { text : message , interrupted : false } ,
118+ } ,
103119 } ;
120+ return ;
104121 }
105122
106123 const serverConfig = serverLookup . get ( mappingEntry . server ) ;
107124 if ( ! serverConfig ) {
108125 const message = `Unknown MCP server: ${ mappingEntry . server } ` ;
109126 yield {
110- type : MessageUpdateType . Tool ,
111- subtype : MessageToolUpdateType . Error ,
112- uuid,
113- message,
127+ type : "update" ,
128+ update : {
129+ type : MessageUpdateType . Tool ,
130+ subtype : MessageToolUpdateType . Error ,
131+ uuid,
132+ message,
133+ } ,
114134 } ;
115- return {
116- toolMessages,
117- toolRuns,
118- finalAnswer : { text : message , interrupted : false } ,
135+ yield {
136+ type : "complete" ,
137+ summary : {
138+ toolMessages,
139+ toolRuns,
140+ finalAnswer : { text : message , interrupted : false } ,
141+ } ,
119142 } ;
143+ return ;
120144 }
121145
122146 try {
@@ -132,19 +156,22 @@ export async function* executeToolCalls({
132156 ) ;
133157
134158 yield {
135- type : MessageUpdateType . Tool ,
136- subtype : MessageToolUpdateType . Result ,
137- uuid,
138- result : {
139- status : ToolResultStatus . Success ,
140- call : { name : call . name , parameters : parametersClean } ,
141- outputs : [
142- {
143- content : output ,
144- sources : outputSources ,
145- } ,
146- ] ,
147- display : true ,
159+ type : "update" ,
160+ update : {
161+ type : MessageUpdateType . Tool ,
162+ subtype : MessageToolUpdateType . Result ,
163+ uuid,
164+ result : {
165+ status : ToolResultStatus . Success ,
166+ call : { name : call . name , parameters : parametersClean } ,
167+ outputs : [
168+ {
169+ content : output ,
170+ sources : outputSources ,
171+ } ,
172+ ] ,
173+ display : true ,
174+ } ,
148175 } ,
149176 } ;
150177
@@ -157,18 +184,28 @@ export async function* executeToolCalls({
157184 "[mcp] tool call failed"
158185 ) ;
159186 yield {
160- type : MessageUpdateType . Tool ,
161- subtype : MessageToolUpdateType . Error ,
162- uuid,
163- message,
187+ type : "update" ,
188+ update : {
189+ type : MessageUpdateType . Tool ,
190+ subtype : MessageToolUpdateType . Error ,
191+ uuid,
192+ message,
193+ } ,
164194 } ;
165- return {
166- toolMessages,
167- toolRuns,
168- finalAnswer : { text : `MCP error: ${ message } ` , interrupted : false } ,
195+ yield {
196+ type : "complete" ,
197+ summary : {
198+ toolMessages,
199+ toolRuns,
200+ finalAnswer : { text : `MCP error: ${ message } ` , interrupted : false } ,
201+ } ,
169202 } ;
203+ return ;
170204 }
171205 }
172206
173- return { toolMessages, toolRuns } ;
207+ yield {
208+ type : "complete" ,
209+ summary : { toolMessages, toolRuns } ,
210+ } ;
174211}
0 commit comments