What is MCP & How to Use It with AI Agents in n8n (Full Tutorial!)

AI WorkshopAbout 5 min readMar 17, 2025Watch original
THE SUMMARYAI-generated

Key Concepts:

  • MCP (Memory, Context, and Personality): A framework for managing AI agent behavior in n8n.
  • n8n: A workflow automation platform.
  • AI Agents: Software entities designed to perform tasks autonomously.
  • Memory: The agent's ability to retain information from past interactions.
  • Context: The current situation or environment the agent is operating in.
  • Personality: The agent's defined characteristics and style of communication.
  • Vector Database (Pinecone): A database optimized for storing and querying vector embeddings.
  • Embeddings: Numerical representations of text or other data, capturing semantic meaning.
  • OpenAI API: Interface for accessing OpenAI's language models.
  • Prompt Engineering: Designing effective prompts to guide AI model behavior.
  • Workflow: A sequence of automated tasks in n8n.

I. Introduction to MCP in n8n

The video introduces the MCP framework as a way to enhance AI agent interactions within n8n workflows. The core idea is to provide AI agents with Memory, Context, and Personality to make them more effective and human-like. The presenter emphasizes that without these elements, AI agents can be forgetful, irrelevant, and bland.

II. Setting Up the n8n Workflow

The tutorial walks through building an n8n workflow that utilizes MCP. The initial steps involve setting up the necessary nodes:

  1. Webhook Node: This node receives incoming requests, acting as the entry point for the workflow.
  2. Set Node: Used to define initial variables, such as the user's input and a default agent personality.
  3. Pinecone Node: Connects to a Pinecone vector database for storing and retrieving memory.
  4. OpenAI Node: Interacts with the OpenAI API to generate responses from a language model.
  5. HTTP Request Node: Used for making API calls, potentially to external services for context.

III. Implementing Memory with Pinecone

The video details how to use Pinecone to give the AI agent memory. The process involves:

  1. Creating a Pinecone Index: A Pinecone index is created to store vector embeddings of past conversations. The presenter recommends using a dimension size of 1536, which is compatible with OpenAI's text-embedding-ada-002 model.
  2. Generating Embeddings: The OpenAI node is used to generate embeddings of the user's input. The text-embedding-ada-002 model is specifically mentioned.
  3. Storing Embeddings in Pinecone: The generated embeddings, along with the corresponding text, are stored in the Pinecone index. The user ID is used as metadata to associate memories with specific users.
  4. Retrieving Relevant Memories: When a new input is received, the OpenAI node generates an embedding of the input. This embedding is then used to query the Pinecone index for similar memories. The topK parameter in the Pinecone node determines how many memories are retrieved (e.g., topK: 5).

IV. Adding Context

The video explains how to incorporate context into the AI agent's responses. This can involve:

  1. External API Calls: Using the HTTP Request node to fetch information from external APIs based on the user's input. For example, if the user asks about the weather, an API call can be made to a weather service.
  2. Workflow Variables: Storing and accessing relevant information within the n8n workflow. For example, the user's location or preferences can be stored as variables.
  3. Prompt Engineering: Including contextual information in the prompt sent to the OpenAI model. For example, "The current weather in London is sunny."

V. Defining Personality

The video demonstrates how to define the AI agent's personality through prompt engineering. This involves:

  1. Setting a Personality Variable: A variable is created to store the agent's personality description. For example, "You are a helpful and friendly assistant."
  2. Including Personality in the Prompt: The personality description is included in the prompt sent to the OpenAI model. This helps the model generate responses that are consistent with the defined personality.
  3. Example: The presenter uses the example of defining the agent as a "sarcastic assistant" to illustrate how personality can influence the agent's responses.

VI. Constructing the Prompt

The video emphasizes the importance of prompt engineering. The prompt should include:

  1. Personality: The agent's defined personality.
  2. Context: Relevant contextual information.
  3. Memory: Retrieved memories from Pinecone.
  4. User Input: The user's current input.
  5. Instructions: Clear instructions for the AI model.

The presenter provides an example prompt structure:

You are a [Personality]. [Context]. Here are some relevant memories: [Memory]. User: [User Input].

VII. Generating the Response with OpenAI

The OpenAI node is used to generate the AI agent's response. The following parameters are important:

  1. Model: The language model to use (e.g., gpt-3.5-turbo).
  2. Prompt: The constructed prompt containing personality, context, memory, and user input.
  3. Temperature: Controls the randomness of the output (e.g., temperature: 0.7).
  4. Max Tokens: Limits the length of the generated response.

VIII. Storing the Conversation in Memory

After generating the response, the conversation (user input and agent response) is stored in the Pinecone index to update the agent's memory. This ensures that the agent can recall past interactions in future conversations.

IX. Returning the Response

The final step is to return the generated response to the user. This is typically done through the Webhook node.

X. Conclusion

The video concludes by highlighting the benefits of using the MCP framework in n8n. By providing AI agents with Memory, Context, and Personality, they can become more effective, engaging, and human-like. The presenter encourages viewers to experiment with different personalities, contexts, and memory retrieval strategies to optimize their AI agent workflows. The key takeaway is that MCP is a powerful tool for building more sophisticated and useful AI agents 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.