Ep. 08 — What is JSON and how to give the first "hand" to your agent (the first Tool)
Yesterday we saw the guardian wake up at 02:17 to restart an agent that had fallen asleep with its eyes open. The problem was the heartbeat tied to network work: a call without response had frozen everything. The lesson? Separate the heart from the hands. Today we build the first hand.
Imagine you need to ask a colleague to check what's in a drawer. You don't say "go and look." You hand them a well-written note: Action: "list_files" Folder: "documents" Sort_by: "date"
That note is JSON. Nothing more than a form with boxes — key on the left, value on the right — that both of you know how to read. The human understands it at a glance. The code parses it into a Python dictionary and knows exactly what to do.
import json
import os
from pathlib import Path
# The "form" the agent fills out to request an action
richiesta = {
"azione": "elenca_file",
"cartella": "documenti",
"ordina_per": "data"
}
# The agent transforms the dictionary into JSON text (string) to send or save it
testo_json = json.dumps(richiesta, ensure_ascii=False, indent=2)
print("The note that passes from hand to hand:")
print(testo_json)
# --- On the other side: the tool receives the text and puts it back into a dictionary ---
dati = json.loads(testo_json)
# The actual tool: READ ONLY, no writing, no network
def elenca_file(cartella: str, ordina_per: str = "nome") -> list[dict]:
"""
Returns a list of dictionaries: name, size, last_modified.
Does not delete, does not move, does not execute. Only reads.
"""
base = Path(cartella).expanduser().resolve()
if not base.is_dir():
return [{"errore": f"Cartella non trovata: {cartella}"}]
elementi = []
for p in base.iterdir():
stat = p.stat()
elementi.append({
"nome": p.name,
"dimensione_bytes": stat.st_size,
"ultima_modifica": stat.st_mtime
})
# Simple sorting
reverse = ordina_per in ("data", "dimensione")
key_map = {"nome": lambda x: x["nome"].lower(),
"data": lambda x: x["ultima_modifica"],
"dimensione": lambda x: x["dimensione_bytes"]}
elementi.sort(key=key_map.get(ordina_per, key_map["nome"]), reverse=reverse)
return elementi
# Execution: the agent calls the tool with parameters extracted from JSON
risultato = elenca_file(dati["cartella"], dati.get("ordina_per", "nome"))
# The tool *always* responds in JSON (list of dictionaries -> text)
print("\nTool response (JSON again):")
print(json.dumps(risultato, ensure_ascii=False, indent=2))What happened here?
- The agent filled out a Python dictionary.
json.dumpsturned it into ordered text (the note).- The tool received the text,
json.loadsput it back into a dictionary. - The
elenca_filefunction performed read only — no side effects. - The tool responded with a list of dictionaries, serialized back into JSON.
Why all this trouble? Because text travels. The note can arrive from a Telegram chat, a config file, a database column, a webhook. The tool doesn't need to know where it comes from: it just needs to know how to read the standard form.
And here's the point. The tool we wrote is safe by design: it doesn't write, doesn't delete, doesn't phone home. But it knows how to read file names. And what if inside one of those files someone hid a trap phrase to give orders to the agent?
Where we stop today
Now your agent can read your files. But what happens if inside one of those files someone hid a trap phrase to give it orders? We'll see tomorrow.
Continue on progettosiliceo.online/tutorial-agenti.html — Ep. 09: The First Danger: what is Prompt Injection (when text deceives the agent).