Add persistent memory to an Anthropic Claude Agent in two ways: hooks for zero-friction automatic memory (the model never sees the plumbing), and an MCP server that exposesDocumentation Index
Fetch the complete documentation index at: https://docs.maximem.ai/llms.txt
Use this file to discover all available pages before exploring further.
synap_search and synap_remember as explicit tools the model can call.
Overview
This guide shows how to add Synap to a Claude Agent SDK application to build agents that:- Inject relevant memories before every turn — automatically, with no tool calls
- Record every completed turn back to Synap so memory grows with use
- Expose explicit search/store tools the model can call mid-conversation when it needs to
| Export | Language | Purpose |
|---|---|---|
create_synap_hooks | Python + TypeScript | Hooks for automatic context injection and turn recording |
create_synap_mcp_server | Python + TypeScript | MCP server exposing synap_search and synap_remember as tools |
build_synap_tools | TypeScript only | Raw tool definitions for manual composition |
Setup
.env
Basic integration
The smallest useful integration uses hooks — memory injection and turn recording happen automatically, with no changes to the model’s tool surface:before_query fetches Synap context and prepends it as a system message, and after_turn ingests the completed user/assistant exchange. Context fetch failures degrade gracefully — empty context is injected and the error is logged. Turn ingestion failures surface explicitly so silent data loss is impossible.
For explicit memory control (the model decides when to search or store), layer in the MCP server — see below.
Core concepts
Hooks — automatic memory
create_synap_hooks returns a dict of hook callbacks that the Claude Agent SDK invokes around each turn:
| Hook | Behavior |
|---|---|
before_query | Fetches Synap context for the incoming prompt and prepends it as a system message |
after_turn | Ingests the full user + assistant turn back into Synap |
MCP server — explicit memory tools
When you want the model to decide when to query or store memories, register the Synap MCP server. It exposes two tools:synap_search— search memories by natural-language querysynap_remember— store a new memory
Raw tool definitions (TypeScript)
For TypeScript users who want to compose tools manually rather than going through MCP,buildSynapTools returns raw Anthropic tool definitions:
Complete example: agent with hooks + MCP server
The pattern below combines both primitives. Hooks provide automatic memory on every turn, and the MCP server gives the model the option to dig deeper when it decides recall is needed:- Hooks and MCP server cover different needs. Hooks are silent and always-on; MCP tools are explicit and model-driven.
- They compose. Use both — the model sees relevant context every turn AND can query for more when needed.
- Scope is per-request. Each
handle_queryinvocation creates its own hooks and MCP server scoped to the right user.
Advanced patterns
Hooks vs. MCP server
| Hooks | MCP server | |
|---|---|---|
| Context injection | Automatic, every turn | On-demand via tool call |
| Memory storage | Automatic, every turn | On-demand via tool call |
| Model awareness | Model doesn’t see the tools | Model can decide when to search/store |
| Best for | Production agents where memory is always needed | Research agents where explicit memory control matters |
Multi-tenant scoping
All three exports accept the standard scoping triple —user_id (required), optional customer_id, optional conversation_id. customer_id is required on B2B Synap instances and ignored on single-tenant ones. See Memory Scopes.
Failure semantics
The integration follows the Synap-wide contract:before_query(hooks) degrades gracefully — empty context on failure, error logged.after_turn(hooks) surfaces failures — turn ingestion raisesSynapIntegrationError.synap_search(MCP) degrades gracefully — returns[]on failure.synap_remember(MCP) surfaces failures — raisesSynapIntegrationError.
Next steps
Vercel AI SDK
Middleware for any Vercel AI SDK model.
Mastra
SynapMemory for the Mastra ADK.Context Fetch
The retrieval API behind hooks and
synap_search — modes, scopes, and response shapes.Memory Scopes
How
user_id, customer_id, and conversation_id interact across reads.