For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Build an AI Agent with MongoDB and n8n

This tutorial demonstrates how to build an AI agent by using n8n that can answer questions about some sample data. The agent uses 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:

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

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. This tutorial requires n8n version 1.115.0 or later.

  • 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 Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

    • A MongoDB Community 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.

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

1

Click +, and then click New 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.

You must create a vector search index on your collection before you can use the MongoDB Atlas Vector Store node. In this section, you use the MongoDB node in n8n to create the index.

1

Click +, and then click New workflow.

2
  1. Click Add first step to open the node menu.

  2. Search for Manual Trigger, and then select it to add the node to the canvas.

3
  1. Select the + connector on the Manual Trigger node.

  2. Search for MongoDB, and then select the MongoDB node.

4

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

Setting
Value

Resource

Search Index

Operation

Create Search Index

Collection

embedded_movies

Index Name

vector_index

Index Type

Vector Search

For Index Definition, paste the following index definition. This definition indexes the plot_embedding field as the vector type with 1536 dimensions:

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

Click Execute step. When the node runs successfully, the output panel displays the name of the index.

MongoDB builds the index in the background. You can use the index in your workflows after the build completes.

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, and then click +. Then, click New 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, and 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)

    To learn more about this mode, see 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
    What is my name?
  3. Test the vector search and RAG functionality by running the following prompt.

    Recommend me a few movies about time travel
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.