November 5, 2026

Pydantic AI Memory: Persistent Project Knowledge with MCP

Build Pydantic AI memory with clear storage boundaries, a working OAuth MCP setup, and a practical correction and cross-client test.

Pydantic AI memory is not one feature. A production agent may need to continue a conversation, retain an agent-specific notebook, and share reviewed project knowledge with other tools. Treating all three as the same store creates stale facts, cross-user leaks, and histories that are difficult to correct.

Pydantic AI now has strong primitives for the first two jobs. Its persistence APIs preserve conversation state, while Pydantic AI Harness provides a namespaced Markdown notebook that an agent can update and search. A third layer is still useful when knowledge must outlive one agent runtime and remain readable to people, Claude Code, Cursor, Codex, or another MCP client.

Separate the three memory jobs

JobGood Pydantic AI mechanismAuthority rule
Continue one conversationMessage history, conversation store, or step persistenceThe conversation owns what was said, not the current project policy.
Give one agent a notebookPydantic AI Harness MemoryThe notebook owns agent-local working knowledge within its namespace.
Share project truthA readable wiki exposed through MCPReviewed pages own current decisions, procedures, and source notes.

This boundary matters more than the database. If a user corrects a deployment port, replaying old message history should not override the new runbook. If an agent writes a tentative observation to its notebook, another agent should not treat it as approved policy. The broader AI agent memory architecture explains these lifecycle rules; here we will implement the Pydantic-specific path.

What the current Pydantic AI options cover

The official persistence guide distinguishes conversation continuity from long-term knowledge. You can serialize message history yourself, use a conversation store, checkpoint settled tool-call boundaries, or delegate state to a model provider. These mechanisms are appropriate when the next request must continue the same interaction.

Pydantic AI Harness memory is different. It gives an agent a persistent, namespaced notebook made of Markdown files, with bounded prompt injection and on-demand search. That is useful for preferences, observations, and working procedures local to that agent. The Harness API is still on 0.x releases, so pin a version and follow its migration guidance.

Neither choice has to exclude semantic retrieval. A vector store can rank similar items at scale, and products such as Hindsight can add extraction and recall. The trade-off is authority: an index is valuable for discovery, but a readable canonical page is easier to diff, correct, review, and restore. This is the same source-versus-index distinction covered in vector memory versus readable memory.

Connect Pydantic AI to readable MCP memory

Pydantic AI can connect to remote Streamable HTTP MCP servers through MCPToolset. Its current client accepts auth="oauth", which uses the FastMCP OAuth 2.1 flow. The first connection opens a browser for authorization. With Meshnote, you choose the projects the client may access and whether the connection is read-only or can write.

Install the OpenAI and MCP extras in a clean project:

uv init pydantic-memory-demo
cd pydantic-memory-demo
uv add "pydantic-ai-slim[openai,mcp]"
export OPENAI_API_KEY="your-key"

Create agent.py:

import asyncio

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPToolset

project_memory = MCPToolset(
    "https://meshnote.io/mcp",
    auth="oauth",
)

agent = Agent(
    "openai:gpt-5.2",
    instructions=(
        "Use the Meshnote project named engineering as shared project memory. "
        "Search before answering project-specific questions. "
        "Do not write tentative conclusions as facts. "
        "When correcting a page, preserve the source and explain the change."
    ),
    toolsets=[project_memory],
)

async def main() -> None:
    async with agent:
        result = await agent.run(
            "Read the current deployment policy from project memory, "
            "include its source page, and list any unresolved questions."
        )
    print(result.output)

if __name__ == "__main__":
    asyncio.run(main())

Run it with uv run agent.py. On first use, complete the browser consent flow. A production service should not depend on interactive login at every restart: FastMCP's default OAuth token storage is in memory, so configure encrypted persistent token storage or provision a scoped Meshnote API key through your normal secret system. Never hard-code a bearer token in the script or commit it to the repository.

The client code only grants tools. The instructions define behavior, not security. Enforce project scope and read/write rights at the MCP server, and use a separate MCPToolset per user when credentials differ. Pydantic's documentation warns that one shared toolset represents one identity across concurrent runs.

Use an explicit promotion rule

Do not copy every chat turn into long-term memory. Promote an item to shared project memory only when it is useful after the current task, belongs to a named project, has a source, and can be corrected later. Good examples are an accepted architecture decision, a verified service endpoint, or a runbook step. Tool output, guesses, and temporary plans should remain in conversation state or expire from the agent notebook.

A simple write contract is:

  1. Search for an existing owner page before creating one.
  2. Write the current fact with its source and scope.
  3. Link related decisions instead of duplicating them.
  4. On correction, update the owner page and leave history available.
  5. Delete or archive derived notes that should no longer be retrieved.

Meshnote exposes search, pages, sources, backlinks, and version history as MCP tools while keeping the underlying wiki readable. The hosted path is Start syncing - $8/mo. Teams that need their own infrastructure can use self-hosting from $10/seat/month with a five-seat minimum.

Run a two-session authority test

A successful connection is not proof that memory is correct. Create a disposable project and test the full lifecycle:

  1. Write: store “API retries are capped at two” with a link to a test source.
  2. Restart: end the Python process, start it again, and ask for the cap and source.
  3. Correct: change the cap to three and state why. Ask again without repeating the new value.
  4. Inspect history: verify that three is current while two remains recoverable as an earlier version.
  5. Cross-client: read the same page from another authorized MCP client. It should see three without replaying the Pydantic conversation.
  6. Scope: create another project with a cap of five. Neither query may leak into the other project.

This test adds what most integration tutorials omit: correction, provenance, history, and cross-client agreement. If the old value wins, your retrieval layer is outranking the source. If the second client cannot see the update, you built agent-local memory rather than shared project knowledge. If projects leak, stop and fix authorization before shipping.

Choose the smallest architecture that stays correct

Use conversation persistence when you only need to continue a thread. Add Harness memory when one Pydantic agent needs a durable notebook. Add a vector layer when semantic recall over a large corpus matters. Add readable MCP project memory when multiple agents and people need a shared source of truth with review, correction, and recovery.

The layers can work together. Keep canonical decisions in files, index them for recall, and let conversation history hold the immediate task. That makes each component replaceable and gives your team a clear answer when two memories disagree.

Related reading

Your agent's memory should be files you can read and own

Meshnote is readable, self-hosted memory for AI agents — markdown wikis your agents maintain over MCP. Hosted from $8/month.

Start syncing — $8/mo

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.