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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 08 β Dynamic Tool Ecosystem: How to Add ANY Tool
|
| 2 |
+
|
| 3 |
+
## π― What This Chapter Covers
|
| 4 |
+
|
| 5 |
+
- How tools are registered in smolagents
|
| 6 |
+
- How to add new tools without retraining the model
|
| 7 |
+
- The tool marketplace concept (1000+ MCP servers)
|
| 8 |
+
- How the agent discovers and uses new tools automatically
|
| 9 |
+
- Architecture for a "tool marketplace" in our agent harness
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
## π§© The Core Principle: Pattern Over Specifics
|
| 14 |
+
|
| 15 |
+
**The #1 insight from our research:** Our 1.7B model doesn't need to know about SPECIFIC tools. It needs to know the PATTERN of using tools.
|
| 16 |
+
|
| 17 |
+
Think of it like this:
|
| 18 |
+
- **Bad approach:** Train model on "how to use Tool A, Tool B, Tool C..."
|
| 19 |
+
- **Good approach:** Train model on "how to write Python code that solves problems"
|
| 20 |
+
|
| 21 |
+
The model already knows Python (Qwen3 was trained on code). We just need to teach it to:
|
| 22 |
+
1. Break problems into steps
|
| 23 |
+
2. Use available Python libraries/functions
|
| 24 |
+
3. Handle errors and try alternatives
|
| 25 |
+
|
| 26 |
+
**Result:** You can add ANY new tool (any Python function) and the model will figure out how to use it.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## π§ How Tool Registration Works in smolagents
|
| 31 |
+
|
| 32 |
+
### The Simple Way: @tool Decorator
|
| 33 |
+
|
| 34 |
+
```python
|
| 35 |
+
from smolagents import tool
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def my_awesome_tool(input_param: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
What this tool does (this becomes the "instruction manual" for the LLM).
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
input_param: What this parameter means
|
| 44 |
+
"""
|
| 45 |
+
# Your code here
|
| 46 |
+
result = do_something(input_param)
|
| 47 |
+
return result
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
**That's it.** The `@tool` decorator automatically:
|
| 51 |
+
1. Reads the function name β becomes the tool name
|
| 52 |
+
2. Reads the docstring β becomes the tool description (shown to the LLM)
|
| 53 |
+
3. Reads type hints β becomes the parameter schema
|
| 54 |
+
4. Registers it in the agent's "toolbox"
|
| 55 |
+
|
| 56 |
+
### Example: Adding a Weather Tool
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
from smolagents import tool
|
| 60 |
+
import requests
|
| 61 |
+
|
| 62 |
+
@tool
|
| 63 |
+
def get_weather(city: str, country_code: str = "") -> str:
|
| 64 |
+
"""
|
| 65 |
+
Get current weather for a city. Returns temperature, conditions, and forecast.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
city: The city name (e.g., "London", "New York")
|
| 69 |
+
country_code: Optional 2-letter country code (e.g., "US", "GB")
|
| 70 |
+
"""
|
| 71 |
+
url = f"https://api.openweathermap.org/data/2.5/weather"
|
| 72 |
+
params = {
|
| 73 |
+
"q": f"{city},{country_code}" if country_code else city,
|
| 74 |
+
"appid": "YOUR_API_KEY",
|
| 75 |
+
"units": "metric"
|
| 76 |
+
}
|
| 77 |
+
response = requests.get(url, params=params)
|
| 78 |
+
data = response.json()
|
| 79 |
+
|
| 80 |
+
temp = data["main"]["temp"]
|
| 81 |
+
conditions = data["weather"][0]["description"]
|
| 82 |
+
humidity = data["main"]["humidity"]
|
| 83 |
+
|
| 84 |
+
return f"Weather in {city}: {temp}Β°C, {conditions}, humidity {humidity}%"
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
**What the LLM sees:**
|
| 88 |
+
```
|
| 89 |
+
You have access to the following tools:
|
| 90 |
+
|
| 91 |
+
- get_weather(city: str, country_code: str = "")
|
| 92 |
+
Get current weather for a city. Returns temperature, conditions, and forecast.
|
| 93 |
+
Args:
|
| 94 |
+
city: The city name (e.g., "London", "New York")
|
| 95 |
+
country_code: Optional 2-letter country code (e.g., "US", "GB")
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
**The LLM learns to use it from the description alone.** No training needed!
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## π¦ The "Tool Marketplace" Concept
|
| 103 |
+
|
| 104 |
+
### How It Works
|
| 105 |
+
|
| 106 |
+
```
|
| 107 |
+
βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 108 |
+
β Tool Marketplace β
|
| 109 |
+
β β
|
| 110 |
+
β ββββββββββββ ββββββββββββ ββββββββββββ β
|
| 111 |
+
β β Weather β β Finance β β Social β β
|
| 112 |
+
β β Tool β β Tool β β Media β β
|
| 113 |
+
β β (Free) β β (Free) β β (Free) β β
|
| 114 |
+
β ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ β
|
| 115 |
+
β β β β β
|
| 116 |
+
β ββββββββββββ ββββββββββββ ββββββββββββ β
|
| 117 |
+
β β Browser β β GitHub β β Image β β
|
| 118 |
+
β β Tool β β Tool β β Gen Tool β β
|
| 119 |
+
β β (Built) β β (Built) β β (Built) β β
|
| 120 |
+
β ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ β
|
| 121 |
+
β β β β β
|
| 122 |
+
β ββββββββββββ ββββββββββββ ββββββββββββ β
|
| 123 |
+
β β Database β β Email β β Calendar β β
|
| 124 |
+
β β Tool β β Tool β β Tool β β
|
| 125 |
+
β β (Built) β β (Built) β β (Built) β β
|
| 126 |
+
β ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ β
|
| 127 |
+
β β β β β
|
| 128 |
+
β ββββββββ¬βββββββ΄βββββββ¬βββββ β
|
| 129 |
+
β β β β
|
| 130 |
+
β βΌ βΌ β
|
| 131 |
+
β βββββββββββββββββββββββββββ β
|
| 132 |
+
β β Agent Tool Loader β β
|
| 133 |
+
β β (User picks which β β
|
| 134 |
+
β β tools to enable) β β
|
| 135 |
+
β ββββββββββββββ¬βββββββββββββ β
|
| 136 |
+
β β β
|
| 137 |
+
β βΌ β
|
| 138 |
+
β βββββββββββββββββββββββββββ β
|
| 139 |
+
β β CodeAgent with Tools β β
|
| 140 |
+
β β (Model sees all enabledβ β
|
| 141 |
+
β β tool descriptions) β β
|
| 142 |
+
β βββββββββββββββββββββββββββ β
|
| 143 |
+
βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
### Adding a Tool Is Just Installing a Package
|
| 147 |
+
|
| 148 |
+
```bash
|
| 149 |
+
# Install weather tool
|
| 150 |
+
pip install some-weather-library
|
| 151 |
+
|
| 152 |
+
# Add to agent config
|
| 153 |
+
# (The tool is auto-registered via @tool decorator)
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
Or for MCP servers:
|
| 157 |
+
```bash
|
| 158 |
+
# Install MCP server
|
| 159 |
+
npm install -g @some-org/mcp-weather
|
| 160 |
+
|
| 161 |
+
# Register in agent
|
| 162 |
+
# Agent discovers tools from the MCP server's tool definitions
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
## π οΈ Building Our Tool Ecosystem
|
| 168 |
+
|
| 169 |
+
### Phase 1: Core Tools (Built Into Agent)
|
| 170 |
+
|
| 171 |
+
These are always available β the foundation:
|
| 172 |
+
|
| 173 |
+
```python
|
| 174 |
+
# core_tools.py
|
| 175 |
+
from smolagents import tool
|
| 176 |
+
import os, subprocess, json
|
| 177 |
+
|
| 178 |
+
@tool
|
| 179 |
+
def read_file(file_path: str, max_chars: int = 10000) -> str:
|
| 180 |
+
"""Read contents of a file."""
|
| 181 |
+
with open(file_path, 'r') as f:
|
| 182 |
+
return f.read()[:max_chars]
|
| 183 |
+
|
| 184 |
+
@tool
|
| 185 |
+
def write_file(file_path: str, content: str) -> str:
|
| 186 |
+
"""Write content to a file."""
|
| 187 |
+
os.makedirs(os.path.dirname(file_path) or '.', exist_ok=True)
|
| 188 |
+
with open(file_path, 'w') as f:
|
| 189 |
+
f.write(content)
|
| 190 |
+
return f"Written {len(content)} chars to {file_path}"
|
| 191 |
+
|
| 192 |
+
@tool
|
| 193 |
+
def list_directory(path: str = '.') -> str:
|
| 194 |
+
"""List files and folders in a directory."""
|
| 195 |
+
entries = os.listdir(path)
|
| 196 |
+
return "\n".join(sorted(entries))
|
| 197 |
+
|
| 198 |
+
@tool
|
| 199 |
+
def execute_shell(command: str) -> str:
|
| 200 |
+
"""Execute a shell command safely."""
|
| 201 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
|
| 202 |
+
return result.stdout + result.stderr
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
### Phase 2: Web Tools (Internet Access)
|
| 206 |
+
|
| 207 |
+
```python
|
| 208 |
+
# web_tools.py
|
| 209 |
+
from smolagents import tool
|
| 210 |
+
import requests
|
| 211 |
+
from bs4 import BeautifulSoup
|
| 212 |
+
|
| 213 |
+
@tool
|
| 214 |
+
def fetch_webpage(url: str) -> str:
|
| 215 |
+
"""Fetch and extract text content from a webpage."""
|
| 216 |
+
response = requests.get(url, timeout=30)
|
| 217 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 218 |
+
# Remove scripts and styles
|
| 219 |
+
for script in soup(["script", "style"]):
|
| 220 |
+
script.decompose()
|
| 221 |
+
return soup.get_text()[:10000]
|
| 222 |
+
|
| 223 |
+
@tool
|
| 224 |
+
def web_search(query: str, num_results: int = 5) -> str:
|
| 225 |
+
"""Search the web using DuckDuckGo."""
|
| 226 |
+
# Using DuckDuckGo's HTML interface
|
| 227 |
+
response = requests.get(f"https://html.duckduckgo.com/html/?q={query}")
|
| 228 |
+
# Parse results...
|
| 229 |
+
return formatted_results
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
### Phase 3: Analysis Tools (Data Processing)
|
| 233 |
+
|
| 234 |
+
```python
|
| 235 |
+
# analysis_tools.py
|
| 236 |
+
from smolagents import tool
|
| 237 |
+
import pandas as pd
|
| 238 |
+
import matplotlib.pyplot as plt
|
| 239 |
+
|
| 240 |
+
@tool
|
| 241 |
+
def analyze_csv(file_path: str, query: str) -> str:
|
| 242 |
+
"""Load a CSV file and answer questions about it using pandas."""
|
| 243 |
+
df = pd.read_csv(file_path)
|
| 244 |
+
# Agent writes code to analyze
|
| 245 |
+
# Could generate summary stats, charts, etc.
|
| 246 |
+
return str(df.describe())
|
| 247 |
+
|
| 248 |
+
@tool
|
| 249 |
+
def create_chart(data_source: str, chart_type: str, output_path: str) -> str:
|
| 250 |
+
"""Create a chart from data."""
|
| 251 |
+
# chart_type: "bar", "line", "pie", "scatter"
|
| 252 |
+
# Agent writes matplotlib code
|
| 253 |
+
# Saves to output_path
|
| 254 |
+
return output_path
|
| 255 |
+
```
|
| 256 |
+
|
| 257 |
+
### Phase 4: Creative Tools (Generation)
|
| 258 |
+
|
| 259 |
+
```python
|
| 260 |
+
# creative_tools.py
|
| 261 |
+
from smolagents import tool
|
| 262 |
+
from diffusers import StableDiffusionPipeline
|
| 263 |
+
import torch
|
| 264 |
+
|
| 265 |
+
# Load model once at startup
|
| 266 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 267 |
+
"runwayml/stable-diffusion-v1-5",
|
| 268 |
+
torch_dtype=torch.float16
|
| 269 |
+
).to("cuda")
|
| 270 |
+
|
| 271 |
+
@tool
|
| 272 |
+
def generate_image(prompt: str, output_path: str = "generated.png") -> str:
|
| 273 |
+
"""Generate an image from a text description."""
|
| 274 |
+
image = pipe(prompt, num_inference_steps=20).images[0]
|
| 275 |
+
image.save(output_path)
|
| 276 |
+
return output_path
|
| 277 |
+
|
| 278 |
+
@tool
|
| 279 |
+
def generate_code(language: str, task: str, output_path: str) -> str:
|
| 280 |
+
"""Generate code for a specific task."""
|
| 281 |
+
# Uses the LLM itself to generate code
|
| 282 |
+
# Then saves to file
|
| 283 |
+
return output_path
|
| 284 |
+
```
|
| 285 |
+
|
| 286 |
+
---
|
| 287 |
+
|
| 288 |
+
## π§ How the Agent Uses Tools It Never Saw Before
|
| 289 |
+
|
| 290 |
+
### Example: User Adds a "Crypto Price" Tool
|
| 291 |
+
|
| 292 |
+
**Step 1: User installs the tool**
|
| 293 |
+
```bash
|
| 294 |
+
pip install crypto-price-library
|
| 295 |
+
```
|
| 296 |
+
|
| 297 |
+
**Step 2: User writes the tool wrapper**
|
| 298 |
+
```python
|
| 299 |
+
from smolagents import tool
|
| 300 |
+
import crypto_price
|
| 301 |
+
|
| 302 |
+
@tool
|
| 303 |
+
def get_crypto_price(symbol: str, currency: str = "USD") -> str:
|
| 304 |
+
"""
|
| 305 |
+
Get the current price of a cryptocurrency.
|
| 306 |
+
|
| 307 |
+
Args:
|
| 308 |
+
symbol: The cryptocurrency symbol (e.g., "BTC", "ETH", "SOL")
|
| 309 |
+
currency: The currency to convert to (default: "USD")
|
| 310 |
+
"""
|
| 311 |
+
price = crypto_price.get_current(symbol, currency)
|
| 312 |
+
return f"{symbol}: ${price} {currency}"
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
**Step 3: Register with agent**
|
| 316 |
+
```python
|
| 317 |
+
from smolagents import CodeAgent
|
| 318 |
+
from my_tools import get_crypto_price, read_file, write_file
|
| 319 |
+
|
| 320 |
+
agent = CodeAgent(
|
| 321 |
+
model=my_model,
|
| 322 |
+
tools=[get_crypto_price, read_file, write_file],
|
| 323 |
+
)
|
| 324 |
+
```
|
| 325 |
+
|
| 326 |
+
**Step 4: The agent automatically learns**
|
| 327 |
+
|
| 328 |
+
```
|
| 329 |
+
User: "What's the price of Bitcoin and should I invest?"
|
| 330 |
+
|
| 331 |
+
Agent's system prompt now includes:
|
| 332 |
+
"- get_crypto_price(symbol: str, currency: str = 'USD')
|
| 333 |
+
Get the current price of a cryptocurrency."
|
| 334 |
+
|
| 335 |
+
Agent thinks (via CodeAgent pattern):
|
| 336 |
+
"User wants Bitcoin price. I have get_crypto_price tool.
|
| 337 |
+
I'll call it with symbol='BTC'."
|
| 338 |
+
|
| 339 |
+
Agent generates Python code:
|
| 340 |
+
```python
|
| 341 |
+
btc_price = get_crypto_price("BTC", "USD")
|
| 342 |
+
print(btc_price)
|
| 343 |
+
```
|
| 344 |
+
|
| 345 |
+
Result: "BTC: $67,420 USD"
|
| 346 |
+
|
| 347 |
+
Agent then might:
|
| 348 |
+
"User also asked if they should invest. I should do more research.
|
| 349 |
+
Let me search for recent Bitcoin news and analysis."
|
| 350 |
+
|
| 351 |
+
Agent generates:
|
| 352 |
+
```python
|
| 353 |
+
news = web_search("Bitcoin investment analysis 2025")
|
| 354 |
+
print(news)
|
| 355 |
+
```
|
| 356 |
+
|
| 357 |
+
Agent synthesizes final answer:
|
| 358 |
+
"Bitcoin is currently $67,420. Recent analysis suggests...
|
| 359 |
+
[summary of research]"
|
| 360 |
+
```
|
| 361 |
+
|
| 362 |
+
**No retraining needed.** The model learns to use the tool from its name, description, and parameter hints.
|
| 363 |
+
|
| 364 |
+
---
|
| 365 |
+
|
| 366 |
+
## π The MCP Server Universe
|
| 367 |
+
|
| 368 |
+
### What Are MCP Servers?
|
| 369 |
+
|
| 370 |
+
MCP (Model Context Protocol) servers are **pre-built tool packages** that expose tools in a standard format. Think of them like "apps" for your agent.
|
| 371 |
+
|
| 372 |
+
There are **1000+ MCP servers** covering every domain:
|
| 373 |
+
|
| 374 |
+
| Category | Example Servers | What They Do |
|
| 375 |
+
|----------|----------------|--------------|
|
| 376 |
+
| **Web** | firecrawl, browser-use, playwright | Web scraping, browsing |
|
| 377 |
+
| **Code** | github, git, code-index | Repo analysis, code search |
|
| 378 |
+
| **Data** | postgres, sqlite, duckdb | Database queries |
|
| 379 |
+
| **Memory** | chroma, mem0 | Long-term memory |
|
| 380 |
+
| **Comm** | slack, gmail, discord | Messaging |
|
| 381 |
+
| **Dev** | kubernetes, docker, aws | Infrastructure |
|
| 382 |
+
| **Creative** | comfyui, image-gen | Image/video generation |
|
| 383 |
+
| **Research** | perplexity, arxiv | Academic search |
|
| 384 |
+
|
| 385 |
+
### How to Use MCP Servers
|
| 386 |
+
|
| 387 |
+
```python
|
| 388 |
+
# Install MCP server
|
| 389 |
+
# npm install -g @modelcontextprotocol/server-filesystem
|
| 390 |
+
|
| 391 |
+
# In Python, use the MCP client
|
| 392 |
+
from mcp import ClientSession, StdioServerParameters
|
| 393 |
+
from mcp.client.stdio import stdio_client
|
| 394 |
+
|
| 395 |
+
# Connect to MCP server
|
| 396 |
+
server_params = StdioServerParameters(
|
| 397 |
+
command="npx",
|
| 398 |
+
args=["-y", "@modelcontextprotocol/server-filesystem", "/home/user"]
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
async with stdio_client(server_params) as (read, write):
|
| 402 |
+
async with ClientSession(read, write) as session:
|
| 403 |
+
# List available tools
|
| 404 |
+
tools = await session.list_tools()
|
| 405 |
+
|
| 406 |
+
# Call a tool
|
| 407 |
+
result = await session.call_tool("read_file", {"path": "/home/user/doc.txt"})
|
| 408 |
+
```
|
| 409 |
+
|
| 410 |
+
**The MCP server exposes its tools as Python functions that smolagents can use.**
|
| 411 |
+
|
| 412 |
+
---
|
| 413 |
+
|
| 414 |
+
## ποΈ Dynamic Tool Loading: The "Plugin System"
|
| 415 |
+
|
| 416 |
+
### Architecture for Loading Tools at Runtime
|
| 417 |
+
|
| 418 |
+
```python
|
| 419 |
+
# tool_loader.py
|
| 420 |
+
import os
|
| 421 |
+
import importlib
|
| 422 |
+
from smolagents import tool, CodeAgent
|
| 423 |
+
|
| 424 |
+
def load_tools_from_directory(directory: str):
|
| 425 |
+
"""Dynamically load all tools from a directory."""
|
| 426 |
+
tools = []
|
| 427 |
+
|
| 428 |
+
for filename in os.listdir(directory):
|
| 429 |
+
if filename.endswith('_tools.py'):
|
| 430 |
+
module_name = filename[:-3] # Remove .py
|
| 431 |
+
module = importlib.import_module(f"tools.{module_name}")
|
| 432 |
+
|
| 433 |
+
# Find all @tool decorated functions
|
| 434 |
+
for attr_name in dir(module):
|
| 435 |
+
attr = getattr(module, attr_name)
|
| 436 |
+
if hasattr(attr, '_is_smolagents_tool'):
|
| 437 |
+
tools.append(attr)
|
| 438 |
+
|
| 439 |
+
return tools
|
| 440 |
+
|
| 441 |
+
# Usage
|
| 442 |
+
custom_tools = load_tools_from_directory('./tools')
|
| 443 |
+
|
| 444 |
+
agent = CodeAgent(
|
| 445 |
+
model=my_model,
|
| 446 |
+
tools=custom_tools + [read_file, write_file], # Core + custom
|
| 447 |
+
)
|
| 448 |
+
```
|
| 449 |
+
|
| 450 |
+
### Tool Configuration File
|
| 451 |
+
|
| 452 |
+
Users can enable/disable tools via a config:
|
| 453 |
+
|
| 454 |
+
```json
|
| 455 |
+
{
|
| 456 |
+
"agent_name": "My Mini-Manus",
|
| 457 |
+
"enabled_tools": [
|
| 458 |
+
"core:read_file",
|
| 459 |
+
"core:write_file",
|
| 460 |
+
"core:execute_shell",
|
| 461 |
+
"web:fetch_webpage",
|
| 462 |
+
"web:web_search",
|
| 463 |
+
"analysis:analyze_csv",
|
| 464 |
+
"creative:generate_image",
|
| 465 |
+
"mcp:github",
|
| 466 |
+
"mcp:slack"
|
| 467 |
+
],
|
| 468 |
+
"max_iterations": 10,
|
| 469 |
+
"model": "muhammadtlha944/MCP-Agent-1.7B"
|
| 470 |
+
}
|
| 471 |
+
```
|
| 472 |
+
|
| 473 |
+
---
|
| 474 |
+
|
| 475 |
+
## π Tool Complexity vs Model Capability
|
| 476 |
+
|
| 477 |
+
### What a 1.7B Model Can Handle
|
| 478 |
+
|
| 479 |
+
| Tool Complexity | Can Use? | Notes |
|
| 480 |
+
|----------------|----------|-------|
|
| 481 |
+
| **Simple function** (1 param, 1 return) | β
Yes | Easy β model gets it from description |
|
| 482 |
+
| **Multi-param function** (3-5 params) | β
Yes | With clear descriptions |
|
| 483 |
+
| **Chain of 2-3 tools** | β
Yes | With ReAct loop |
|
| 484 |
+
| **Chain of 5+ tools** | β οΈ Maybe | Depends on context length |
|
| 485 |
+
| **Complex logic** (loops, if/else) | β
Yes | CodeAgent handles this well |
|
| 486 |
+
| **API calls with auth** | β
Yes | If keys are pre-configured |
|
| 487 |
+
| **Browser automation** | β
Yes | With Helium/Selenium abstraction |
|
| 488 |
+
| **Vision/image understanding** | β οΈ Maybe | Needs vision model (adds VRAM) |
|
| 489 |
+
| **Real-time streaming** | β No | Too complex for 1.7B |
|
| 490 |
+
| **Multi-agent coordination** | β οΈ Maybe | smolagents multi-agent can help |
|
| 491 |
+
|
| 492 |
+
### Rule of Thumb
|
| 493 |
+
|
| 494 |
+
If you can describe the tool in 2-3 sentences and it has 1-5 parameters,
|
| 495 |
+
a 1.7B model can learn to use it from the description alone.
|
| 496 |
+
|
| 497 |
+
---
|
| 498 |
+
|
| 499 |
+
## π The Complete Architecture
|
| 500 |
+
|
| 501 |
+
```
|
| 502 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 503 |
+
β User Interface β
|
| 504 |
+
β (Gradio Web App) β
|
| 505 |
+
βββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
|
| 506 |
+
β
|
| 507 |
+
βΌ
|
| 508 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 509 |
+
β Agent Controller β
|
| 510 |
+
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
|
| 511 |
+
β β CodeAgent (Qwen3-1.7B) β β
|
| 512 |
+
β β β β
|
| 513 |
+
β β System Prompt: β β
|
| 514 |
+
β β "You are an AI assistant. Use available tools β β
|
| 515 |
+
β β to solve problems. Write Python code." β β
|
| 516 |
+
β β β β
|
| 517 |
+
β β Memory: Conversation history + tool results β β
|
| 518 |
+
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
|
| 519 |
+
β β β
|
| 520 |
+
β ββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ β
|
| 521 |
+
β β βΌ β β
|
| 522 |
+
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
|
| 523 |
+
β β β Tool Registry β β β
|
| 524 |
+
β β β β β β
|
| 525 |
+
β β β Core Tools Custom Tools β β β
|
| 526 |
+
β β β ββ read_file ββ get_weather β β β
|
| 527 |
+
β β β ββ write_file ββ fetch_webpage β β β
|
| 528 |
+
β β β ββ list_dir ββ analyze_csv β β β
|
| 529 |
+
β β β ββ shell_exec ββ generate_image β β β
|
| 530 |
+
β β β ββ web_search ββ create_presentation β β β
|
| 531 |
+
β β β ββ python_exec ββ [user adds more!] β β β
|
| 532 |
+
β β β β β β
|
| 533 |
+
β β β MCP Servers (external): β β β
|
| 534 |
+
β β β ββ github-mcp-server β β β
|
| 535 |
+
β β β ββ slack-mcp-server β β β
|
| 536 |
+
β β β ββ [any MCP server] β β β
|
| 537 |
+
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
|
| 538 |
+
β β β β β
|
| 539 |
+
β β βΌ β β
|
| 540 |
+
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
|
| 541 |
+
β β β Tool Implementations β β β
|
| 542 |
+
β β β β β β
|
| 543 |
+
β β β Python Libraries: β β β
|
| 544 |
+
β β β ββ requests (HTTP) β β β
|
| 545 |
+
β β β ββ pandas (data) β β β
|
| 546 |
+
β β β βοΏ½οΏ½ matplotlib (charts) β β β
|
| 547 |
+
β β β ββ selenium/helium (browser) β β β
|
| 548 |
+
β β β ββ diffusers (image gen) β β β
|
| 549 |
+
β β β ββ [any Python library!] β β β
|
| 550 |
+
β β β β β β
|
| 551 |
+
β β β System Tools: β β β
|
| 552 |
+
β β β ββ git β β β
|
| 553 |
+
β β β ββ ffmpeg β β β
|
| 554 |
+
β β β ββ [any CLI tool!] β β β
|
| 555 |
+
β β βββββββββββββββββββββββββββββββββββββββββββββββ β β
|
| 556 |
+
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
|
| 557 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 558 |
+
```
|
| 559 |
+
|
| 560 |
+
---
|
| 561 |
+
|
| 562 |
+
## π Summary: Adding Tools Is Just Python
|
| 563 |
+
|
| 564 |
+
| Step | What You Do | Time |
|
| 565 |
+
|------|-------------|------|
|
| 566 |
+
| 1 | Write a Python function with `@tool` decorator | 5 min |
|
| 567 |
+
| 2 | Write a good docstring (this teaches the LLM!) | 5 min |
|
| 568 |
+
| 3 | Add it to your agent's tools list | 1 min |
|
| 569 |
+
| 4 | Test it | 5 min |
|
| 570 |
+
| **Total** | | **16 min per tool** |
|
| 571 |
+
|
| 572 |
+
**No retraining. No model changes. Just write Python.**
|
| 573 |
+
|
| 574 |
+
---
|
| 575 |
+
|
| 576 |
+
## π Key Takeaways
|
| 577 |
+
|
| 578 |
+
1. **Tools are just Python functions** β write them, decorate with `@tool`, done
|
| 579 |
+
2. **The LLM learns from docstrings** β the description teaches the model how to use it
|
| 580 |
+
3. **No retraining needed** β add/remove tools anytime
|
| 581 |
+
4. **MCP servers = pre-built tools** β 1000+ available, install and use
|
| 582 |
+
5. **CodeAgent writes Python to use tools** β more flexible than JSON tool calls
|
| 583 |
+
6. **1.7B model handles 90% of tools** β anything with clear description + 1-5 params
|
| 584 |
+
7. **Dynamic loading** β tool marketplace concept: enable/disable tools via config
|
| 585 |
+
|
| 586 |
+
---
|
| 587 |
+
|
| 588 |
+
## π How This Changes Our Project
|
| 589 |
+
|
| 590 |
+
### Original Plan
|
| 591 |
+
- Train model to generate JSON tool calls (MCP format)
|
| 592 |
+
- Build manual ReAct loop
|
| 593 |
+
- Hardcode tool registry
|
| 594 |
+
- Limited to trained tools
|
| 595 |
+
|
| 596 |
+
### New Plan (Based on Research)
|
| 597 |
+
- Train model to solve problems by writing Python (it already knows Python!)
|
| 598 |
+
- Use smolagents CodeAgent (handles ReAct loop)
|
| 599 |
+
- Dynamic tool registration via `@tool` decorator
|
| 600 |
+
- Unlimited tools β add any Python function anytime
|
| 601 |
+
- Leverage 1000+ MCP servers
|
| 602 |
+
- Use built-in GradioUI for the web app
|
| 603 |
+
|
| 604 |
+
### Training Focus Changes
|
| 605 |
+
|
| 606 |
+
**Instead of teaching:** "Generate JSON tool calls in MCP format"
|
| 607 |
+
**We teach:** "Break problems into steps, write Python code, use available functions"
|
| 608 |
+
|
| 609 |
+
**Benefits:**
|
| 610 |
+
- Less training data needed (model already knows Python)
|
| 611 |
+
- More flexible (any tool works, not just trained ones)
|
| 612 |
+
- Easier to add tools later (just write Python)
|
| 613 |
+
- More natural for the model (code is easier than JSON schemas)
|
| 614 |
+
|
| 615 |
+
---
|
| 616 |
+
|
| 617 |
+
*This is the final piece of our planning. You now have the complete picture: vision, research, architecture, training, dataset, execution plan, tool ecosystem, and dynamic tool loading.*
|
| 618 |
+
|
| 619 |
+
**When you're ready: say "START" and we build! π**
|