Production‑ready MongoDB checkpointer for LangGraph. A lightweight implementation of BaseCheckpointSaver that persists checkpoints and writes in MongoDB. Works out‑of‑the‑box with LangGraph’s serde: pass a serde for efficient typed storage, or omit it to store legacy shapes for compatibility. Ships with ESM/CJS builds and TypeScript types.
pnpm add langgraph-checkpoint-mongodb
# or: npm i langgraph-checkpoint-mongodbPeer deps you likely already have:
@langchain/langgraph@langchain/coremongodb
TypeScript (ESM) — simple usage (no serde):
import { MongoClient } from 'mongodb'
import type { BaseCheckpointSaver } from '@langchain/langgraph'
import { MongoCheckpointSaver } from 'langgraph-checkpoint-mongodb'
let saver: BaseCheckpointSaver | null = null
export async function getCheckpointer(): Promise<BaseCheckpointSaver> {
if (saver) return saver
const dbName = process.env.MONGODB_DB!
const uri = process.env.MONGODB_URI!
const client = await MongoClient.connect(uri)
saver = new MongoCheckpointSaver(client, dbName)
return saver
}
export async function closeCheckpointer() {
if (!saver) return
// if you kept a client reference, close it here
saver = null
}TypeScript (ESM) — with a custom serde:
import { MongoClient } from 'mongodb'
import { MongoCheckpointSaver } from 'langgraph-checkpoint-mongodb'
const serde = {
async dumpsTyped(v) {
const buf = Buffer.from(JSON.stringify(v))
return ['json', new Uint8Array(buf)] as const
},
async loadsTyped(_t, b) {
return JSON.parse(Buffer.from(b).toString('utf8'))
},
}
const client = await MongoClient.connect(process.env.MONGODB_URI!)
const saver = new MongoCheckpointSaver(client, process.env.MONGODB_DB!, serde)
// provide `saver` to your LangGraph appJavaScript (CJS) — minimal:
const { MongoClient } = require('mongodb')
const { MongoCheckpointSaver } = require('langgraph-checkpoint-mongodb')
(async () => {
const client = await MongoClient.connect(process.env.MONGODB_URI)
const saver = new MongoCheckpointSaver(client, process.env.MONGODB_DB)
// use saver
})()new MongoCheckpointSaver(client: MongoClient, dbName?: string, serde?: any)- Uses collections:
checkpoints,checkpoint_writes,checkpoint_versions
- Uses collections:
put(config, checkpoint, metadata, newVersions)→ stores a checkpointputWrites(config, writes, taskId)→ stores pending writesget(config)→ latest checkpointgetTuple(config)→CheckpointTuplewithpendingWriteslist(config, options)→ async iterator of checkpointsdeleteThread(thread_id, checkpoint_ns?)→ clears a thread namespaceclearAll()→ wipes all collections
Notes:
- Provide the LangGraph serde you use in your app; this saver persists typed bytes and falls back to legacy shape for compatibility.
- Ensure your
RunnableConfighasconfigurable.thread_idandconfigurable.checkpoint_ns.
Build:
pnpm run buildTest:
pnpm i
pnpm run test