Lý thuyết
35 phút
Bài 2/3

Tool Calling & Function Execution

Master tool calling trong n8n AI Agents

🔧 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 response

Defining Tools trong n8n

Tool Node Structure

Mỗi tool cần:

  1. Name: Unique identifier
  2. Description: When to use (critical for agent)
  3. Parameters: Input schema
  4. Execution: Logic to run

Example: Weather Tool

Tool Configuration:

JavaScript
1// Tool Name: get_weather
2// Description: Get current weather for a location. Use when user asks about weather conditions.
3
4// Parameters Schema
5{
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.json
2Method: GET
3Query Parameters:
4 - key: {{ $env.WEATHER_API_KEY }}
5 - q: {{ $json.location }}
6
7// Response mapping
8return [{
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.humidity
15 }
16}];

Common Tool Patterns

1. Database Query Tool

JavaScript
1// Tool: query_orders
2// Description: Search customer orders by various criteria
3
4// Code Node
5const { customerId, status, dateFrom, dateTo } = $input.first().json;
6
7let query = `SELECT * FROM orders WHERE 1=1`;
8const params = [];
9
10if (customerId) {
11 query += ` AND customer_id = $${params.length + 1}`;
12 params.push(customerId);
13}
14
15if (status) {
16 query += ` AND status = $${params.length + 1}`;
17 params.push(status);
18}
19
20if (dateFrom) {
21 query += ` AND created_at >= $${params.length + 1}`;
22 params.push(dateFrom);
23}
24
25if (dateTo) {
26 query += ` AND created_at <= $${params.length + 1}`;
27 params.push(dateTo);
28}
29
30query += ` ORDER BY created_at DESC LIMIT 10`;
31
32// Execute via PostgreSQL node
33return [{ json: { query, params } }];

2. Email Sending Tool

JavaScript
1// Tool: send_email
2// Description: Send email to customer. Use for order confirmations, updates.
3
4// Parameters
5{
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}
11
12// Gmail Node Configuration
13To: {{ $json.to }}
14Subject: {{ $json.subject }}
15Message: {{ $json.body }}

3. File Operation Tool

JavaScript
1// Tool: create_report
2// Description: Generate and save report to file
3
4const { reportType, data, format } = $input.first().json;
5
6let content;
7let filename;
8
9switch (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}
19
20// Write to file or upload to cloud storage
21return [{
22 json: {
23 success: true,
24 filename,
25 url: `https://storage.example.com/${filename}`
26 }
27}];

4. API Integration Tool

JavaScript
1// Tool: create_task
2// Description: Create task in project management system (Notion/Asana/ClickUp)
3
4// Parameters
5{
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}
12
13// HTTP Request to Notion API
14URL: https://api.notion.com/v1/pages
15Method: POST
16Headers:
17 - Authorization: Bearer {{ $env.NOTION_API_KEY }}
18 - Notion-Version: 2022-06-28
19
20Body:
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 item
42. Then, use check_inventory to verify availability
53. If available, use create_order to place the order
64. If not available, suggest alternatives
7
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 news
2Query: "Plan my trip to Da Nang next week"
3
4// 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)
8
9// Then synthesizes results

Tool Description Best Practices

Critical: Tool Descriptions

Agent chọn tools dựa trên description. Bad description = wrong tool usage!

Good Descriptions

JavaScript
1// ✅ Good
2"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."
5
6// ❌ Bad
7"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?'
6
7Do 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 implementation
2try {
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 clearly
52. If retryable, ask if they want to try again
63. If not retryable, suggest alternative approaches
74. Never make up data - admit when you cannot complete task

Security Considerations

Tool Security
  1. Validate inputs - Don't trust agent-generated params
  2. Limit scope - Tools should do ONE thing
  3. Audit logs - Track all tool executions
  4. Rate limits - Prevent runaway agents
  5. Confirmation - Require user approval for destructive actions

Input Validation Example

JavaScript
1// Before executing database query
2const { customerId } = $input.first().json;
3
4// Validate
5if (!customerId || typeof customerId !== 'string') {
6 return [{ json: { error: "Invalid customer ID" } }];
7}
8
9// Sanitize (prevent SQL injection)
10const sanitizedId = customerId.replace(/[^a-zA-Z0-9-]/g, '');
11
12// Length check
13if (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:

  1. search_products: Search by name, category, price
  2. check_inventory: Check stock levels
  3. get_customer_orders: Lookup order history
  4. create_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.