@@ -70,6 +70,7 @@ export async function run(): Promise<void> {
70
70
const enableMcp = core . getBooleanInput ( 'enable-mcp' ) || false
71
71
72
72
let azureTools : any [ ] = [ ]
73
+ let mcp : Client | null = null
73
74
74
75
// Connect to MCP server if enabled
75
76
if ( enableMcp || true ) {
@@ -86,7 +87,7 @@ export async function run(): Promise<void> {
86
87
}
87
88
)
88
89
89
- const mcp = new Client ( {
90
+ mcp = new Client ( {
90
91
name : 'ai-inference-action' ,
91
92
version : '1.0.0' ,
92
93
transport
@@ -153,21 +154,103 @@ export async function run(): Promise<void> {
153
154
)
154
155
}
155
156
156
- const modelResponse : string | null =
157
+ let modelResponse : string | null =
157
158
response . body . choices [ 0 ] . message . content
158
159
159
160
core . info ( `Model response: ${ response || 'No response content' } ` )
160
161
161
162
// Handle tool calls if present
162
163
const toolCalls = response . body . choices [ 0 ] . message . tool_calls
163
- if ( toolCalls && toolCalls . length > 0 ) {
164
+ if ( toolCalls && toolCalls . length > 0 && mcp ) {
164
165
core . info ( `Model requested ${ toolCalls . length } tool calls` )
165
- // Note: For now, we'll just log the tool calls
166
- // In a full implementation, you'd execute them via MCP and continue the conversation
166
+
167
+ // Execute tool calls via MCP and continue the conversation
168
+ const toolResults : any [ ] = [ ]
169
+
167
170
for ( const toolCall of toolCalls ) {
168
171
core . info (
169
- `Tool call : ${ toolCall . function . name } with args: ${ toolCall . function . arguments } `
172
+ `Executing tool : ${ toolCall . function . name } with args: ${ toolCall . function . arguments } `
170
173
)
174
+
175
+ try {
176
+ // Parse the arguments from JSON string
177
+ const args = JSON . parse ( toolCall . function . arguments )
178
+
179
+ // Call the tool via MCP
180
+ const result = await mcp . callTool ( {
181
+ name : toolCall . function . name ,
182
+ arguments : args
183
+ } )
184
+
185
+ core . info ( `Tool ${ toolCall . function . name } executed successfully` )
186
+
187
+ // Store the result for the follow-up conversation
188
+ toolResults . push ( {
189
+ tool_call_id : toolCall . id ,
190
+ role : 'tool' ,
191
+ name : toolCall . function . name ,
192
+ content : JSON . stringify ( result . content )
193
+ } )
194
+
195
+ } catch ( toolError ) {
196
+ core . warning ( `Failed to execute tool ${ toolCall . function . name } : ${ toolError } ` )
197
+
198
+ // Add error result to continue conversation
199
+ toolResults . push ( {
200
+ tool_call_id : toolCall . id ,
201
+ role : 'tool' ,
202
+ name : toolCall . function . name ,
203
+ content : `Error: ${ toolError } `
204
+ } )
205
+ }
206
+ }
207
+
208
+ // If we have tool results, continue the conversation
209
+ if ( toolResults . length > 0 ) {
210
+ core . info ( 'Continuing conversation with tool results...' )
211
+
212
+ // Build the follow-up request with the original conversation + tool results
213
+ const followUpMessages = [
214
+ {
215
+ role : 'system' ,
216
+ content : systemPrompt
217
+ } ,
218
+ { role : 'user' , content : prompt } ,
219
+ {
220
+ role : 'assistant' ,
221
+ content : modelResponse ,
222
+ tool_calls : toolCalls
223
+ } ,
224
+ ...toolResults
225
+ ]
226
+
227
+ const followUpRequest : any = {
228
+ messages : followUpMessages ,
229
+ max_tokens : maxTokens ,
230
+ model : modelName
231
+ }
232
+
233
+ // Add tools again for potential follow-up tool calls
234
+ if ( azureTools . length > 0 ) {
235
+ followUpRequest . tools = azureTools
236
+ }
237
+
238
+ const followUpResponse = await client . path ( '/chat/completions' ) . post ( {
239
+ body : followUpRequest
240
+ } )
241
+
242
+ if ( isUnexpected ( followUpResponse ) ) {
243
+ core . warning (
244
+ 'Failed to get follow-up response after tool execution: ' +
245
+ followUpResponse . status + ': ' + followUpResponse . body
246
+ )
247
+ } else {
248
+ const finalResponse = followUpResponse . body . choices [ 0 ] . message . content
249
+ core . info ( `Final response after tool execution: ${ finalResponse } ` )
250
+
251
+ // Update the model response to the final one
252
+ modelResponse = finalResponse || modelResponse
253
+ }
171
254
}
172
255
}
173
256
0 commit comments