Knowledge Graphs in n8n are FINALLY Here!

Cole MedinAbout 6 min readSep 23, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • Knowledge Graphs: A way to store information that emphasizes the relationships between different entities (people, companies, products, etc.).
  • RAG (Retrieval-Augmented Generation): A framework for improving the accuracy and reliability of large language models by grounding them in external knowledge sources.
  • Vector Database: A database that stores data as vectors, allowing for efficient similarity searches.
  • Graffiti MCP Server: A tool that extracts entities and relationships from raw text and stores them in a Neo4j database. It also provides an MCP server for use with N8N.
  • Neo4j: A graph database used to store knowledge graphs.
  • N8N: A self-hostable workflow automation platform.
  • MCP (Message Control Protocol): A protocol for communication between agents and tools.
  • Entities and Relationships: The core components of a knowledge graph. Entities are the nodes, and relationships are the connections between them.

Traditional RAG vs. Knowledge Graph Enhanced RAG

  • Traditional RAG: Uses a vector database to store chunks of data. While effective, it struggles to represent and leverage relationships between entities.

  • Knowledge Graph Enhanced RAG: Augments traditional RAG by building a knowledge graph alongside the vector database. This allows the agent to navigate relationships between entities, leading to more informed and context-aware responses.

    • Example: Asking about a single company is suitable for a vector database search. Asking how two companies work together is better suited for a knowledge graph search.

Setting up Knowledge Graphs in N8N

This section details the process of integrating knowledge graphs into an existing N8N RAG template using Graffiti MCP server and Neo4j.

Prerequisites

  • Self-hosted N8N instance (cloud version not supported).
  • Digital Ocean droplet (or similar) with N8N up and running.

Step-by-Step Setup

  1. Install Graffiti and Neo4j:

    • Clone the Graffiti repository: git clone https://github.com/your-graffiti-repo
    • Change directory: cd graffiti/mcp_server
    • Set up environment variables:
      • Copy .example to .env: cp .example .env
      • Edit .env using nano .env and set:
        • OPENAI_API_KEY: Your OpenAI API key.
        • NEO4J_URL: Change localhost to neo4j (service name of the Neo4j container).
        • Optionally, change the Neo4j password.
      • Save changes in nano: Ctrl+X, then Y, then Enter.
    • Run Docker Compose: sudo docker-compose up -d
    • Check logs: sudo docker logs graffiti-mcp-server (look for "Uvicorn running").
    • Verify containers are running: docker ps -a (ensure no errors or exited containers).
  2. Configure N8N to Access Graffiti MCP:

    • Edit the N8N Docker Compose file:

      • Add the following lines under the n8n service to enable access to the host machine:
      extra_hosts:
        - "host.docker.internal:host-gateway"
      
      • Save changes in nano: Ctrl+X, then Y, then Enter.
    • Restart N8N: sudo docker-compose up -d

    • Access the N8N container: sudo docker exec -it <n8n_container_name> /bin/sh (get container name from docker ps -a).

    • Get the gateway IP address: ip route | grep default

    • Exit the N8N container: exit

    • Allow the gateway IP address through the firewall: sudo ufw allow from <gateway_ip> to any port <graffiti_mcp_port> (e.g., sudo ufw allow from 172.17.0.1 to any port 8030).

    • Reload the firewall: sudo ufw reload

  3. Install the Community MCP Node in N8N:

    • Go to N8N settings (bottom left).
    • Go to "Community Nodes".
    • Add a new node: n8n-nodes-mcp
    • Check the box and install.
  4. Configure the MCP Client Node:

    • Add an "MCP Client" node to your workflow.
    • Create a new connection:
      • Protocol: "Server Sent Events"
      • IP: host.docker.internal
      • Port: <graffiti_mcp_port> (e.g., 8030)
      • Path: /sse
    • Test the connection using the "List Available Tools" operation.

Integrating Knowledge Graph into the RAG Pipeline

  1. Add Memory Node:

    • Add an "MCP Client" node to the RAG pipeline after the data chunking step.
    • Operation: "Execute Tool"
    • Tool Name: "add_memory"
    • Parameters:
      • document_name: The name of the document being added.
      • episode_body: The text content of the document chunk.
  2. Search Memory Nodes Tool:

    • Add an "MCP Client" node as a tool for the agent.
    • Operation: "Execute Tool"
    • Tool Name: "search_memory_nodes"
    • Parameters:
      • query: The query for searching the knowledge graph (the LLM will determine this).

Key Arguments and Considerations

  • When to Use Knowledge Graphs: Knowledge graphs are most beneficial when dealing with relational data and complex queries that involve understanding relationships between entities.
  • Performance Considerations: Building and querying knowledge graphs can be slower and more expensive than using a vector database alone.
  • Agentic RAG: The template follows the theme of agentic RAG, giving the agent the ability to choose the best tool (vector database or knowledge graph) for a given query.
  • Customization: The template is designed to be customized and adapted to specific use cases and data.

Notable Quotes

  • "We're giving the agent the ability to search through our knowledge base in different ways."
  • "If we're asking about how two companies work together, now that would be a good example to go and search the knowledge graph."

Technical Terms Explained

  • Docker Compose: A tool for defining and running multi-container Docker applications.
  • UFW (Uncomplicated Firewall): A user-friendly firewall management tool for Linux.
  • Gateway IP Address: The IP address of the gateway that allows a container to communicate with the outside world.
  • Host.docker.internal: A special DNS name that resolves to the IP address of the host machine from within a Docker container.

Logical Connections

The video logically connects the need for knowledge graphs to the limitations of traditional vector database-based RAG. It then provides a step-by-step guide to setting up the necessary infrastructure (Graffiti MCP server, Neo4j) and integrating it into an existing N8N RAG pipeline. The video emphasizes the importance of understanding when to use knowledge graphs and provides considerations for performance and customization.

Data, Research Findings, or Statistics

The video does not explicitly mention specific data, research findings, or statistics. However, it implicitly relies on the understanding that knowledge graphs can improve the accuracy and relevance of responses in certain scenarios, particularly when dealing with relational data.

Synthesis/Conclusion

The video demonstrates how to enhance an N8N RAG template with knowledge graphs using Graffiti MCP server and Neo4j. By building a knowledge graph alongside a vector database, the agent gains the ability to understand and leverage relationships between entities, leading to more informed and context-aware responses. While knowledge graphs offer significant advantages for certain use cases, it's crucial to consider the performance implications and determine whether the added complexity is justified by the nature of the data and the types of queries being performed. The provided template offers a flexible framework for experimenting with and customizing knowledge graph-enhanced RAG in N8N.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.