AI Agent1 input → 1 output
AI Tool (Function)
Creates a JavaScript function-based tool that the AI Agent can call to execute custom logic.
Inputs
Outputs
- Output 1 - To Next Node
- Output 2
Not used during initial message pass. Function executes inline when AI calls the tool.
Details
Function tools let you write custom JavaScript that the AI can invoke. Perfect for:
- Data transformations
- Calculations
- Calling external APIs
- Database operations
Configuration
- Tool Name
- Unique identifier (letters, numbers, underscores, hyphens only)
- Description
- Tell the AI what this function does
- Function Code
- JavaScript code to execute. Has access to:
input- Arguments provided by the AIcontext- Node-RED context objectnode- The Node-RED node instance
Examples
Calculate Age:
// Tool: calculate_age
// Description: Calculate age from date of birth
const dob = new Date(input.dateOfBirth);
const today = new Date();
const age = today.getFullYear() - dob.getFullYear();
return { age: age };
Format Currency:
// Tool: format_currency
const amount = parseFloat(input.amount);
const formatted = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(amount);
return formatted;
Best Practices
- Use
new Date()instead ofDate() - Always return a value (AI receives this as tool result)
- Handle errors gracefully with try/catch
- Keep functions focused on single tasks