Docs Menu
Docs Home
/
Atlas
/ /

Integrate MongoDB with LangGraph.js

You can integrate MongoDB with LangGraph.js to build AI agents and advanced RAG applications. This page provides an overview of the MongoDB LangGraph.js integration and how you can use MongoDB for both retrieval and agent state persistence in your LangGraph workflows.

Get Started

Note

For the Python integration, see LangGraph.

LangGraph is a specialized framework within the LangChain ecosystem designed for building AI agents and complex multi-agent workflows. Graphs are the core components of LangGraph, representing the workflow of your agent. The MongoDB LangGraph integration enables the following capabilities:

  • Retrieval Tools: You can use the MongoDB LangChain integration to quickly create retrieval tools for your LangGraph workflows.

  • MongoDB Checkpointer: You can persist the state of your LangGraph agents in MongoDB, providing conversation memory and fault tolerance.

Integrating your LangGraph applications with MongoDB allows you to consolidate both retrieval capabilities and agent state persistence in a single database, simplifying your architecture and reducing operational complexity.

You can seamlessly use retrievers as tools in your LangGraph workflow to retrieve relevant data from Atlas.

  1. To create a basic retrieval tool with Atlas Vector Search and LangChain:

    import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";
    import { MongoClient } from "mongodb";
    import { VoyageAIEmbeddings } from "@langchain/community/embeddings/voyage";
    import { createRetrieverTool } from "langchain/tools/retriever";
    // Instantiate the vector store
    const client = new MongoClient("<connection-string>");
    const collection = client.db("<databaseName>").collection("<collectionName>");
    const embeddingModel = new VoyageAIEmbeddings();
    const vectorStore = new MongoDBAtlasVectorSearch(embeddingModel, {
    collection: collection,
    indexName: "vector_index", // Name of the index
    textKey: "text", // Name of the collection field containing the raw content
    embeddingKey: "embedding", // Name of the collection field containing the embedded text
    });
    // Create a retriever tool
    const retriever = vectorStore.asRetriever();
    const retrieverTool = createRetrieverTool(
    retriever,
    {
    name: "vector_search_retriever", // Tool name
    description:
    "Retrieve relevant documents from the collection" // Tool description
    },
    );
  2. To add the tool as a node in LangGraph:

    1. Convert the tool into a node.

    2. Add the node to the graph.

    import { StateGraph } from "@langchain/langgraph";
    import { ToolNode } from "@langchain/langgraph/prebuilt";
    // Convert the retriever tool into a node
    const retrieverNode = new ToolNode([retrieverTool]);
    // Define the graph
    const workflow = new StateGraph(SomeGraphState)
    // Add the tool as a node in the graph
    .addNode("vector_search_retriever", retrieverNode);
    const graph = workflow.compile();

The MongoDB Checkpointer allows you to persist your agent's state in MongoDB. This feature enables human-in-the-loop, memory, time travel, and fault-tolerance for your LangGraph agents.

import { MongoDBSaver } from "@langchain/langgraph-checkpoint-mongodb";
import { MongoClient } from "mongodb";
// Connect to your Atlas cluster or local Atlas deployment
const client = new MongoClient("<connection-string>");
// Initialize the MongoDB checkpointer
const checkpointer = new MongoDBSaver(client);
// Instantiate the graph with the checkpointer
const app = graph.compile(checkpointer);

Back

Build Agents

On this page