Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

fix: populate tool_calls in conversation history for streamEvents - #98

Open
clawtom wants to merge 1 commit into
mcp-use:mainfrom
clawtom:fix/conversation-history-tool-calls
Open

fix: populate tool_calls in conversation history for streamEvents#98
clawtom wants to merge 1 commit into
mcp-use:mainfrom
clawtom:fix/conversation-history-tool-calls

Conversation

@clawtom

@clawtom clawtom commented Mar 9, 2026

Copy link
Copy Markdown

Problem

When using streamEvents() with memory enabled, getConversationHistory() returns AIMessage objects with an empty tool_calls array, even when the agent used tools during execution (fixes #8).

The issue is in how conversation history is saved at the end of streamEvents():

// Before — tool calls are discarded
this.addToHistory(new AIMessage(finalResponse))

The AIMessage receives only the text response. Tool call data is available throughout execution but never propagated to the history entry.

Fix

Track tool calls from on_tool_start events as the agent runs. These events are emitted by LangChain for every tool invocation and provide the tool name, input args, and run ID in a provider-agnostic way (works with ChatBedrockConverse, ChatOpenAI, ChatAnthropic, etc.).

When saving to history, pass the accumulated tool calls to the AIMessage constructor:

// After — tool calls are preserved
this.addToHistory(new AIMessage({
  content: finalResponse,
  tool_calls: capturedToolCalls.length > 0 ? capturedToolCalls : undefined,
}))

Changes

  • : 18 lines changed
    • Add array before the event loop
    • Capture events to accumulate tool call data
    • Pass to when saving final response to history

When streamEvents() saves the final AI response to conversation history,
it created an AIMessage with only the text content. Any tool calls made
during execution were discarded, causing getConversationHistory() to
return AIMessages with an empty tool_calls array.

Fix: track tool calls from on_tool_start events as the agent runs, then
pass them to the AIMessage constructor when saving to history. The
on_tool_start event provides the tool name, input args, and run_id
(used as the call id) in a provider-agnostic way.

Fixes mcp-use#8

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Conversation History has no tool calls

1 participant