n8n-custom-nodes

n8n AI tool node to execute workflows with JavaScript output transformation

Package Information

Downloads: 7 weeklyĀ /Ā 54 monthly
Latest Version: 0.1.0
Author: go9tech

Documentation

n8n Tool Workflow with Transformer

A custom n8n node that executes workflows as AI agent tools with JavaScript output transformation capabilities.

šŸŽÆ Overview

The Tool Workflow with Transformer node is based on the official n8n ToolWorkflow node, but adds a powerful feature: the ability to transform the workflow output using custom JavaScript code before returning it to the AI agent.

This is particularly useful when you need to:

  • Format workflow output for specific AI agent requirements
  • Extract only relevant data from complex workflow results
  • Convert data structures to match expected formats
  • Summarize or aggregate workflow outputs

✨ Features

  • šŸ¤– AI Tool Integration: Designed to work as a tool for AI agents
  • šŸ”„ Workflow Execution: Execute any n8n workflow from database or JSON
  • šŸ”§ Output Transformation: Transform workflow output with custom JavaScript
  • šŸ“¦ Flexible Input/Output: Handle various data formats
  • ⚔ Error Handling: Robust error handling and reporting

šŸ“¦ Installation

Using npm

npm install n8n-nodes-tool-workflow-transformer

Local Development

  1. Clone the repository:
git clone https://github.com/your-org/n8n-nodes-tool-workflow-transformer.git
cd n8n-nodes-tool-workflow-transformer
  1. Install dependencies:
npm install
  1. Build the project:
npm run build
  1. Link to n8n:
npm link
cd /path/to/n8n
npm link n8n-nodes-tool-workflow-transformer

šŸ”§ Node Configuration

Parameters

Tool Configuration

  • Name: The name of the tool function (alphanumeric with underscores)
  • Description: Description of what the tool does for the AI agent

Workflow Source

  • Source: Choose between:
    • Database: Load workflow by ID from n8n database
    • Define Below: Provide workflow JSON directly

Transformation

  • Transform Output: Toggle to enable/disable output transformation
  • Transformation Code: JavaScript code to transform the workflow output

Transformation Variables

The transformation code has access to:

  • $output: The output from the executed workflow (object or array)
  • $input: The input that was sent to the workflow

šŸ“ Usage Examples

Example 1: Extract Specific Fields

// Extract only names from an array of objects
if (Array.isArray($output)) {
  return $output.map(item => item.name).join(', ');
}
return $output.name || 'Unknown';

Example 2: Format for AI Response

// Format workflow output as a structured message
const count = Array.isArray($output) ? $output.length : 1;
return `Found ${count} results. Details: ${JSON.stringify($output, null, 2)}`;

Example 3: Aggregate Data

// Sum values from workflow output
if (Array.isArray($output)) {
  const total = $output.reduce((sum, item) => sum + (item.value || 0), 0);
  return { total, itemCount: $output.length, average: total / $output.length };
}
return $output;

Example 4: Error Handling

// Safely handle different output types
try {
  if (!$output) return 'No data received';
  
  if (Array.isArray($output)) {
    return $output.filter(item => item.status === 'active');
  }
  
  return $output.status === 'active' ? $output : null;
} catch (error) {
  return `Error processing data: ${error.message}`;
}

šŸ—ļø Architecture

The node extends the standard n8n workflow execution with:

  1. Workflow Execution: Executes the specified workflow with provided inputs
  2. Output Capture: Captures the workflow output data
  3. Transformation: Applies JavaScript transformation if enabled
  4. Result Return: Returns the (transformed) result to the AI agent

šŸ› ļø Development

Project Structure

n8n-nodes-tool-workflow-transformer/
ā”œā”€ā”€ nodes/
│   ā”œā”€ā”€ ToolWorkflowWithTransformer.node.ts
│   └── toolWorkflowWithTransformer.svg
ā”œā”€ā”€ dist/                 # Compiled files
ā”œā”€ā”€ package.json
└── tsconfig.json

Available Scripts

  • npm run build - Build the TypeScript code
  • npm run dev - Build in watch mode
  • npm run lint - Run ESLint
  • npm run lintfix - Fix linting issues

šŸ¤ Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

šŸ“„ License

MIT

šŸ†˜ Support

For issues or questions, please open an issue on GitHub.

šŸ”— Links


Made with ā¤ļø for the n8n community

Discussion