How to Integrate agentmemory with Claude Code's MEMORY.md Bidirectional Bridge
The memory_claude_bridge_sync MCP tool enables bidirectional synchronization between Claude Code's MEMORY.md file and agentmemory's persistent store, handling JSONL conversion for seamless knowledge transfer in both directions.
The rohitg00/agentmemory repository ships with a dedicated bridge that allows you to integrate agentmemory with Claude Code's MEMORY.md bidirectional bridge using the Model Context Protocol (MCP). This integration treats Claude's memory file as a JSONL transcript that can be imported into agentmemory's vector database or exported back to update Claude's local context.
How the Bidirectional Bridge Works
The bridge operates through two distinct data flows defined in the MCP tool registry. According to the rohitg00/agentmemory source code, the tool memory_claude_bridge_sync is declared in [src/mcp/tools-registry.ts](https://github.com/rohitg00/agentmemory/blob/main/src/mcp/tools-registry.ts) with a JSON schema that accepts a direction parameter ("import" or "export") and an optional filePath.
When invoked, the handler in [src/mcp/server.ts](https://github.com/rohitg00/agentmemory/blob/main/src/mcp/server.ts) routes requests to the appropriate pipeline:
- Import (Claude → agentmemory): Parses the
MEMORY.mdfile into JSONL observations and stores them via themem::rememberfunction. - Export (agentmemory → Claude): Calls
mem::exportAllfrom [src/functions/export-import.ts](https://github.com/rohitg00/agentmemory/blob/main/src/functions/export-import.ts) to stream the entire memory graph as a JSONL payload.
Installation and MCP Server Setup
To enable the bridge, you must install the MCP server and register the tool with Claude Code.
-
Install the MCP server globally.
npm i -g @agentmemory/mcp npx @agentmemory/mcp -
Register the tool in your Claude Code plugin.
Edit your project-specific plugin configuration at
~/.claude/projects/your-project/.claude-plugin/plugin.jsonand add"memory_claude_bridge_sync"to the tools array:{ "tools": [ "memory_claude_bridge_sync" ] }Alternatively, set the environment variable
AGENTMEMORY_TOOLS=allto expose every core tool without explicit listing.
Configuring Bridge Behavior
Fine-tune the synchronization behavior using environment variables before starting the MCP server:
AGENTMEMORY_ALLOW_AGENT_SDK=true: Enables a Claude SDK fallback for handling large imports that exceed standard MCP payload limits.AGENTMEMORY_INJECT_CONTEXT=true: Controls whether session-start context is injected into Claude's prompt. Set tofalse(default) for pure bridge usage without additional context injection.
Implementation Examples
Importing MEMORY.md into agentmemory
To push Claude's local memory into agentmemory, send an HTTP POST request to the MCP server endpoint with direction: "import":
// Triggered from a Claude Code hook or external script
await fetch("http://localhost:49134/mcp/tools/memory_claude_bridge_sync", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
direction: "import",
filePath: "/home/user/.claude/projects/my-project/MEMORY.md"
})
});
This reads the specified MEMORY.md file, converts each line to a JSONL observation, and persists it through the mem::remember storage function.
Exporting agentmemory to MEMORY.md
To update Claude's local file with the current state of agentmemory, use the export direction and write the response to disk:
import fetch from "node-fetch";
import fs from "fs";
async function exportToMemoryMd() {
const res = await fetch(
"http://localhost:49134/mcp/tools/memory_claude_bridge_sync",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ direction: "export" })
}
);
if (!res.ok) throw new Error(`Bridge error: ${res.statusText}`);
const jsonl = await res.text();
fs.writeFileSync(
"/home/user/.claude/projects/my-project/MEMORY.md",
jsonl
);
}
exportToMemoryMd().catch(console.error);
This retrieves the full memory graph as a JSONL stream and overwrites the target MEMORY.md file, ensuring Claude Code loads the latest persistent context on its next initialization.
Using the CLI Shortcut
For manual imports without writing custom scripts, use the built-in CLI command which internally invokes the same MCP tool:
npx @agentmemory/agentmemory import-jsonl \
~/.claude/projects/my-project/MEMORY.md
Key Source Files
Understanding the implementation requires familiarity with these specific files in the rohitg00/agentmemory repository:
- [
src/mcp/tools-registry.ts](https://github.com/rohitg00/agentmemory/blob/main/src/mcp/tools-registry.ts): Defines thememory_claude_bridge_synctool name, description, and input schema. - [
src/mcp/server.ts](https://github.com/rohitg00/agentmemory/blob/main/src/mcp/server.ts): Contains the request router that handles thememory_claude_bridge_synccase and delegates to import or export logic. - [
src/functions/export-import.ts](https://github.com/rohitg00/agentmemory/blob/main/src/functions/export-import.ts): Implements the JSONL serialization for exports and the parsing logic for imports. - [
plugin/.claude-plugin/plugin.json](https://github.com/rohitg00/agentmemory/blob/main/plugin/.claude-plugin/plugin.json): Advertises available tools to Claude Code.
Summary
- The
memory_claude_bridge_synctool provides the official integration point between agentmemory and Claude Code's MEMORY.md file. - Bidirectional sync supports importing CLI chat history into agentmemory and exporting the vector database back to JSONL format.
- MCP architecture allows invocation from Claude hooks, custom scripts, or the provided CLI wrapper.
- Key files include the tools registry for schema definition and
export-import.tsfor data transformation logic.
Frequently Asked Questions
What is the memory_claude_bridge_sync tool?
The memory_claude_bridge_sync tool is a core MCP tool registered in src/mcp/tools-registry.ts that facilitates bidirectional data exchange between Claude Code's local MEMORY.md file and the agentmemory persistent storage system. It accepts direction and filePath parameters to determine whether to import from or export to the specified file.
How does the import direction handle existing data?
When importing with direction: "import", the tool reads the provided MEMORY.md path and parses each line as a JSONL observation. These observations are then stored via the mem::remember function, which typically appends to or updates the existing memory graph in agentmemory without duplicating entries that match existing vector signatures.
Can I automate synchronization between the two systems?
Yes. Because the bridge exposes standard HTTP endpoints through the MCP server running on localhost (default port 49134), you can schedule periodic syncs using cron jobs, Git hooks, or Claude Code's session lifecycle hooks. The tool is designed to be called programmatically from any environment that can reach the MCP server.
Where is the actual JSONL conversion logic implemented?
The JSONL export pipeline is implemented in [src/functions/export-import.ts](https://github.com/rohitg00/agentmemory/blob/main/src/functions/export-import.ts), which contains the mem::exportAll function used during export operations. For imports, the parsing logic resides in the same file but is accessed through the import pathway defined in src/mcp/server.ts.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →