feat(core): Track workflow action source for external API and MCP requests#28483
feat(core): Track workflow action source for external API and MCP requests#28483RicardoE105 wants to merge 3 commits intomasterfrom
Conversation
…uests
Add a `source` field ('ui' | 'api' | 'n8n-mcp') to workflow telemetry
events to track whether workflow actions originate from the n8n UI,
public API, or MCP server.
- Add `WorkflowActionSource` type and `source` field to workflow-created,
workflow-saved, workflow-activated, and workflow-deactivated events
- Thread `source` through service methods from each entry point
- Add missing telemetry listeners for workflow-activated and
workflow-deactivated events
- MCP tools pass `source: 'n8n-mcp'`, public API passes `source: 'api'`,
UI defaults to `source: 'ui'`
❌ 7 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Add 'n8n-cli' to WorkflowActionSource type. The public API middleware detects User-Agent: n8n-cli and sets apiSource on the request, so workflow telemetry events distinguish CLI usage from other API consumers.
Performance ComparisonComparing current → latest master → 14-day baseline docker-stats
Idle baseline with Instance AI module loaded
Memory consumption baseline with starter plan resources
How to read this table
|
There was a problem hiding this comment.
1 issue found across 13 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/cli/src/public-api/v1/handlers/workflows/workflows.handler.ts">
<violation number="1" location="packages/cli/src/public-api/v1/handlers/workflows/workflows.handler.ts:31">
P1: Custom agent flagged.
According to linked Linear issue ADO-5062, the implementation must generate a `request_id` that combines the source and a UUID (e.g., `api-<uuid>`) at the ingress point, and propagate this ID to telemetry events to correlate related actions. The current implementation only extracts and passes the source string, missing the unique identifier required by the Data Team's proposed solution for correlating downstream side-effects.</violation>
</file>
Architecture diagram
sequenceDiagram
participant Client as External Client (UI/CLI/API)
participant MCP as MCP Tool Handler
participant Middleware as Public API Middleware
participant Handler as Workflow Handlers
participant Service as Workflow Service
participant Events as Event Service
participant Relay as Telemetry Relay
Note over Client,Relay: Workflow Action Source Tracking Flow
alt Request via Public API
Client->>Middleware: HTTP Request
Middleware->>Middleware: CHANGED: Check User-Agent header
alt User-Agent is 'n8n-cli'
Middleware->>Middleware: NEW: Set req.apiSource = 'n8n-cli'
else Other API client
Middleware->>Middleware: NEW: Set req.apiSource = 'api'
end
Middleware->>Handler: Forward Request
Handler->>Service: Call method with source
else Request via MCP
MCP->>Service: NEW: Call method with source: 'n8n-mcp'
else Request via UI (Legacy/Default)
Client->>Handler: Direct API Call
Handler->>Service: Call method (source undefined)
end
Service->>Service: Process Logic (Create/Save/Activate)
Service->>Events: emit('workflow-saved' | 'workflow-activated', { ..., source })
Note over Events,Relay: Event Processing
Events->>Relay: Trigger listeners
alt Event: Activated/Deactivated
Relay->>Relay: NEW: Receive workflow-activated/deactivated
else Event: Created/Saved
Relay->>Relay: CHANGED: Process workflow-created/saved
end
Relay->>Relay: CHANGED: Extract source (default to 'ui')
Relay->>Relay: telemetry.track('User ... workflow', { ..., source })
Relay-->>Service: Done
Service-->>Handler: Return Workflow
Handler-->>Client: 200 OK / Response
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| @@ -28,7 +28,8 @@ export = { | |||
| createWorkflow: [ | |||
There was a problem hiding this comment.
P1: Custom agent flagged.
According to linked Linear issue ADO-5062, the implementation must generate a request_id that combines the source and a UUID (e.g., api-<uuid>) at the ingress point, and propagate this ID to telemetry events to correlate related actions. The current implementation only extracts and passes the source string, missing the unique identifier required by the Data Team's proposed solution for correlating downstream side-effects.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/cli/src/public-api/v1/handlers/workflows/workflows.handler.ts, line 31:
<comment>According to linked Linear issue ADO-5062, the implementation must generate a `request_id` that combines the source and a UUID (e.g., `api-<uuid>`) at the ingress point, and propagate this ID to telemetry events to correlate related actions. The current implementation only extracts and passes the source string, missing the unique identifier required by the Data Team's proposed solution for correlating downstream side-effects.</comment>
<file context>
@@ -28,7 +28,8 @@ export = {
publicApiScope('workflow:create'),
async (req: WorkflowRequest.Create, res: express.Response): Promise<express.Response> => {
- const createdWorkflow = await createWorkflow(req.user, req.body);
+ const source = req.apiSource ?? 'api';
+ const createdWorkflow = await createWorkflow(req.user, req.body, source);
return res.json(createdWorkflow);
</file context>
Summary
Adds an optional
sourcefield to workflow telemetry events to track whether workflow actions originate from the n8n UI, public API, n8n CLI, or MCP server. Defaults to'ui'when not specified.Source values
uiapin8n-cliUser-Agent: n8n-cliheadern8n-mcpEvents affected
User created workflowUser saved workflowUser activated workflow(new listener)User deactivated workflow(new listener)Changes
WorkflowActionSourcetype —'ui' | 'api' | 'n8n-mcp' | 'n8n-cli'sourcefield on 4 workflow telemetry events — defaults to'ui'in the telemetry relay, so existing code doesn't need to pass itworkflow-activatedandworkflow-deactivated(previously missing — these events were emitted but never tracked)User-Agent: n8n-cliand setsapiSourceon the request, distinguishing CLI from other API consumersapiSourceonAuthenticatedRequest— typed as'api' | 'n8n-cli', set by middleware, read by handlers — no type casts neededHow to test
source: 'ui'source: 'api'source: 'n8n-cli'source: 'n8n-mcp'Related Linear tickets, Github issues, and Community forum posts
https://linear.app/n8n/issue/ADO-5062
Review / Merge checklist
Backport to Beta,Backport to Stable, orBackport to v1(if the PR is an urgent fix that needs to be backported)