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
snake_case in Python, camelCase in TypeScript):
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"..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: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:
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 calllisten(). 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:
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
PreCompacthook passes Synap’s compaction to Claude Code’s summarizer, so its summary builds on it. In TypeScript theSessionStarthook adds Synap’s compaction again right after Claude Code compacts. - When you resume a session (Python): pass
create_synap_session_storeassession_storeand 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.
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 querysynap_remember: store a new memory
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:
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:- 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
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.
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.
Going further
- Patterns overview: reusable memory patterns across frameworks.
- Cookbook overview: end-to-end worked examples.
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.