Files
mcp-use__mcp-use/docs/python/agent/memory-management.mdx
Pietro Zullo 8348c59a53 docs: reorganize, fix accuracy, and update for agent readability (#1502)
* docs: reorganize navigation and clean up content

- Remove 'Misc' group with misplaced blog posts
- Remove 'Libraries' redirect group from Home nav
- Simplify 'Dev Tools' group (flatten Inspector, CLI, Tunneling)
- Remove individual changelog versions from Python nav (keep summary)
- Remove changelog from TypeScript 'Get Started' group
- Remove tunneling duplicate from Python Development group
- Rewrite mcp101 as clean MCP concepts page (was pitch deck)
- Delete typescript/blog posts (not technical docs)
- Add redirects for deleted blog post URLs

* docs: fix content accuracy and remove sloppy language

- Fix broken python/server/mcp-apps link in home page
- Remove informal pitch-speak from python/server/index.mdx
- Fix grammar error in typescript/server/index.mdx
- Update typescript/client/logging.mdx: remove fake 'coming soon' API code
- Fix non-existent model name gpt-5.1 in typescript quickstart
- Fix mcp_use naming in development contributing guide

* docs: fix incorrect TypeScript server API calls in inspector docs

- Replace server.addTool() with server.tool() (correct mcp-use API)
- Add missing zod import to debugging-chatgpt-apps example

* docs: fix fake model names (gpt-5.1 → gpt-4o) in agent docs

* docs: update model names, fix APIs, and add agent visibility hints

- Replace all deprecated model names with latest: gpt-4.1, gpt-4.1-mini,
  claude-sonnet-4-6, claude-opus-4-7, gemini-2.5-flash across all docs
- Fix TypeScript server.tool() API in installation.mdx (old positional string form)
- Fix TypeScript agent.run() in quickstart.mdx (deprecated string form → object)
- Remove broken manufact.com/docs/llms.txt hidden hints from 5 pages
- Add <Visibility for="agents"> block to mcp101.mdx with install commands,
  key classes, and API signatures for agent-oriented navigation
- Add LangChain provider install note to Python quickstart (mcp-use agent
  requires a separate langchain-* provider package)
- Update model name cards in Python llm-integration.mdx

* docs: clean up remaining stale model names and improve accuracy

- Update supported model lists in Python and TypeScript llm-integration pages
- Fix cost optimization table to use current model IDs
- Update TypeScript agent overview supported providers list

* docs: add LangChain provider install to TypeScript quickstart agent section

The MCPAgent requires a separate @langchain/* provider package; this was
not mentioned before the code example that imports from @langchain/openai.

* docs: update to latest flagship model IDs (May 2026)

- OpenAI: gpt-4.1 → gpt-5.5, gpt-4.1-mini → gpt-5.5-mini
- Google: gemini-2.5-flash → gemini-3.1-flash, gemini-2.5-pro → gemini-3.1-pro
- Groq: llama-3.1-* → llama-4-scout-17b-16e-instruct
- Anthropic (claude-opus-4-7, claude-sonnet-4-6) already current, no change

* docs: replace all em dashes with hyphens

* docs: restore llms.txt hints using Visibility component

* docs: simplify TypeScript logging page to unsupported notice only

* docs: rewrite mcp101 to explain MCP protocol with mcp-use examples and doc links

* docs: remove goal.md

* docs: revert nav changes, keep only Misc group removal and blog redirects

* docs: reorder mcp101 to Server/App -> Client/Inspector -> Agent, restore title

* docs: add code examples for Resources and Prompts in mcp101

* docs: remove em dashes from mcp101

* docs: add MCPClient code examples to mcp101

* docs: add React client example to mcp101

* docs: add agent code examples to mcp101

* docs: expand MCP Apps section in mcp101 with example and links

* docs: restore Contacts, Socials, and Founders sections to mcp101

* docs: add context-setting intro paragraph to mcp101 for new readers

* docs: add MCP stores section to mcp101 intro with Claude, ChatGPT, Cursor

* docs: fix Claude store link

* docs: fix ChatGPT store link

* docs: fix Cursor store link

* docs: replace store Cards with linked image grid

* docs: add captions to store images

* docs: add Notifications to client primitives table in mcp101

* docs: add lead-in paragraph before resource cards at bottom of mcp101

* docs: move resource cards to end of page after Contacts/Socials/Founders

* docs: demote Contacts/Socials/Founders to bold text to remove from TOC

* docs: move Contacts/Socials/Founders to very end of mcp101

* docs: add Resources and Contacts section headers, link official MCP docs

* chore(docs): update site metadata, footer links, and rename mcp_use to mcp-use

* update docs metadata + images

* docs: enhance MCP section by highlighting the reach of MCP stores with 1 billion weekly active users

* api ref
2026-05-14 18:49:06 +02:00

136 lines
4.7 KiB
Plaintext

---
title: Memory Management
description: Learn how to manage conversation memory in MCPAgent with different memory modes
icon: brain
---
MCPAgent provides flexible memory management options to control how conversation history is handled. You can choose between three different memory modes depending on your use case.
## Memory Modes
### 1. Self-Managed Memory (`memory_enabled=True`)
When `memory_enabled=True`, the agent automatically manages conversation history internally. This is the simplest option for most use cases.
```python
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI
client = MCPClient(config=config)
llm = ChatOpenAI(model="gpt-5.5-mini")
agent = MCPAgent(
llm=llm,
client=client,
memory_enabled=True # Agent manages memory internally
)
# The agent will automatically maintain conversation context
response = await agent.run("Hello, my name is Alice")
response = await agent.run("What's my name?") # Agent remembers Alice
```
### 2. No Memory (`memory_enabled=False`)
When `memory_enabled=False`, the agent has no internal memory and treats each interaction independently.
```python
agent = MCPAgent(
llm=llm,
client=client,
memory_enabled=False # No internal memory
)
# Each interaction is independent
response = await agent.run("Hello, my name is Alice")
response = await agent.run("What's my name?") # Agent doesn't remember Alice
```
### 3. External Memory Management
You can provide conversation history externally for full control over memory management. This allows you to implement custom memory strategies like limited history, persistence, or filtering.
```python
from langchain_core.messages import HumanMessage, AIMessage
agent = MCPAgent(
llm=llm,
client=client,
memory_enabled=True # Can be True or False
)
# External history management
external_history = []
# First interaction
response1 = await agent.run("Hello, my name is Alice", external_history=external_history)
external_history.append(HumanMessage(content="Hello, my name is Alice"))
external_history.append(AIMessage(content=response1))
# Second interaction with limited history
limited_history = external_history[-4:] # Keep only last 4 messages
response2 = await agent.run("What's my name?", external_history=limited_history)
external_history.append(HumanMessage(content="What's my name?"))
external_history.append(AIMessage(content=response2))
```
## Memory Strategies
### Unlimited History
Keep all conversation history for maximum context:
```python
# Use all external history
response = await agent.run(user_input, external_history=external_history)
```
### Limited History
Limit conversation history to manage memory usage and context length:
```python
# Keep only last N messages
MAX_HISTORY_MESSAGES = 5
limited_history = external_history[-MAX_HISTORY_MESSAGES:] if external_history else []
response = await agent.run(user_input, external_history=limited_history)
```
### Sliding Window
Implement a sliding window approach to maintain recent context while discarding older messages:
```python
def get_sliding_window_history(history, window_size=10):
"""Return the last window_size messages from history"""
return history[-window_size:] if history else []
# Usage
sliding_history = get_sliding_window_history(external_history, window_size=10)
response = await agent.run(user_input, external_history=sliding_history)
```
## Best Practices
1. **For Simple Use Cases**: Use `memory_enabled=True` for automatic memory management
2. **For Stateless Operations**: Use `memory_enabled=False` when each interaction should be independent
3. **For Custom Control**: Use external memory management when you need specific memory strategies
4. **Memory Limits**: Consider implementing memory limits to prevent context overflow with large conversations
5. **Persistence**: For long-running applications, consider persisting conversation history to external storage
## Examples
See the following examples for practical implementations:
- [`chat_example.py`](https://github.com/mcp-use/mcp-use/tree/main/examples/chat_example.py) - Unlimited memory with external management
- [`limited_memory_chat.py`](https://github.com/mcp-use/mcp-use/tree/main/examples/limited_memory_chat.py) - Limited memory with sliding window
## Configuration Options
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `memory_enabled` | bool | `True` | Enable/disable internal memory management |
| `external_history` | List[BaseMessage] | `None` | External conversation history to use |
The `external_history` parameter takes precedence over internal memory when provided, allowing you to override the agent's internal memory with your own implementation.