Skip to main content
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 exposes synap_search and synap_remember as explicit tools the model can call.
Runtime: Python 3.11 or later, or Node.js 20+ for TypeScript. The gRPC stream needs a long-lived process; where it cannot open, turns go over REST.

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 the user’s prompt, the agent’s reply and every tool call back to Synap so memory grows with use
  • Give Claude Code Synap’s compaction of the conversation when its own transcript lacks it
  • Expose explicit search/store tools the model can call mid-conversation when it needs to
The integration is available in both Python and TypeScript. The names differ by language (snake_case in Python, camelCase in TypeScript):
The TypeScript package builds on the JavaScript SDK and needs Node.js 20+. See Installation → JavaScript and TypeScript SDK.

Setup

In Python the pip package is maximem-synap-claude-agent, but the import drops the maximem- prefix and uses underscores: from synap_claude_agent import .... In TypeScript the import name matches the npm package: import { createSynapHooks } from "@maximem/synap-claude-agent".
Configure your API key. Generate one from the Synap Dashboard.
.env
Initialize the SDK once at application startup:
See SDK Initialization for the full lifecycle and configuration options.

Basic integration

The smallest useful integration uses hooks: memory injection and turn recording happen automatically, with no changes to the model’s tool surface:
The hooks use Claude Code’s own hook events. On each prompt, Synap context is added to it as additionalContext, and the prompt is recorded as a turn. When the agent finishes, its reply is recorded. A failed context fetch degrades gracefully: no context is added and the error is logged. A failed write is logged and never raised, so Synap being unreachable cannot stop the agent. 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:
The model never sees the hook plumbing: there are no tools added, no schemas to learn. This is the right primitive for production agents where memory should be omnipresent. Options besides the identity: mode ("accurate" default, or "fast"), max_results / maxResults (default 20), context_preamble / contextPreamble (a format string with one {body} placeholder), and record_user_prompts / recordUserPrompts (default on). Turning the last one off stops prompts being recorded; the agent’s reply is still recorded. A tool result is usually your customer’s data. Tool calls and results go on the stream for anticipation and never become long-term memories, but they do leave your process.
The reply comes from the Stop hook’s last_assistant_message, which Claude Code sends on versions that carry the field. On a version that does not, the reply is not recorded; in Python, call report_assistant_turn(sdk, content, conversation_id=..., user_id=...) once your run returns.

The gRPC stream

The first hook that reports opens the gRPC stream for you; you do not call listen(). Once it is open, the prompt, every tool call and result, the reply and the end of the session go on it, and anticipated context comes back into the SDK’s cache, so the next turn’s fetch can be answered without a cold retrieval. The open waits up to 10 seconds. If it fails, the user and assistant turns are written over REST with conversation.record_message, never both. Tool calls and results exist only on the stream, and the stream is tried once more a few seconds later, then no more than once a minute. The hooks never raise. A stream your own code opened with sdk.instance.listen() is used as is. To keep the integration on REST, set SYNAP_AUTO_LISTEN=false, or turn it off for one SDK:
In Python this needs maximem-synap-integrations-common 0.2.3 or later, which the package installs. In TypeScript an open stream does not keep a Node.js script alive: the process exits when its work is done. This applies only to the connection to Synap, at the address in SYNAP_GRPC_HOST and SYNAP_GRPC_PORT or the default.

Short-term history

Claude Code keeps its own transcript, and a hook can add context to a prompt but cannot remove what the transcript already holds. So the hooks cannot replace the history of a running session. What they do:
  • Each prompt: Synap’s compaction block is added to the prompt with the long-term context. Python adds it only when Claude Code’s transcript lacks it, and when the transcript has no earlier turns (a fresh query() per turn) it adds Synap’s whole short-term context, recent turns included. TypeScript adds the block once per compaction, so the transcript does not grow by the same block each turn.
  • When Claude Code compacts (Python): the PreCompact hook passes Synap’s compaction to Claude Code’s summarizer, so its summary builds on it. In TypeScript the SessionStart hook adds Synap’s compaction again right after Claude Code compacts.
  • When you resume a session (Python): pass create_synap_session_store as session_store and the resumed session starts from Synap’s compaction, followed by the turns after it, instead of the stored transcript. Until Synap has compacted the conversation, or if it cannot answer, the transcript resumes as it would without the store.
The store mirrors the raw transcript in memory by default and loses it on exit; pass inner= a durable SessionStore to keep it. By default the Synap conversation is the Claude Code session id, as with the hooks; pass conversation_id= to the store if you gave one to the hooks. The TypeScript package has no session store.

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 query
  • synap_remember: store a new memory
The tools reach the model as mcp__synap__synap_search and mcp__synap__synap_remember (the synap is the server name, which you can change with name). If your permission mode asks before running tools, add them to allowed_tools (Python) or allowedTools (TypeScript). The MCP server can be used alone, or alongside hooks for layered memory (automatic context plus on-demand search/store).

Raw tool definitions (TypeScript)

For TypeScript users who want to compose tools manually rather than going through MCP, buildSynapTools returns the Claude Agent SDK’s tool definitions for synap_search and synap_remember:
Add other tools to the list and wrap them in your own MCP server with the Claude Agent SDK’s createSdkMcpServer. The Python integration uses MCP exclusively for explicit tooling, so this export is TypeScript-only.

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:
Three things to notice in this pattern:
  1. Hooks and MCP server cover different needs. Hooks are silent and always-on; MCP tools are explicit and model-driven.
  2. They compose. Use both: the model sees relevant context every turn AND can query for more when needed.
  3. Scope is per-request. Each handle_query invocation creates its own hooks and MCP server scoped to the right user.

Advanced patterns

Hooks vs. MCP server

Use both together for maximum coverage: hooks handle the always-on path, MCP tools handle the explicit path.

Multi-tenant scoping

The hooks and the MCP server accept the standard scoping triple: user_id (required), customer_id (B2B instances only), optional conversation_id. customer_id is required on B2B Synap instances and not accepted on B2C (single-tenant) ones, where a call carrying it is rejected with HTTP 400. See Memory Scopes.
For multi-tenant services, construct hooks/MCP per request rather than caching them globally.

Failure semantics

The integration follows the Synap-wide contract:
  • The hooks never raise: a failed context fetch adds no context and logs; a failed turn write is logged and the run goes on.
  • synap_search (MCP) degrades gracefully: a failure returns a “no context available” result and logs, so the agent loop keeps going.
  • synap_remember (MCP) surfaces failures: a failed write comes back to the model as an error tool result.
Read failures shouldn’t break a user-facing turn. An explicit store is different: the model asked for it, so a failure is reported back to it.

Going further


Next steps

Vercel AI SDK

Middleware for any Vercel AI SDK model.

Mastra

SynapMemory for Mastra.

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.