Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Jokes
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 agent state persistence and retrieval in your LangGraph workflows.

To build a sample AI agent that uses all of the components on this page, see the tutorial.

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:

  • MongoDB LangGraph Checkpointer: You can persist the state of your LangGraph agents in MongoDB, providing short-term memory.

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

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

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

To install the package for this component:

npm install @langchain/langgraph-checkpoint-mongodb
import { MongoDBSaver } from "@langchain/langgraph-checkpoint-mongodb";
import { MongoClient } from "mongodb";
// Connect to your MongoDB cluster
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);

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

  1. To create a basic retrieval tool with MongoDB 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();

Back

Build an AI Agent

Earn a Skill Badge

Master "Gen AI" for free!

Learn more

On this page