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
31 changes: 16 additions & 15 deletions src/agents/mcp_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,7 @@ export class MCPAgent {
= query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, ' ')}...` : query.replace(/\n/g, ' ')
logger.info(`💬 Received query: '${display_query}'`)

// —–– Record user message
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(query))
}

// Prepare history (WITHOUT adding current message yet)
const historyToUse = externalHistory ?? this.conversationHistory
const langchainHistory: BaseMessage[] = []
for (const msg of historyToUse) {
Expand Down Expand Up @@ -669,6 +665,14 @@ export class MCPAgent {
logger.info('🎉 Agent execution complete')
success = true

// Add BOTH the user message and AI response to conversation history if memory is enabled
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(query))
if (result) {
this.addToHistory(new AIMessage(result as string))
}
}

// Return regular result
return result as string | T
}
Expand Down Expand Up @@ -802,13 +806,7 @@ export class MCPAgent {
= query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, ' ')}...` : query.replace(/\n/g, ' ')
logger.info(`💬 Received query for streamEvents: '${display_query}'`)

// Add user message to history if memory enabled
if (this.memoryEnabled) {
logger.info(`🔄 Adding user message to history: ${query}`)
this.addToHistory(new HumanMessage(query))
}

// Prepare history
// Prepare history (WITHOUT adding current message yet)
const historyToUse = externalHistory ?? this.conversationHistory
const langchainHistory: BaseMessage[] = []
for (const msg of historyToUse) {
Expand Down Expand Up @@ -859,9 +857,12 @@ export class MCPAgent {
}
}

// Add the final AI response to conversation history if memory is enabled
if (this.memoryEnabled && finalResponse) {
this.addToHistory(new AIMessage(finalResponse))
// Add BOTH the user message and AI response to conversation history if memory is enabled
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(query))
if (finalResponse) {
this.addToHistory(new AIMessage(finalResponse))
}
}

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