Dynamic AI Server Selection and Contextual Reranking - n8n Workflow

Use this advanced n8n workflow to dynamically select and rank Model Context Protocol (MCP) servers using an OpenAI LLM for initial decision making and Contextual AI for highly relevant reranking. Explore n8n templates for tool calling.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • AI Developers: Those building complex AI agents that require dynamic, context-aware tool selection from large directories.

  • Automation Engineers: Users seeking advanced examples of conditional logic and external AI API integration within an n8n workflow.

  • LLM Enthusiasts: Individuals exploring how to leverage contextual reranking services like Contextual AI to enhance Retrieval-Augmented Generation (RAG) processes.

  • n8n Power Users: Anyone looking for sophisticated n8n templates demonstrating custom code and multiple service integrations.

Overview

When dealing with thousands of dynamic tools, such as Model Context Protocol (MCP) servers, traditional LLMs struggle with selection and context. This powerful n8n workflow solves this by introducing a two-stage AI process.

First, an OpenAI LLM (GPT-4.1 mini) acts as a high-level decision agent, analyzing the user's query and determining if an external MCP server is needed. If needed, the LLM generates a precise reranking instruction.

Second, the automation fetches a large list of up to 5000 servers from the PulseMCP directory. It then uses the Contextual AI Reranker service to score every potential server based on the user's query and the instruction generated by the initial AI agent. This specialized reranker identifies the most contextually relevant servers, eliminating the complexity of manual setup or generic tool lists. This advanced n8n workflow provides a robust solution for dynamic, scalable tool orchestration.

How it Works

This n8n workflow executes the following steps:


  1. Trigger: The User-Query chat trigger initiates the flow when a user sends a message. This is a common starting point for interactive n8n templates.

  2. AI Decision Agent: The LLM Agent for Decision-Making n8n node, powered by the OpenAI Chat Model (GPT-4.1-mini), analyzes the input query. It determines if an MCP server is necessary (usemcp: true/false) and generates a specific instruction text for contextual reranking.

  3. Conditional Routing: The If n8n node checks the LLM output. If usemcp is false, the flow terminates and provides the LLM's reason in Final Response1.

  4. Fetch Data (If needed): If use_mcp is true, the PulseMCP Fetch MCP Servers HTTP Request n8n node fetches up to 5000 server records from the external PulseMCP API.

  5. Data Preparation: The Parse MCP Server list into documents w metadata custom n8n node formats the thousands of server records into arrays of documents and metadata, combining them with the user's query and the AI-generated instruction. This is critical for the reranker API.

  6. Contextual Reranking: The ContextualAI Reranker HTTP Request n8n node calls the Contextual AI service, passing the query, instruction, and all 5000 documents. The reranker model scores the relevance of each server.

  7. Format Results: The Format the top 5 results custom n8n node processes the scores, identifies the top 5 ranked servers, and generates a structured, user-friendly message containing the server name, description, GitHub stars, downloads, and the relevance score.

  8. Final Output: The Final Response2 n8n node displays the highly relevant, contextually ranked list of MCP servers back to the user.

Installation Guide

To set up this dynamic server selection n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and import it directly into your n8n instance via the Workflows sidebar using the 'New' -> 'Import from JSON' option.

  2. OpenAI Credential Setup:

Find the OpenAI Chat Model n8n node.
Set up or select your OpenAI API credentials. This requires an API key.

  1. Contextual AI API Key:

This workflow uses an Environment Variable for security. In your n8n instance, go to the Variables section (typically in the sidebar).
Add a new environment variable named CONTEXTUALAIAPIKEY and paste your API key from Contextual AI.

  1. Activation: Ensure the n8n workflow is set to 'Active'. The User-Query n8n trigger will immediately be available for testing.

  2. Customization: If you wish to change the reranker model or the number of servers fetched, adjust the parameters in the PulseMCP Fetch MCP Servers and ContextualAI Reranker n8n nodes, respectively.

Node Details

User-Query (Chat Trigger): Function: The initial n8n trigger. Starts the workflow upon receiving a chat input. Key Configuration: Set to public with a starting message encouraging users to try the MCP Reranker.
OpenAI Chat Model (LangChain Node): Function: Provides the language model (GPT-4.1-mini) used by the LLM Agent. Key Configuration: Response format set to jsonobject for reliable parsing.
LLM Agent for Decision-Making (LangChain Agent n8n node): Function: Analyzes the user query using the system prompt to decide if external MCP servers are necessary and generates contextual reranking instructions. Key Configuration: System message explicitly demands a JSON output containing usemcp, reason, and instruction.
If (n8n node): Function: Controls the workflow path based on the LLM's decision, checking if the parsed JSON output for usemcp is true. This is a crucial n8n node for flow control.
PulseMCP Fetch MCP Servers (HTTP Request n8n node): Function: Retrieves the list of available MCP servers. Key Configuration: Query parameter countperpage is set to 5000 to fetch a large batch.
Parse MCP Server list into documents w metadata (Code n8n node): Function: A custom JavaScript n8n node that transforms the raw server list into structured documents and metadata arrays required by the Contextual AI Reranker API, ensuring optimal data transmission.
ContextualAI Reranker (HTTP Request n8n node): Function: Sends the query, instruction, and documents to the Contextual AI API for scoring. Key Configuration: Uses Bearer {{$vars.CONTEXTUALAIAPI_KEY}} for authorization and utilizes the ctxl-rerank-v2-instruct-multilingual reranker model.
Format the top 5 results (Code n8n node): Function: Sorts the results based on relevance score, extracts the top 5 servers, and formats the data into a clean text message for the final chat response. This n8n node completes the processing.

Related n8n Workflows

Free

Nodes: 9 Nodes
Updated: December 26 2025
View all
Created by
Jinash Rouniyar
Jinash Rouniyar

Featured*