{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "45ba993f-2078-4119-8722-6a710f1e2802",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1308,
        500
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.9
    },
    {
      "id": "2e4ded08-f4a0-4b7e-b833-ff1f8228a078",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1560,
        500
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "ed34d7d0-572e-4022-b14d-b7c1ead42240",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1340,
        720
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-nano",
          "cachedResultName": "gpt-4.1-nano"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ffbcd9d2-4276-48bc-aa39-dc97f07ca6d5",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -1220,
        720
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "b42d7a27-cc18-4d5c-b169-b041816e4ccc",
      "name": "Execute the tool",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        -1100,
        720
      ],
      "parameters": {
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "mZxPCcrfdwSgZDUQ",
          "cachedResultName": "Bright Data MCP Test"
        },
        "description": "This tool is MCP based and it will return the data accordingly. This tool will return API from Bright Data and the user might want to ask to search via Google. Please mind the response given by the agent.",
        "workflowInputs": {
          "value": {
            "query": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('query', `The usual query from AI.`, 'string') }}",
            "session_id": "={{ $json.sessionId }}"
          },
          "schema": [
            {
              "id": "query",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "query",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "tool_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "tool_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "session_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "session_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "bb56e06d-ea56-475c-b64c-e96c52532a1e",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -1300,
        1440
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-nano",
          "cachedResultName": "GPT-4.1-NANO"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "=Query: \n{{ $('Tool call by the chatbot').item.json.query }}"
            },
            {
              "role": "system",
              "content": "=Match the provided query with the MCP tools listed below. Ensure that the output adheres to the required format in the tools schema, populating the specified properties. If any parameters are missing, prompt the user for input. Output it as JSON. The required key is the \"name\" for the tool name and the other required is defined by the necessary tool's schema.\n\nOutput format:\n\n{\"name\": \"the name of the matched tool. If there are no matching tool, return 'none' \",\n\"parameters\": the corresponding parameters defined by the matching MCP tool. This could be an object, string, array or whatever \n\"additional_info_needed\": \"if there are missing info from the user based on the schema. Return 'none' if the info is sufficient.\"\n}\n\n\nMCP Tools:\n\n{{ $json.tools.toJsonString() }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "40d20726-6c0c-4688-8623-5b9c5cd0bb94",
      "name": "If1",
      "type": "n8n-nodes-base.if",
      "position": [
        -580,
        1400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "139b4be7-6995-4f72-9c49-b52784e197a4",
              "operator": {
                "type": "string",
                "operation": "notContains"
              },
              "leftValue": "={{ $json.message.content.additional_info_needed }}",
              "rightValue": "none"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c6f20cd6-64d8-4ad2-9b13-cc4bf40b0e4a",
      "name": "Edit Fields1",
      "type": "n8n-nodes-base.set",
      "position": [
        -360,
        1560
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "b04e2724-227e-412f-9ac5-5f8774e84bda",
              "name": "needed_more_info_from_the_user",
              "type": "object",
              "value": "={{ $json.message.content }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "5d9824a6-a5aa-444d-b34d-d23b0b463de3",
      "name": "Chat Memory Manager",
      "type": "@n8n/n8n-nodes-langchain.memoryManager",
      "position": [
        120,
        1240
      ],
      "parameters": {
        "mode": "insert",
        "messages": {
          "messageValues": [
            {
              "message": "={{ $('Bright Data MCP - Execute a tool').item.json.toJsonString() }}",
              "hideFromUI": true
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "0d030063-0675-4397-bbf3-6d884b266cde",
      "name": "Simple Memory1",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        120,
        1400
      ],
      "parameters": {
        "sessionKey": "={{ $('Tool call by the chatbot').item.json.session_id }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "47106d93-fa33-49f7-bc79-ecd6ab6866de",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -860,
        1440
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a2b0a1ae-c2c7-41c2-8d23-0cc7f1a7deb9",
              "operator": {
                "type": "string",
                "operation": "notContains"
              },
              "leftValue": "={{ $json.message.content.name }}",
              "rightValue": "none"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "8f640b71-3e7d-40f1-a636-de1cca8ba46a",
      "name": "Return error message for no matching tool",
      "type": "n8n-nodes-base.set",
      "position": [
        -680,
        1580
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "e654783c-e7c6-4c00-b18a-f4a3c37b2f2a",
              "name": "message",
              "type": "string",
              "value": "No matching tool"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a8e64261-225f-43e5-8530-65bea38a5657",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1720,
        340
      ],
      "parameters": {
        "width": 1000,
        "height": 540,
        "content": "## AI Chat Agent\nYou may use any AI model. Make sure to point the 'Execute the Tool' tool to the correct sub-workflow."
      },
      "typeVersion": 1
    },
    {
      "id": "bfb5099c-5fac-47ba-9fe0-20a571a6b0e8",
      "name": "Tool call by the chatbot",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -2120,
        1420
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "query"
            },
            {
              "name": "session_id"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "9235d4c6-1ac2-41fd-9b60-c491de53aa6d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1880,
        1280
      ],
      "parameters": {
        "width": 460,
        "height": 480,
        "content": "## Retrieve all possible tools\nTo minimize the time to get all the tools, store all the results in Edit Field node or Code node."
      },
      "typeVersion": 1
    },
    {
      "id": "18568e11-908e-4b70-980f-c19a2f572499",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1380,
        1280
      ],
      "parameters": {
        "color": 4,
        "width": 440,
        "height": 480,
        "content": "## Classify the query and match it with the appropriate MCP tool.\nThe tool will return a JSON object containing the tool\u2019s name and its parameters, adhering to the schema of a specific tool."
      },
      "typeVersion": 1
    },
    {
      "id": "88cbcb2b-10c8-4538-b27d-9ffa8cf99fd0",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -920,
        1280
      ],
      "parameters": {
        "color": 6,
        "width": 760,
        "height": 480,
        "content": "## Verify the output\nCheck the output from OpenAI if it needs some info from the user or there's no matchin tool from their inquiry."
      },
      "typeVersion": 1
    },
    {
      "id": "f5c45d2d-0ac1-4a4c-b052-bc01eef6c1c9",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        1120
      ],
      "parameters": {
        "color": 2,
        "width": 760,
        "height": 480,
        "content": "## Deliver the output to the AI Agent"
      },
      "typeVersion": 1
    },
    {
      "id": "52353143-f1a6-4146-99c1-80c9518a87e9",
      "name": "Copy the output from the MCP tool",
      "type": "n8n-nodes-base.set",
      "position": [
        460,
        1240
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={{ $('Bright Data MCP - Execute a tool').item.json.result }}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "844bd15c-e6ed-4077-8f12-3c5cee55c2c0",
      "name": "Bright Data MCP - List tools",
      "type": "n8n-nodes-mcp.mcpClient",
      "position": [
        -1720,
        1440
      ],
      "parameters": {},
      "credentials": {
        "mcpClientApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3fb02605-054b-43c5-b9bd-2b798323372c",
      "name": "Bright Data MCP - Execute a tool",
      "type": "n8n-nodes-mcp.mcpClient",
      "position": [
        -60,
        1380
      ],
      "parameters": {
        "toolName": "={{ $json.message.content.name }}",
        "operation": "executeTool",
        "toolParameters": "={{ $json.message.content.parameters.toJsonString().toLowerCase() }}"
      },
      "credentials": {
        "mcpClientApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Return error message for no matching tool",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Bright Data MCP - Execute a tool",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory1": {
      "ai_memory": [
        [
          {
            "node": "Chat Memory Manager",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Execute the tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Chat Memory Manager": {
      "main": [
        [
          {
            "node": "Copy the output from the MCP tool",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool call by the chatbot": {
      "main": [
        [
          {
            "node": "Bright Data MCP - List tools",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Bright Data MCP - List tools": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Bright Data MCP - Execute a tool": {
      "main": [
        [
          {
            "node": "Chat Memory Manager",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}