MCPAgent keeps conversation history in memory by default. Successful run() and stream() calls store the user prompt and final assistant text as ProviderMessage[].
Keep conversation context
This example completes two turns, inspects the stored messages, and then clears them.Know what is stored
Conversation history uses the provider-neutralProviderMessage shape. Each message has a role and content; provider messages can also represent tool calls and results.
The agent’s automatic memory is narrower:
run()andstream()append the prompt and final assistant text after a successful call.- Tool calls and tool results are not appended to automatic memory.
streamEvents()does not append any conversation history.- Per-call
messagesandexternalHistoryaffect that call but are not copied into stored history. - A failed or aborted call does not append the prompt as a completed turn.
getConversationHistory() returns a new array. Changing that array does not replace agent memory, although message objects inside it are not deep-cloned.
Disable memory
SetmemoryEnabled: false when every call must start with only the system prompt, per-call messages, and current prompt.
Persist history outside the agent
History lives only on theMCPAgent instance. It is not written to disk or restored after a process restart.
Store getConversationHistory() in your application if you need durable conversations. Pass restored ProviderMessage values through messages for a later call:
messages provides context for that call. It does not seed anotherAgent’s internal history.
Clear a conversation
clearConversationHistory() removes all stored conversation messages immediately. The system prompt is held separately, so clearing history does not change it.
Closing an agent does not clear its in-memory history. If you initialize the same instance again, its stored conversation remains until you clear it or discard the instance.