Docs Menu
Docs Home
/
Atlas
/ / /

Build an AI Agent with MongoDB and n8n

This tutorial demonstrates how to quickly build an AI agent using n8n that can answer questions about some sample data. The agent leverages MongoDB as both a vector store for document retrieval and a chat memory store for conversation persistence.

The following screenshot demonstrates the workflow that you will build in this tutorial. In this workflow, you create an AI agent that uses two MongoDB nodes:

  • MongoDB Atlas Vector Store Node to retrieve relevant documents from the sample data

  • MongoDB Chat Memory Node to store conversation history

It also uses OpenAI as the LLM and embedding model provider.

When you send a message to the agent, it triggers the following actions:

  1. The AI agent node processes the user message and determines the next action.

  2. The AI agent checks whether it needs to make any tool calls.

    • If yes, it calls the MongoDB Vector Store node to retrieve documents.

    • If not, it continues to the next action.

  3. The agent generates a response using the LLM that you configured for the workflow.

  4. After each interaction, the MongoDB Chat Memory node stores the conversation history.

Screenshot of AI agent workflow with n8n and MongoDB
click to enlarge

Note

The red asterisks in the AI Agent node's configuration indicate required parameters.

Before you begin, ensure that you have the following:

  • n8n installed.

  • One of the following MongoDB cluster types:

    • An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later. Ensure that your IP address is included in your Atlas project's access list.

    • A local Atlas deployment created using the Atlas CLI. To learn more, see Create a Local Atlas Deployment.

    • A MongoDB Community or Enterprise cluster with Search and Vector Search installed.

  • The sample data loaded into your MongoDB deployment.

  • An OpenAI API key, or an API key for any other supported provider of LLMs and embedding models that you intend to use.

When working with n8n and MongoDB, consider the following:

  • For MongoDB Atlas users, ensure that you add your n8n IP addresses to your project's IP Access List. To learn more, see Network Access.

  • When configuring the MongoDB Vector Store node, ensure that you toggle the Include Metadata to Off. If you don't toggle this setting to Off, the AI agent might not run as the documents might be too large to load into the context window.

You must create a vector search index on your collection before you can use the MongoDB vector store node.

To use a different index creation method, follow the steps in the index creation procedure. Use the following configuration:

{
"fields": [
{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "dotProduct"
}
]
}

In this section, you complete the steps to configure the required credentials in n8n.

1

In the upper right corner of the n8n home page, right click Create Workflow. Alternatively, select the Credentials tab and click Add first credential.

2
  1. From the drop-down menu, select the credentials to configure.

    For example, select OpenAI and click Continue.

  2. To see which parameters to enter for your model provider, check the Credentials Library.

    For example, for OpenAI, enter the API Key, the name of the organization, and the base URL.

  3. Click Save. n8n adds the credential to your personal space.

3

Follow the same instructions described in the previous step to add your MongoDB credentials to n8n. Specify the following values:

Setting
Value

Configuration Type

In the dropdown menu, select the Connection String method.

Connection String

Enter your MongoDB Connection String.

To learn how to find your MongoDB connection string, see Connection Strings.

Database

Enter sample_mflix.

Complete the following steps in n8n to build an AI agent workflow with MongoDB and n8n. To learn more about the workflow, see About the n8n Workflow with MongoDB.

1

Open n8n where you are hosting it, then create a new workflow:

  • If you have no workflows or are using n8n for the first time, use the empty workflow that is displayed.

  • If you have existing workflows, click Create Workflow.

2

All workflows in n8n start with a trigger node. For this workflow, you use a chat trigger node so that the AI agent workflow is triggered by a user message.

  1. Select Add first step or press Tab to open the node menu.

  2. Search for Chat Trigger. n8n displays a list of nodes that match the search.

  3. Select Chat Trigger to add the node to the canvas.

  4. Click Back to canvas in the upper-left corner of your screen. You can skip testing this node at this stage. Click Save.

3

In the AI Agent node, you configure the chat model, memory, and tools. These are required components of the AI Agent node where you add additional nodes and sub-nodes.

  1. Select the + connector on the trigger node.

  2. Search for the AI Agent node and add it to the workflow. The editing view of the AI agent displays.

  3. Click Back to canvas in the upper-left corner of your screen. You can skip testing this node at this stage. Click Save.

4

Configure the chat model to attach the node to process incoming prompts:

  1. Click the + connector next to the Chat Model label on the AI Agent node. The search panel appears, filtered on Language Models.

  2. Select OpenAI Chat Model from the list, or your preferred LLM provider.

  3. Click Credential to connect with.

  4. After selecting your credentials, choose a model from the drop-down menu (for example, gpt-4o).

  5. Click Back to canvas in the upper-left corner of your screen. You can skip testing this node at this stage. Click Save.

  6. Click the AI Agent icon at the top of the dialog box to return to the AI Agent node configuration.

5

Configure the MongoDB Chat Memory sub-node to enable persistent conversation context.

  1. Click the + connector next to the Memory label on the AI Agent node.

  2. From the side panel, select MongoDB Chat Memory.

  3. Click Select credential, then select your MongoDB credentials.

  4. Use the default configurations for the MongoDB Chat Memory node. By default, n8n creates a collection called n8n_chat_histories in the specified database.

  5. Click Back to canvas in the upper-left corner of your screen. You can skip testing this node at this stage. Click Save.

  6. Click the AI Agent icon to return to the AI Agent node configuration.

6

Configure the MongoDB Atlas Vector Store node to enable document retrieval.

  1. Click the + connector next to the Tool label on the AI Agent node.

  2. In the search panel, select MongoDB Atlas Vector Store.

  3. Select the MongoDB credentials that you configured, and then specify the following values:

    Setting
    Value

    Operation Mode

    Retrieve Documents (As Tool for AI Agent)

    Description

    "Search through documents about movies to find relevant information"

    MongoDB Collection

    embedded_movies

    Embedding

    plot_embedding

    Metadata Field

    plot

    Vector Index Name

    vector_index

    Limit

    4

    Include Metadata

    Toggle to Off

    IMPORTANT:

    If you don't toggle this setting to Off, the AI agent might not run as the documents might be too large to load into the context window.

    Rerank Results

    Toggle to Off

7

Configure the embedding model for the MongoDB Atlas Vector Store node:

  1. Click the + connector next to the Embedding label on the MongoDB Atlas Vector Store node.

  2. From the side panel, select Embeddings OpenAI.

  3. Select your OpenAI credentials, and then specify the text-embedding-ada-002 model.

  4. Click Back to canvas in the upper-left corner of your screen. Click Save.

8

Now that you've configured the AI Agent node, you can test the complete workflow. After you send a message, the workflow runs in real-time on the canvas.

  1. Click the Open chat button near the bottom of the canvas.

  2. Test the memory functionality by running a few initial prompts. For example:

    Hi, my name is Mongo
    Hello Mongo! Nice to meet you. How can I help you today?
    What is my name?
    Your name is Mongo, as you mentioned when you introduced yourself earlier.
  3. Test the vector search and RAG functionality by running the following prompt.

    Recommend me a few movies about time travel
    Here are a few movies about time travel that you might enjoy:
    The Time Traveler's Wife - A romantic drama about a Chicago librarian with a gene that causes him to involuntarily time travel, leading to complications in his marriage.
    Timecop - An officer working for a security agency that regulates time travel must battle for his life against a corrupt politician tied to his past.
    We Are from the Future (My iz budushchego) - Four modern-day treasure seekers are unexpectedly transported into the midst of a World War II battle set in Russia.
    About Time - A young man discovers at the age of 21 that he can travel back in time. He attempts to improve his life, including finding love, but things don't turn out to be as simple as they seem.
    Let me know if you'd like any additional information about these films!
9
  1. Click the Save button in the top right of the editor window.

  2. Give your workflow a descriptive name. For example, name it "AI Agent with MongoDB".

You can return to this workflow at any time.

Back

n8n

On this page