🔧 Tool Calling & Function Execution
Tools là "hands" của AI Agent - cho phép agents thực hiện actions. Bài này deep dive vào tool design và implementation.
Tool Calling Flow
Diagram
sequenceDiagram
User->>Agent: Query with task
Agent->>Agent: Analyze & decide
Agent->>Tool: Call with parameters
Tool->>External: Execute action
External->>Tool: Return result
Tool->>Agent: Processed result
Agent->>User: Final responseDefining Tools trong n8n
Tool Node Structure
Mỗi tool cần:
- Name: Unique identifier
- Description: When to use (critical for agent)
- Parameters: Input schema
- Execution: Logic to run
Example: Weather Tool
Tool Configuration:
JavaScript
1// Tool Name: get_weather2// Description: Get current weather for a location. Use when user asks about weather conditions.34// Parameters Schema5{6 "type": "object",7 "properties": {8 "location": {9 "type": "string",10 "description": "City name (e.g., 'Hanoi', 'Ho Chi Minh City')"11 },12 "units": {13 "type": "string",14 "enum": ["celsius", "fahrenheit"],15 "description": "Temperature unit preference"16 }17 },18 "required": ["location"]19}HTTP Request Node:
JavaScript
1URL: https://api.weatherapi.com/v1/current.json2Method: GET3Query Parameters:4 - key: {{ $env.WEATHER_API_KEY }}5 - q: {{ $json.location }}67// Response mapping8return [{9 json: {10 location: $json.location.name,11 country: $json.location.country,12 temperature: $json.current.temp_c,13 condition: $json.current.condition.text,14 humidity: $json.current.humidity15 }16}];Common Tool Patterns
1. Database Query Tool
JavaScript
1// Tool: query_orders2// Description: Search customer orders by various criteria34// Code Node5const { customerId, status, dateFrom, dateTo } = $input.first().json;67let query = `SELECT * FROM orders WHERE 1=1`;8const params = [];910if (customerId) {11 query += ` AND customer_id = $${params.length + 1}`;12 params.push(customerId);13}1415if (status) {16 query += ` AND status = $${params.length + 1}`;17 params.push(status);18}1920if (dateFrom) {21 query += ` AND created_at >= $${params.length + 1}`;22 params.push(dateFrom);23}2425if (dateTo) {26 query += ` AND created_at <= $${params.length + 1}`;27 params.push(dateTo);28}2930query += ` ORDER BY created_at DESC LIMIT 10`;3132// Execute via PostgreSQL node33return [{ json: { query, params } }];2. Email Sending Tool
JavaScript
1// Tool: send_email2// Description: Send email to customer. Use for order confirmations, updates.34// Parameters5{6 "to": { "type": "string", "description": "Recipient email" },7 "subject": { "type": "string", "description": "Email subject" },8 "body": { "type": "string", "description": "Email content (HTML supported)" },9 "priority": { "type": "string", "enum": ["low", "normal", "high"] }10}1112// Gmail Node Configuration13To: {{ $json.to }}14Subject: {{ $json.subject }}15Message: {{ $json.body }}3. File Operation Tool
JavaScript
1// Tool: create_report2// Description: Generate and save report to file34const { reportType, data, format } = $input.first().json;56let content;7let filename;89switch (reportType) {10 case 'sales':11 content = generateSalesReport(data);12 filename = `sales_report_${Date.now()}.${format}`;13 break;14 case 'inventory':15 content = generateInventoryReport(data);16 filename = `inventory_${Date.now()}.${format}`;17 break;18}1920// Write to file or upload to cloud storage21return [{22 json: {23 success: true,24 filename,25 url: `https://storage.example.com/${filename}`26 }27}];4. API Integration Tool
JavaScript
1// Tool: create_task2// Description: Create task in project management system (Notion/Asana/ClickUp)34// Parameters5{6 "title": { "type": "string", "description": "Task title" },7 "description": { "type": "string", "description": "Task details" },8 "assignee": { "type": "string", "description": "Person to assign" },9 "dueDate": { "type": "string", "description": "Due date (YYYY-MM-DD)" },10 "priority": { "type": "string", "enum": ["low", "medium", "high", "urgent"] }11}1213// HTTP Request to Notion API14URL: https://api.notion.com/v1/pages15Method: POST16Headers:17 - Authorization: Bearer {{ $env.NOTION_API_KEY }}18 - Notion-Version: 2022-06-281920Body:21{22 "parent": { "database_id": "{{ $env.NOTION_DATABASE_ID }}" },23 "properties": {24 "Title": { "title": [{ "text": { "content": "{{ $json.title }}" } }] },25 "Status": { "select": { "name": "To Do" } },26 "Priority": { "select": { "name": "{{ $json.priority }}" } },27 "Due Date": { "date": { "start": "{{ $json.dueDate }}" } }28 }29}Advanced: Multi-Tool Workflows
Chained Tools
Diagram
graph LR
A[Agent] --> T1[Search Products]
T1 --> A
A --> T2[Check Inventory]
T2 --> A
A --> T3[Create Order]
T3 --> A
A --> R[Response]Agent System Prompt:
Text
1You are an order assistant. To process an order:2 31. First, use search_products to find the requested item42. Then, use check_inventory to verify availability53. If available, use create_order to place the order64. If not available, suggest alternatives7 8Always confirm with user before creating order.Parallel Tools
Một số queries cần multiple tools simultaneously:
JavaScript
1// Agent recognizes it needs weather AND news2Query: "Plan my trip to Da Nang next week"34// Agent calls both in parallel:5// Tool 1: get_weather("Da Nang")6// Tool 2: search_events("Da Nang", "next week")7// Tool 3: get_hotels("Da Nang", dates)89// Then synthesizes resultsTool Description Best Practices
Critical: Tool Descriptions
Agent chọn tools dựa trên description. Bad description = wrong tool usage!
Good Descriptions
JavaScript
1// ✅ Good2"Get current weather conditions for a specific city. 3Use this when user asks about temperature, rain, humidity, 4or weather forecast. Input must be a city name."56// ❌ Bad7"Weather tool"Include Examples
JavaScript
1"Search products in inventory.2Examples of when to use:3- 'Do you have iPhone 15?'4- 'Show me laptops under $1000'5- 'Is the blue dress in stock?'67Do NOT use for:8- Order status (use order_lookup instead)9- Price changes (use admin_tools)"Error Handling in Tools
Graceful Failures
JavaScript
1// Tool implementation2try {3 const result = await externalAPICall(params);4 5 if (!result.success) {6 return [{7 json: {8 error: true,9 message: "API returned error",10 details: result.error,11 suggestion: "Try with different parameters"12 }13 }];14 }15 16 return [{ json: result.data }];17 18} catch (error) {19 return [{20 json: {21 error: true,22 message: error.message,23 type: error.name,24 retryable: error.code === 'TIMEOUT'25 }26 }];27}Agent Handling Errors
Text
1System Prompt addition:2 3When a tool returns an error:41. Explain the issue to the user clearly52. If retryable, ask if they want to try again63. If not retryable, suggest alternative approaches74. Never make up data - admit when you cannot complete taskSecurity Considerations
Tool Security
- Validate inputs - Don't trust agent-generated params
- Limit scope - Tools should do ONE thing
- Audit logs - Track all tool executions
- Rate limits - Prevent runaway agents
- Confirmation - Require user approval for destructive actions
Input Validation Example
JavaScript
1// Before executing database query2const { customerId } = $input.first().json;34// Validate5if (!customerId || typeof customerId !== 'string') {6 return [{ json: { error: "Invalid customer ID" } }];7}89// Sanitize (prevent SQL injection)10const sanitizedId = customerId.replace(/[^a-zA-Z0-9-]/g, '');1112// Length check13if (sanitizedId.length > 50) {14 return [{ json: { error: "Customer ID too long" } }];15}Bài tập thực hành
Hands-on Exercise
Build Tool Set cho E-commerce Agent:
search_products: Search by name, category, pricecheck_inventory: Check stock levelsget_customer_orders: Lookup order historycreate_support_ticket: Create support case
Each tool should:
- Have clear description
- Validate inputs
- Handle errors gracefully
Target: Complete toolset cho customer support agent
Tiếp theo
Bài tiếp theo: Memory Systems - Implement conversation memory cho agents.
