Skip to main content
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.
After two successful calls, history normally contains four messages: one user and one assistant message for each turn.

Know what is stored

Conversation history uses the provider-neutral ProviderMessage 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() and stream() 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 messages and externalHistory affect 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

Set memoryEnabled: false when every call must start with only the system prompt, per-call messages, and current prompt.
With memory disabled, the second call does not receive the first turn from agent memory.

Persist history outside the agent

History lives only on the MCPAgent 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:
Passing 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.

Next steps