AutomationFlowsAI & RAG › Dynamic Mcp Server Selection with Openai Gpt-4.1 and Contextual AI Reranker

Dynamic Mcp Server Selection with Openai Gpt-4.1 and Contextual AI Reranker

ByJinash Rouniyar @jinash on n8n.io

Thousands of MCP Servers exist and many are updated daily, making server selection difficult for LLMs. Current approaches require manually downloading and configuring servers, limiting flexibility. When multiple servers are pre-configured, LLMs get overwhelmed and confused about…

Chat trigger trigger★★★★☆ complexityAI-powered16 nodesOpenAI ChatChat TriggerAgentHTTP RequestChatN8N Nodes Contextualai
AI & RAG Trigger: Chat trigger Nodes: 16 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #8272 — we link there as the canonical source.

This workflow follows the Agent → Chat Trigger recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "ijenlYhCd6odrorU",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Rank MCP Servers using Contextual AI Reranker",
  "tags": [],
  "nodes": [
    {
      "id": "e01ddd1f-7261-4bfb-a1b1-7a6f58bfdb3c",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        912,
        368
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {
          "responseFormat": "json_object"
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "47e86618-9c89-4b70-b4ce-a30bd89eebde",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        1184,
        144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "47fd1d36-7a24-4086-9b68-ba5b42d9a714",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.output.parseJson().use_mcp }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "30458c75-70ef-4b18-91b3-6ad36db1370e",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1632,
        64
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "6c32510a-9b0a-40b4-90f3-3fae29359cee",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        2304,
        64
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "5ee74efd-afb7-4cbc-940a-92cda69aeef2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        64,
        -112
      ],
      "parameters": {
        "width": 480,
        "height": 1152,
        "content": "# Dynamic MCP Selection\n## PROBLEM\nThousands of MCP Servers exist and many are updated daily, making server selection difficult for LLMs.\n- Current approaches require manually downloading and configuring servers, limiting flexibility.\n- When multiple servers are pre-configured, LLMs get overwhelmed and confused about which server to use for specific tasks.\n\n### This template enables dynamic server selection from a live PulseMCP directory of 5000+ servers.\n\n## How it works\n- A user query goes to an LLM that decides whether to use MCP servers to fulfill a given query and provides reasoning for its decision.\n- Next, we fetch MCP Servers from Pulse MCP API and format them as documents for reranking\n- Now, we use Contextual AI's Reranker to score and rank all MCP Servers based on our query and instructions\n\n## How to set up\n- Sign up for a free trial of Contextual AI [here](https://app.contextual.ai/) to find CONTEXTUALAI_API_KEY.\n- Click on variables option in left panel and add a new environment variable CONTEXTUALAI_API_KEY.\n- For the baseline model, we have used GPT 4.1 mini, you can find your OpenAI API key[ here](https://platform.openai.com/api-keys)\n\n## How to customize the workflow\n- We use chat trigger to initate the workflow. Feel free to replace it with a webhook or other trigger as required.\n- We use OpenAI's GPT 4.1 mini as the baseline model and reranker prompt generator. You can swap out this section to use the LLM of your choice.\n- We fetch 5000 MCP Servers from the PulseMCP directory as a baseline number, feel free to adjust this parameter as required.\n- We are using Contextual AI's ctxl-rerank-v2-instruct-multilingual reranker model, which can be swapped with any one of the following rerankers: \n   1) ctxl-rerank-v2-instruct-multilingual\n   2) ctxl-rerank-v2-instruct-multilingual-mini\n   3)  ctxl-rerank-v1-instruct\n- You can checkout this [blog](https://contextual.ai/blog/context-engineering-for-your-mcp-client/) for more information about rerankers to make informed choice.\n- If you have feedback or need support, please email reranker-feedback@contextual.ai"
      },
      "typeVersion": 1
    },
    {
      "id": "14d4db9f-0191-4cdd-a05e-0e1438a312db",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        592,
        -112
      ],
      "parameters": {
        "color": 7,
        "width": 704,
        "height": 608,
        "content": "## 1. Determine whether MCP servers are needed\nBased on user's request, LLM determines the need for an MCP Server, provides a reason, and if needed, provides reranking instruction text which will be passed to reranker"
      },
      "typeVersion": 1
    },
    {
      "id": "01a24336-eca1-48bf-bafb-f7c3ea1bdcf5",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1360,
        -112
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 400,
        "content": "## 2. Fetch MCP Server list and format them\nWe fetch 5000 MCP Servers from PulseMCP directory and parse them as documents to pass it onto the Contextual AI Reranker"
      },
      "typeVersion": 1
    },
    {
      "id": "5c96dbde-fa89-4256-8850-95c5bfe21314",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2064,
        -112
      ],
      "parameters": {
        "color": 7,
        "width": 816,
        "height": 400,
        "content": "## 3. Rerank the servers and display top five results\nWe use Contextual AI's reranker to re-rank the servers and identify the top 5 servers based ont eh user query and re-ranker instruction, which is then formatted to be displayed in user friendly format.\n- You can checkout this [blog](https://contextual.ai/blog/introducing-instruction-following-reranker/) to learn more about rerankers"
      },
      "typeVersion": 1
    },
    {
      "id": "2716d897-3417-45af-9820-3b6e68378419",
      "name": "User-Query",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        608,
        144
      ],
      "parameters": {
        "public": true,
        "options": {
          "responseMode": "responseNodes",
          "allowFileUploads": true
        },
        "initialMessages": "Try MCP Reranker using Contextual AI's Reranker v2"
      },
      "typeVersion": 1.3
    },
    {
      "id": "8e59faac-6e36-43ec-9ef3-4169ab498065",
      "name": "LLM Agent for Decision-Making",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        832,
        144
      ],
      "parameters": {
        "options": {
          "systemMessage": "=Analyze this user query and decide if it requires external tools/APIs (Model Context Protocol (MCP) servers) or can be answered directly.\n        Query: \"{{ $json.chatInput }}\"\n\n        Consider:\n        - Does it need real-time data, web search, or external APIs?\n        - Does it require specialized tools (file management, databases, etc.)?\n        - Is it a complex task that would benefit from external services?\n        - Can it be answered with general knowledge alone?\n\n        If MCP is needed, also generate a concise reranking instruction for selecting the best external tools/APIs (MCPs) for this query.\n\n        The instruction should:\n        - Specify the exact capabilities/features/details that an MCP server requires for this query\n        - Look for domain/field specificity and functionality needs\n        - Any specific requirements that the user asks for\n        - Highlight the user's prioritized criteria for server selection\n\n        Base the instruction only on what is explicitly stated or clearly implied in the user's query.\n        Do not assume additional requirements or preferences that are not present in the query.\n\n        Respond with JSON: {\"use_mcp\": true/false, \"reason\": \"brief explanation\", \"instruction\": \"reranking instruction text or null if not needed\"}"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "3afade9b-c6ba-45d3-8011-1aa02bcf5ab3",
      "name": "PulseMCP Fetch MCP Servers",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1408,
        144
      ],
      "parameters": {
        "url": "=https://api.pulsemcp.com/v0beta/servers",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "count_per_page",
              "value": "5000"
            },
            {
              "name": "offset",
              "value": "0"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "dfb80dc9-dff8-4869-b160-f2e6b7e35742",
      "name": "Final Response1",
      "type": "@n8n/n8n-nodes-langchain.chat",
      "position": [
        1408,
        336
      ],
      "parameters": {
        "message": "= {{ $json.output.parseJson().reason }} Therefore, no MCP Servers are required to fulfill this request.",
        "options": {},
        "waitUserReply": false
      },
      "typeVersion": 1
    },
    {
      "id": "6b2fa6bb-79d3-4fc6-b0d6-7a04fe126662",
      "name": "Parse MCP Server list into documents w metadata",
      "type": "n8n-nodes-base.code",
      "position": [
        1856,
        64
      ],
      "parameters": {
        "jsCode": "const servers = $input.first().json.servers || [];\nconst documents = [];\nconst metadata = [];\n\nfor (const server of servers) {\n  documents.push(`MCP Server: ${server.name}\\nDescription: ${server.short_description}`);\n  metadata.push(`Name: ${server.name}, Stars: ${server.github_stars}, Downloads: ${server.package_download_count}`);\n}\n\nconst aiOutputRaw = $('LLM Agent for Decision-Making').first().json.output;\nconst aiOutput = JSON.parse(aiOutputRaw);\n\nreturn [{\n  json: {\n    query: $('User-Query').first().json.chatInput,\n    instruction: aiOutput.instruction,   \n    documents,\n    metadata,\n    servers\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "f6249154-124d-4e53-82c1-d18c078a0a12",
      "name": "Format the top 5 results",
      "type": "n8n-nodes-base.code",
      "position": [
        2528,
        64
      ],
      "parameters": {
        "jsCode": "const results = $input.first().json.results || [];\nconst servers = $('Parse MCP Server list into documents w metadata').first().json.servers || [];\n\nconst top = results.slice(0, 5).map((r, i) => {\n  const server = servers[r.index] || {};\n  return {\n    name: server.name || \"Unknown\",\n    description: server.short_description || \"N/A\",\n    stars: server.github_stars || 0,\n    downloads: server.package_download_count || 0,\n    score: r.relevance_score\n  };\n});\n\nlet message = \"Top MCP Servers \\n\\n\";\ntop.forEach((s, i) => {\n  message += `${i + 1}. ${s.name} (\u2b50 ${s.stars}, \u2b07\ufe0f ${s.downloads}, \ud83d\udd0e ${s.score.toFixed(2)})\\n   ${s.description}\\n\\n`;\n});\n\nreturn [{ json: { message } }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "8053044b-27fa-45b6-bd15-42d327dc7452",
      "name": "Final Response2",
      "type": "@n8n/n8n-nodes-langchain.chat",
      "position": [
        2752,
        64
      ],
      "parameters": {
        "message": "={{ $json.message }}",
        "options": {},
        "waitUserReply": false
      },
      "typeVersion": 1
    },
    {
      "id": "830a8333-3aa2-482c-b8a0-c05a978f2d81",
      "name": "Rerank documents",
      "type": "n8n-nodes-contextualai.contextualAi",
      "position": [
        2112,
        144
      ],
      "parameters": {
        "query": "={{ $json.query }}",
        "metadata": "={{ $json.metadata }}",
        "resource": "Reranker",
        "documents": "={{ $json.documents }}",
        "instruction": "={{ $json.instruction }}"
      },
      "credentials": {
        "contextualAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "680665db-f0f8-4e47-9046-76ce4d53c15f",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "PulseMCP Fetch MCP Servers",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ],
        [
          {
            "node": "Final Response1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Parse MCP Server list into documents w metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Format the top 5 results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User-Query": {
      "main": [
        [
          {
            "node": "LLM Agent for Decision-Making",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rerank documents": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "LLM Agent for Decision-Making",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Format the top 5 results": {
      "main": [
        [
          {
            "node": "Final Response2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PulseMCP Fetch MCP Servers": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LLM Agent for Decision-Making": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse MCP Server list into documents w metadata": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          },
          {
            "node": "Rerank documents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

Thousands of MCP Servers exist and many are updated daily, making server selection difficult for LLMs. Current approaches require manually downloading and configuring servers, limiting flexibility. When multiple servers are pre-configured, LLMs get overwhelmed and confused about…

Source: https://n8n.io/workflows/8272/ — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

Who is this workflow for? This workflow is designed for SEO analysts, content creators, marketing agencies, and developers who need to index a website and then interact with its content as if it were

Agent, OpenAI Chat, Memory Buffer Window +10
AI & RAG

This Chatbot automates the process of discovering job openings and generating tailored job application emails.

Chat Trigger, OpenAI Chat, Mcp Client Tool +12
AI & RAG

This template automates stock analysis by combining technical analysis, news sentiment, and real-time market data to generate actionable trading recommendations with confidence scores, risk management

Chat Trigger, HTTP Request, OpenAI +6
AI & RAG

💰 Beginner Investor – Learn the market faster with AI-powered insights guiding your decisions. 📈 Retail Trader – Optimize your trading strategy with in-depth analysis typically reserved for profession

HTTP Request, Output Parser Structured, OpenAI Chat +4
AI & RAG

Lection 9 main. Uses formTrigger, chatTrigger, agent, lmChatOpenAi. Event-driven trigger; 55 nodes.

Form Trigger, Chat Trigger, Agent +7