ai n8n claude mcp automation docker vps llm javascript workflow-engineering nexus-ai agentic-workflows

Orchestrating Autonomous Workflows: Integrating Claude via Model Context Protocol (MCP) and Self-Hosted n8n

5 min read

Orchestrating Autonomous Workflows: Integrating Claude via Model Context Protocol (MCP) and Self-Hosted n8n

The paradigm of Large Language Models (LLMs) is shifting from passive text generation to active, agentic execution. While standard chat interfaces allow for querying data, the true frontier lies in "Agentic Workflows"—systems where an LLM can autonomously trigger, manage, and even refactor backend automation pipelines. This post explores a sophisticated implementation of this concept: connecting Claude to a self-hosted n8n instance using the Model Context Protocol (MCP), enabling the model to execute scheduled tasks and write custom JavaScript logic directly into your production workflows.

The Infrastructure Layer: Self-Hosted n8n via Docker

To move beyond the limitations of local execution—where automations cease when a laptop lid is closed—a persistent, cloud-based environment is required. For this architecture, we utilize a VPS (Virtual Private Server) configured with at least a KVM2 tier specification: 2 vCPU cores and 8GB of RAM. This overhead is critical because the instance must simultaneously manage the n8n engine, an internal database for Data Tables, and the computational load of AI-driven nodes.

The deployment leverages Dockerized n8n. By utilizing pre-configured templates (such as those provided by Hostinger), we bypass the manual complexities of setting up reverse proxies and SSL certificates via the command line. The result is a production-ready instance accessible via a secure HTTPS endpoint, providing the necessary stable address for Claude to reach the server.

Engineering the Workflow: Data Persistence and Aggregation Logic

A robust automation requires structured data input and precise logic handling. In this implementation, we utilize n8n Data Tables, an internal, spreadsheet-like database engine that eliminates the need for external dependencies like Google Sheets or SQL databases for simpler tasks.

The Pipeline Architecture

The workflow follows a deterministic sequence:

  1. Schedule Trigger: An interval-based trigger (e.g., 07:00 daily) to initiate the routine.
  2. Data Table Node (getRows): Retrieving raw transaction or order data from the internal schema.
  3. The Aggregate Node (Critical Step): This is a frequent point of failure in AI-integrated workflows. When processing multiple rows, n8n executes subsequent nodes once for every input item. Without an Aggregate Node to consolidate these individual items into a single array/list, the LLM would trigger separate execution loops for every order, leading to redundant API calls and fragmented summaries. The Aggregate node transforms $N$ items back into 1 singular item containing the full dataset.

Integrating the LLM: Nexus AI and the Basic LLM Chain

The core intelligence is powered by Claude (specifically utilizing models like Claude 3.5/5.5 Haiku via an API router). To achieve this, we do not use a standard "Message a Model" node, which often lacks the necessary configuration for custom endpoints. Instead, we implement the Basic LLM Chain.

Configuring the OpenAI-Compatible Interface

To access Claude through an OpenAI-compatible architecture without managing multiple API keys, we utilize the Nexus AI router. This requires specific reconfiguration of the OpenAI Chat Model sub-node:

  • Credential Setup: Use the API key provided by the Nexus AI service provider.
  • Base URL Override: The default https://api.openai.com/v1 must be redirected to the Nexus endpoint: https://api.nexus.ai/v1.

Failure to update this Base URL is a common cause of "Model Not Supported" errors, as the node will attempt to query OpenAI's model registry rather than the routed Claude models available via Nexus. By using a smaller, high-speed model like Haiku, we minimize latency and token consumption while maintaining sufficient reasoning capabilities for data summarization.

Implementing MCP: Granting Claude Instance-Level Access

The most transformative aspect of this setup is the implementation of Instance-level MCP (Model Context Protocol) within n8n settings. This allows an external LLM interface (Claude) to act as a controller for your specific n8n instance.

Permissions and Security

When connecting Claude via the official n8n connector, you must define the scope of access:

  • Read-Only: Allows Claude to inspect workflow structures and execution history but prevents modification.
  • Custom/Full Access: Grants permission to execute workflows and modify nodes.

By enabling MCP, Claude gains a "door" into your server. When prompted with a natural language command like "Run my daily sales summary," Claude identifies the relevant workflow by its name or description, triggers the execution via the API, and parses the resulting output back into the chat interface.

Autonomous Refactoring: The Agentic Loop

The ultimate capability demonstrated here is Autonomous Workflow Development. Because Claude has access to the workflow's underlying structure, it can perform "Agentic Refactoring."

In a practical test, when asked to optimize a workflow that was manually built with inefficient nodes (such as an unnecessary Aggregate node or unoptimized filtering), Claude does not simply suggest changes—it writes and injects JavaScript directly into the n8n nodes. It can replace complex, multi-step logic chains with a single Code Node, effectively refactoring the architecture to be more performant and less prone to errors (such as date-handling bugs or arithmetic inaccuracies).

This represents a shift from "No-Code" to "AI-Code," where the human provides the high-level intent, and the LLM manages the low-level implementation within the n8n canvas.