Give Your AI Agents Memory

MCP Full Course Lesson 7 - MCP shared memory for agents

Table of Contents

This lesson covers adding persistent memory to MCP-based AI agents using three tools, save_memory, read_memory, and search_memory, backed by a shared JSON file. It lets a Researcher and Writer agent pair store insights, recall past learnings, and build on previous work across sessions instead of starting from zero each time.

What you will be able to do

  • Build three MCP memory tools: save_memory, read_memory, and search_memory
  • Set up a shared JSON file (shared_memory.json) that stores timestamped memory entries both agents can read and append to
  • Have agents check memory before starting a task and save new learnings after finishing it
  • Share memory across multiple agents so a Researcher and a Writer learn from each other's notes
  • Explain the difference between a context window and persistent memory when designing agent systems

Before you start

  • A working multi-agent MCP system with a Researcher and Writer agent sharing an MCP server, built in the previous lesson
  • Basic understanding of how MCP tools are defined and called by agents
  • A place to store a shared file that both agents can read from and write to

Reference

Tool/Element Input Purpose When to use
save_memory Memory content (text describing what was learned) Saves content to shared_memory.json with a timestamp After completing a task, to store new insights
read_memory None Returns all saved memories Before starting a new task, to check past learnings
search_memory Search term (e.g. "writing style") Returns memories matching that term When an agent needs focused context on one topic instead of everything
shared_memory.json N/A Single file storing all memory entries; both agents read from and append to it Acts as the shared filing cabinet for the whole agent team
Memory record timestamp, agent, content JSON structure of each saved entry Written automatically every time save_memory is called

Common errors and fixes

What goes wrong The fix
Assuming a large context window means the AI remembers past sessions The context window only covers the current conversation; build a separate memory layer for anything that needs to survive between sessions
Agents re-research topics and repeat past mistakes every session Call read_memory before starting work so agents build on what was already learned
Agents finish a task without recording what they learned Call save_memory after completing significant work so learnings compound over time
Reading the entire memory file overwhelms the agent with unrelated information Use search_memory with a specific term to get focused results instead of reading everything

Read the full walkthrough

The complete lesson, with screenshots and any downloads, is published on Substack as part of MCP Masterclass: Connect AI to Everything.

Read Lesson 7 on Substack →

More in this section

Continue the course

Browse all lessons in the MCP Masterclass: Connect AI to Everything course, or subscribe to the GenAI Unplugged newsletter to get new lessons in your inbox.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Read the full lesson on Substack