Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/mcp-use/src/agents/mcp_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ export class MCPAgent {
let eventCount = 0
let totalResponseLength = 0
let finalResponse = ''
const capturedToolCalls: Array<{ name: string; args: Record<string, unknown>; id?: string }> = []

// Enhance query with schema information if structured output is requested
if (outputSchema) {
Expand Down Expand Up @@ -1042,6 +1043,16 @@ export class MCPAgent {
totalResponseLength += event.data.chunk.content.length
}

// Capture tool calls as they are invoked, for inclusion in conversation history
if (event.event === 'on_tool_start') {
const toolInput = event.data?.input
capturedToolCalls.push({
name: event.name,
args: toolInput && typeof toolInput === 'object' ? toolInput as Record<string, unknown> : { input: toolInput },
id: event.run_id,
})
}

yield event

// Capture final response from chain end event
Expand Down Expand Up @@ -1130,8 +1141,13 @@ export class MCPAgent {
} as unknown as StreamEvent
}
} else if (this.memoryEnabled && finalResponse) {
// Add the final AI response to conversation history if memory is enabled
this.addToHistory(new AIMessage(finalResponse))
// Add the final AI response to conversation history if memory is enabled.
// Include any tool calls captured during execution so getConversationHistory()
// returns AIMessages with a populated tool_calls array.
this.addToHistory(new AIMessage({
content: finalResponse,
tool_calls: capturedToolCalls.length > 0 ? capturedToolCalls : undefined,
}))
}

logger.info(`🎉 StreamEvents complete - ${eventCount} events emitted`)
Expand Down