fix: resolve SplitInBatches output confusion for AI assistants (#97)

## Problem
AI assistants were consistently connecting SplitInBatches node outputs backwards because:
- Output index 0 = "done" (runs after loop completes)
- Output index 1 = "loop" (processes items inside loop)
This counterintuitive ordering caused incorrect workflow connections.

## Solution
Enhanced the n8n-mcp system to expose and clarify output information:

### Database & Schema
- Added `outputs` and `output_names` columns to nodes table
- Updated NodeRepository to store/retrieve output information

### Node Parsing
- Enhanced NodeParser to extract outputs and outputNames from nodes
- Properly handles versioned nodes like SplitInBatchesV3

### MCP Server
- Modified getNodeInfo to return detailed output descriptions
- Added connection guidance for each output
- Special handling for loop nodes (SplitInBatches, IF, Switch)

### Documentation
- Enhanced DocsMapper to inject critical output guidance
- Added warnings about counterintuitive output ordering
- Provides correct connection patterns for loop nodes

### Workflow Validation
- Added validateSplitInBatchesConnection method
- Detects reversed connections and provides specific errors
- Added checkForLoopBack with depth limit to prevent stack overflow
- Smart heuristics to identify likely connection mistakes

## Testing
- Created comprehensive test suite (81 tests)
- Unit tests for all modified components
- Edge case handling for malformed data
- Performance testing with large workflows

## Impact
AI assistants will now:
- See explicit output indices and names (e.g., "Output 0: done")
- Receive clear connection guidance
- Get validation errors when connections are reversed
- Have enhanced documentation explaining the correct pattern

Fixes #97

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-08-07 15:58:07 +02:00
parent a4e711a4e8
commit f508d9873b
12 changed files with 2895 additions and 12 deletions

View File

@@ -834,10 +834,26 @@ export class N8NDocumentationMCPServer {
null
};
// Process outputs to provide clear mapping
let outputs = undefined;
if (node.outputNames && node.outputNames.length > 0) {
outputs = node.outputNames.map((name: string, index: number) => {
// Special handling for loop nodes like SplitInBatches
const descriptions = this.getOutputDescriptions(node.nodeType, name, index);
return {
index,
name,
description: descriptions.description,
connectionGuidance: descriptions.connectionGuidance
};
});
}
return {
...node,
workflowNodeType: getWorkflowNodeType(node.package, node.nodeType),
aiToolCapabilities
aiToolCapabilities,
outputs
};
}
@@ -1937,6 +1953,52 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
};
}
private getOutputDescriptions(nodeType: string, outputName: string, index: number): { description: string, connectionGuidance: string } {
// Special handling for loop nodes
if (nodeType === 'nodes-base.splitInBatches') {
if (outputName === 'done' && index === 0) {
return {
description: 'Final processed data after all iterations complete',
connectionGuidance: 'Connect to nodes that should run AFTER the loop completes'
};
} else if (outputName === 'loop' && index === 1) {
return {
description: 'Current batch data for this iteration',
connectionGuidance: 'Connect to nodes that process items INSIDE the loop (and connect their output back to this node)'
};
}
}
// Special handling for IF node
if (nodeType === 'nodes-base.if') {
if (outputName === 'true' && index === 0) {
return {
description: 'Items that match the condition',
connectionGuidance: 'Connect to nodes that handle the TRUE case'
};
} else if (outputName === 'false' && index === 1) {
return {
description: 'Items that do not match the condition',
connectionGuidance: 'Connect to nodes that handle the FALSE case'
};
}
}
// Special handling for Switch node
if (nodeType === 'nodes-base.switch') {
return {
description: `Output ${index}: ${outputName || 'Route ' + index}`,
connectionGuidance: `Connect to nodes for the "${outputName || 'route ' + index}" case`
};
}
// Default handling
return {
description: outputName || `Output ${index}`,
connectionGuidance: `Connect to downstream nodes`
};
}
private getCommonAIToolUseCases(nodeType: string): string[] {
const useCaseMap: Record<string, string[]> = {
'nodes-base.slack': [