Feat/tool history preservation #40
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This implements comprehensive tool call history preservation for MCP agents, fixing a critical gap where tool execution information was lost between conversation turns. The root issue was that
AIMessage
objects were created with only text content, completely ignoring LangChain'stool_calls
field and discarding allintermediateSteps
data containing tool executions.This was breaking stateful workflows - LLMs couldn't reference previous tool calls, leading to duplicate entity creation and workflow interruptions. For example, mentioning additional details about an injury would trigger creation of new assessments instead of continuing with existing ones.
The fix preserves complete tool call history in both
stream()
andstreamEvents()
methods using standard LangChain message patterns thatcreateToolCallingAgent
expects. Added comprehensive error handling, configurable output truncation, and memory safety measures for production use.Depends on #39 (conversation memory duplication fix) as it builds upon those memory management improvements.
Type of change
Checklist
The only unchecked item is documentation since this is an internal fix that doesn't change the public API - the improvement is transparent to users but enables the LLM orchestration to work properly.