Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Set-up · 15 minutes

Set up your lab for securing AI agents

After this lesson you can
  • Create the lab folder and its sandbox for this course
  • Check that Python and the local model are ready
  • Run a check script that proves the lab works
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.

What you need

  • A Linux computer with Python 3.12 or newer. The course uses only the Python standard library, so you install no package.
  • The course "Python for security work". The code of this course uses functions, dictionaries, try, with and classes. Lessons that need more explain it where it first appears.
  • The course "How LLMs and agents work". Its lesson 3 installs Ollama and the model llama3.2:3b, and prints the helper llm.py that lesson 1 of this course uses. Do that course first.
  • About 3 GB of free disk space for the model, and a computer with no GPU is enough.

Everything in this course is defensive. You attack only your own lab agent. It works in one sandbox folder with harmless tools: no tool touches the network or the rest of your home folder.

The lab on your computer
Lab folderYour computeragent.pysandboxOllama model12Lab folderYour computeragent.pysandboxOllama model12
  1. agent.py → Ollama model: asks the model for the next action
  2. agent.py → sandbox: reads files here, and nowhere else

Set it up

1. Check the Python version

python3 --version prints the version of Python. The first number must be 3, and the second must be 12 or more.

python3 --version
Python 3.14.4

Your version can differ.

2. Make the lab folder and the sandbox

mkdir -p (make directory, with parents) creates a folder and the folders above it, and gives no error if they exist. The folder ~/krlab/securing-agents-mcp is the lab. Its sub-folder sandbox is the only place where the agent may work.

mkdir -p ~/krlab/securing-agents-mcp/sandbox
cd ~/krlab/securing-agents-mcp
pwd
/home/you/krlab/securing-agents-mcp

3. Start the model server

Ollama runs as a service on most systems, and then it is already running. To see whether the model server answers, run the check script of the next section. If it says that the server does not answer, start Ollama as the course "How LLMs and agents work" shows.

4. Write the check script

Create labcheck.py in the lab folder. The module urllib.request is the standard library way to open a web address, and json reads the answer. Ollama lists its models at /api/tags. The script prints one line for each requirement and ends with a count. sys.exit ends the program with the code 0 when all four checks pass, and with 1 when one fails.

labcheck.py

"""labcheck.py: checks the lab of this course. Standard library only."""
import json
import os
import sys
import urllib.request
from pathlib import Path

LAB = Path.home() / "krlab" / "securing-agents-mcp"
SERVER = os.environ.get("KR_OLLAMA", "http://127.0.0.1:11434")  # the same address as llm.py uses
MODEL = "llama3.2:3b"
passed = 0


def report(ok, text):
    global passed
    passed += ok
    print(("ok   " if ok else "FAIL ") + text)


report(sys.version_info >= (3, 12), f"Python {sys.version_info.major}.{sys.version_info.minor} (3.12 or newer)")
report((LAB / "sandbox").is_dir(), "sandbox folder exists")
try:
    with urllib.request.urlopen(SERVER + "/api/tags", timeout=3) as reply:
        models = [m["name"] for m in json.load(reply)["models"]]
    up = True
except (OSError, ValueError, KeyError):  # no answer, or an answer that is not the list of models
    up, models = False, []
report(up, "model server answers")
report(MODEL in models, f"model {MODEL} is present")
print(f"lab ready: {passed} of 4")
sys.exit(0 if passed == 4 else 1)

Check your lab

python3 labcheck.py
ok   Python 3.14 (3.12 or newer)
ok   sandbox folder exists
ok   model server answers
ok   model llama3.2:3b is present
lab ready: 4 of 4

Your Python version in the first line can differ. The last line must be the same. Type it here.

If it fails

The line saysCause and fix
FAIL Python ... (3.12 or newer)Your Python is too old. Install a newer python3 with your system's package manager.
FAIL sandbox folder existsThe folder is missing, or you ran the script as another user. Repeat step 2 and run the script from any folder, as the same user.
FAIL model server answersOllama is not running. Start it as the course "How LLMs and agents work" shows, wait a few seconds, and run the script again.
FAIL model llama3.2:3b is presentThe server runs, but the model is not downloaded. Pull it once: ollama pull llama3.2:3b. This step needs a network and a few minutes. It was not run for this lesson.
An error that mentions a proxy or a portAnother program uses port 11434, or a proxy setting blocks the local address. Set KR_OLLAMA to the address of your model server, for example http://127.0.0.1:11434. Both labcheck.py and llm.py read it.

When the last line says lab ready: 4 of 4, go to lesson 1.