Files
legal-shield/butler/n8n_server.py
T

94 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""MCP-Server `n8n`: Automationen in n8n (butler/n8n.py). Workflows als Entwurf bauen, Ausführungen prüfen,
veröffentlichte Webhook-Workflows starten (n8n_run nur mit Klick-Freigabe). Aktivieren und löschen nur der Nutzer.
Zugang: LibreChat schickt den n8n-API-Schlüssel des Nutzers in der Kopfzeile X-N8n-Api-Key (customUserVars).
Start: python -m butler.n8n_server (N8N_URL)
"""
from __future__ import annotations
from mcp.server.mcpserver import Context, MCPServer
from . import n8n
from .mcp_run import erwartet, starten
server = MCPServer("n8n", instructions=(
"Automationen in n8n (Skill `automatisieren`). Bestehendes: n8n_workflows, n8n_workflow_get, n8n_executions, "
"n8n_execution_get. Neues bauen: n8n_workflow_create legt immer einen inaktiven Entwurf an (nodes und "
"connections im n8n-Workflow-JSON); ändern mit n8n_workflow_update nur, solange er inaktiv ist. Veröffentlichen, "
"Zugangsdaten eintragen und löschen macht der Nutzer in n8n (Link zeigen). n8n_run startet einen veröffentlichten "
"Webhook-Workflow – Klick-Freigabe; vorher sagen, was der Workflow tut."))
def _z(ctx: Context | None):
try:
h = ctx.headers or {}
except (AttributeError, ValueError, RuntimeError):
h = {}
return n8n.zugang(h.get("x-n8n-api-key"))
@server.tool()
@erwartet
def n8n_workflows(ctx: Context | None = None) -> list[dict]:
"""Alle Workflows: id, Name, aktiv, Tags, Auslöser (Webhook-Pfad, Zeitplan …), Link."""
with _z(ctx):
return n8n.workflows()
@server.tool()
@erwartet
def n8n_workflow_get(workflow_id: str, ctx: Context | None = None) -> dict:
"""Ein Workflow vollständig (nodes, connections, settings)."""
with _z(ctx):
return n8n.workflow(workflow_id)
@server.tool()
@erwartet
def n8n_workflow_create(name: str, nodes: list[dict], connections: dict, settings: dict | None = None,
ctx: Context | None = None) -> dict:
"""Neuen Workflow als inaktiven Entwurf anlegen. nodes: [{name, type (z. B. 'n8n-nodes-base.scheduleTrigger'),
typeVersion, parameters, position}], connections: {"Knoten A": {"main": [[{"node": "Knoten B", "type": "main",
"index": 0}]]}}. Keine Zugangsdaten eintragen – das macht der Nutzer in n8n."""
with _z(ctx):
return n8n.anlegen(name, nodes, connections, settings)
@server.tool()
@erwartet
def n8n_workflow_update(workflow_id: str, name: str = "", nodes: list[dict] | None = None,
connections: dict | None = None, settings: dict | None = None,
ctx: Context | None = None) -> dict:
"""Inaktiven Workflow ändern (nur übergebene Teile werden ersetzt). Aktive Workflows lehnt das Werkzeug ab."""
with _z(ctx):
return n8n.aendern(workflow_id, name, nodes, connections, settings)
@server.tool()
@erwartet
def n8n_executions(workflow_id: str = "", status: str = "", limit: int = 10, ctx: Context | None = None) -> list[dict]:
"""Letzte Ausführungen (optional eines Workflows; status: success, error, waiting, running)."""
with _z(ctx):
return n8n.ausfuehrungen(workflow_id, limit, status)
@server.tool()
@erwartet
def n8n_execution_get(execution_id: str, ctx: Context | None = None) -> dict:
"""Eine Ausführung mit Daten (gekürzt) – zum Prüfen von Ergebnissen und Fehlern."""
with _z(ctx):
return n8n.ausfuehrung(execution_id)
@server.tool()
@erwartet
def n8n_run(workflow_id: str, data: dict | None = None, ctx: Context | None = None) -> dict:
"""Veröffentlichten Workflow über seinen Webhook starten (data als JSON). Klick-Freigabe."""
with _z(ctx):
return n8n.starten(workflow_id, data)
if __name__ == "__main__":
starten(server)