File size: 5,074 Bytes
4e909c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# Tool Development Guide
Tools are functions that agents can call to interact with external systems, perform calculations, or access data.
The `@tool` decorator registers a Python function as an agent tool with proper schema and tracing.
---
## Basic Tool Structure
```python
from laddr import tool
from typing import Dict
@tool(
name="weather_lookup",
description="Fetches current weather for a given city",
parameters={
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "description": "Units (metric/imperial)", "default": "metric"}
},
"required": ["city"]
}
)
def weather_lookup(city: str, units: str = "metric") -> Dict:
"""Tool docstring: fetch current weather."""
try:
result = get_weather(city, units)
return {"status": "success", "data": result}
except Exception as e:
return {"status": "error", "error": str(e)}
```
---
## The `@tool` Decorator
**Syntax:**
```python
@tool(
name: str,
description: str,
parameters: dict,
trace: bool = True,
trace_mask: list = []
)
```
---
## Key Features
| Parameter | Type | Required | Description |
|------------|------|-----------|-------------|
| name | str | ✅ | Unique identifier used by agents |
| description | str | ✅ | Summary of the tool’s purpose |
| parameters | dict | ✅ | JSON Schema for tool inputs |
| trace | bool | ❌ | Enables or disables logging |
| trace_mask | list | ❌ | Redacts sensitive trace fields |
---
### Parameter Descriptions
#### `name`
**Rules:**
- Must be unique and descriptive
- Use lowercase `snake_case`
- Avoid generic names
**Examples:**
```python
name="web_search"
name="calculate_area"
name="query_database"
```
---
#### `description`
**Best Practices:**
- Begin with an action verb
- Limit to 100 characters
- Avoid redundant phrasing like “use this tool to…”
**Examples:**
```python
description="Scrape text content from a webpage"
description="Translate text from English to French"
```
---
#### `parameters`
Each tool defines a **JSON Schema** describing its accepted parameters.
**Example:**
```python
parameters={
"type": "object",
"properties": {
"url": {"type": "string", "description": "Page URL"},
"timeout": {"type": "integer", "description": "Timeout seconds", "default": 10}
},
"required": ["url"]
}
```
**Supported JSON Schema Types:**
| Type | Description | Example |
|------|--------------|---------|
| string | Text input | `"hello"` |
| integer | Whole numbers | `42` |
| number | Decimals | `3.14` |
| boolean | True/False | `true` |
| array | Lists | `["a", "b"]` |
| object | Nested dict | `{"key": "value"}` |
---
#### `trace`
Enables or disables trace logging.
**Example:**
```python
trace=True # Default
```
Set to `False` when:
- Handling sensitive user data
- Reducing log volume for high-frequency tools
---
#### `trace_mask`
Redacts specified fields in traces.
**Example:**
```python
trace_mask=["api_key", "token", "password"]
```
Fields matching these keys will appear as:
```
***REDACTED***
```
in logs.
---
## Example Tool Patterns
### Pattern 1: API Wrapper
```python
@tool(name="api_call", description="Perform a GET request to external API")
def api_call(endpoint: str) -> Dict:
api_key = os.getenv("API_KEY")
response = requests.get(
f"https://api.example.com/{endpoint}",
headers={"Authorization": f"Bearer {api_key}"},
timeout=10
)
return {"status": "success", "data": response.json()}
```
---
### Pattern 2: Data Transformer
```python
@tool(name="convert_format", description="Convert data between formats")
def convert_format(data: str, to_format: str = "json") -> Dict:
if to_format == "json":
return {"result": json.loads(data)}
elif to_format == "csv":
return {"result": parse_csv(data)}
else:
return {"error": f"Unsupported format {to_format}"}
```
---
### Pattern 3: File Operations
```python
@tool(name="list_files", description="List files by pattern")
def list_files(directory: str, pattern: str = "*") -> Dict:
import glob
files = glob.glob(f"{directory}/{pattern}")
return {"files": files, "count": len(files)}
```
---
## Registering Tools with Agents
Tools must be passed to an agent during instantiation:
```python
from laddr import Agent
from tools.web_tools import web_search
from tools.math_tools import calculate
agent = Agent(
name="researcher",
tools=[web_search, calculate]
)
```
Agents automatically load the tool schemas and metadata for decision-making.
---
## Testing Tools
### Manual Testing (Python)
```python
from tools.web_tools import web_search
result = web_search(query="AI trends", max_results=3)
print(result)
```
### Manual Testing (API)
```bash
curl -X POST http://localhost:8000/api/agents/researcher/chat \
-H "Content-Type: application/json" \
-d '{"message": "search for AI trends"}'
```
|