Below is a structured "Deep Dive" blueprint designed to be a definitive resource for self-hosting **n8n + MCP**.
---
## 🏗️ The "Direct Pipe" Architecture Guide
This setup ensures a 100% stable, unbuffered stream between your Raspberry Pi and Google Antigravity.
### **Phase 1: The Docker Engine (.env & Compose)**
The goal here is to stop n8n from "over-processing" the data before it leaves the Pi.
* **Key `.env` Nuances**:
* `N8N_SKIP_RESPONSE_COMPRESSION=true`: **Crucial.** Prevents n8n from gzipping the stream, which causes "Connection Closed: EOF" errors in strict AI agents.
* `N8N_TRUST_PROXY=all`: Tells n8n to trust the headers (like `Authorization`) passed by Nginx and Cloudflare.
* **Docker Compose Secret**:
* Ensure your `N8N_ENCRYPTION_KEY` is at least 32 characters. If you recreate your container without this being static, your MCP tokens will instantly become invalid.
---
### **Phase 2: The Nginx Location Block (The Pipe)**
Standard Nginx settings "buffer" data, which is death for real-time MCP streams. You need a dedicated location block for the `/mcp-server/` path.
```nginx
location ^~ /mcp-server/http {
proxy_set_header Host $host;
proxy_set_header Authorization $http_authorization;
# --- SSE STABILITY ---
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive"; # Keeps the pipe open during AI "thinking"
proxy_buffering off; # Forces data to flow instantly
proxy_cache off; # Prevents serving stale tool definitions
gzip off; # Essential: compression breaks SSE
# --- TIMEOUTS ---
proxy_read_timeout 24h; # Prevents the proxy from killing long tool calls
add_header X-Accel-Buffering no; # Extra layer to disable Nginx buffering
}
```---
### **Phase 3: Cloudflare Edge Rules**
Cloudflare's default "Speed" features often try to optimize traffic by holding it for a millisecond—this causes the AI agent to drop the connection.
* **Cache Rule (Bypass)**:
* **Rule**: `URI Path starts with /mcp-server/`.
* **Action**: Set **Cache Level** to **Bypass**. This ensures Cloudflare doesn't attempt to inspect or "save" the stream.
* **Disabled Speed Features**:
* **Early Hints** & **Speed Brain**: **Turn OFF.** These send partial data to "pre-load" pages, which confuses the JSON-RPC protocol used by MCP.
* **Brotli**: Ensure compression is disabled for this subdomain to maintain stream integrity.
---
### **Phase 4: Connecting Antigravity**
Antigravity uses the **Server-Sent Events (SSE)** transport. Once the "Direct Pipe" is verified, connecting is a one-step process.
2. **The Token**: Generate a **Bearer Token** in n8n's MCP settings. This token is verified by n8n against your static Encryption Key.
3. **The Discovery**: When the agent connects, it first pulls a list of "Capabilities." You will see it "Consulting documentation" before it ever calls your real data.
---
### **Phase 5: Verifying Success**
Don't just look at the tool list; test the **Data Retrieval**.
* **Documentation Pull**: "List the available n8n tools." (If this works, your Nginx/Cloudflare path is 200 OK).
* **Live Data Pull**: "Run `n8n_list_workflows`." (If this returns your actual workflow names like 'MCP TEST', your authentication and backend connection are 100% verified).
---