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

Core / Securing AI agents and MCP

Securing AI agents and MCP cheat sheet

Set up (lesson 0)

TaskCommand or code
Prove that the lab workspython3 labcheck.py
Point the lab at another model serverexport KR_OLLAMA=http://127.0.0.1:11434

The agent and its trace (lesson 1)

TaskCommand or code
Run the agent on a taskpython3 agent.py "task text"
Run the loop with a step limit, a check and a data markerrun(task, seed=0, max_steps=6, check=check, wrap=mark)
Test your own code with a scripted modelscripted.use({"tool": "list_files", "args": {"path": "."}}, {"final": "done"})
Read what the model saw at a stepinput_sha in the trace line
Measure the clean agentpython3 baseline.py

Hijack (lesson 2)

TaskCommand or code
Run a task N times and count a testcount(runs(task, 5, **kw), hit)
Fix the task in codeTASKS[name]["text"]
Mark a tool result as datamark(text)
Allow only the tools of the taskcheck_for(task)
Measure the three controlspython3 test_hijack.py

Arguments and paths (lesson 3)

TaskCommand or code
Test that a path stays in the sandboxinside(path)
Compare whole path parts, not textpath.resolve().is_relative_to(SANDBOX.resolve())
Make a link for a testln -s ../secret.txt sandbox/link.txt
Measure the argument checkpython3 test_misuse.py check argument check

Identity and tokens (lesson 4)

TaskCommand or code
Issue a token with a scope and a lifelabapi.issue({"tickets:read"}, ttl=60)
Compare two signatures without a timing leakhmac.compare_digest(a, b)
Give a tool one folder onlyfolder_tools("inbox")
Give a tool one token onlyticket_tool(token)
Find every use of the admin keygrep -rn "ADMIN_KEY" --include=*.py .
Measure full power and a narrow tokenpython3 test_scope.py full full power and python3 test_scope.py narrow read-only token

Memory (lesson 5)

TaskCommand or code
Add a note with its sourcememory.add(text, "user", days=7)
List the notes and the pending onespython3 memory.py review
Approve a note by its numberpython3 memory.py approve 0
Read the text that goes into the promptmemory.context()
Measure a later, clean taskpython3 test_memory.py label

Code execution (lesson 6)

TaskCommand or code
Cut a command line into a list, run nothingshlex.split(command)
Run a program with no shellsubprocess.run(["ls", name], capture_output=True, text=True)
Ask why a command is refusedshell_problem(command)
Run an allowed command in a bare environmentsafe_shell(command)
Start a command with no network (needs user namespaces)unshare -rn python3 script.py
Measure the dangerous-command testpython3 test_exec.py

MCP servers (lesson 7)

TaskCommand or code
Send one request to a server by handprintf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"server/discover"}' | python3 server_good.py
Name the version in every request"_meta": {"io.modelcontextprotocol/protocolVersion": "2026-07-28"}
Start a server and list its toolsClient("server_good.py").tools()
Show what the model will readpython3 show_tools.py
Measure a good and a poisoned serverpython3 test_mcp.py
Server does not support the version, or a legacy initialize with no _meta (the code for initialize is this lab's choice)error -32022 with data.supported: retry with one of those versions
Tool name that does not existerror -32602 (invalid params)
Probe a server that may be olderno answer, or an error that is not modern: fall back to initialize. A modern error such as -32022: do not fall back

Pins (lesson 8)

TaskCommand or code
Fingerprint a filesha256sum server_good.py
Read the tools and pin the serverpython3 vet.py approve server_good.py
Check a server against its pinspython3 vet.py check server_good.py
Start a server only through the pinsvet.connect(script)

Approval, budgets and the kill switch (lesson 9)

TaskCommand or code
Classify an actionrisk(tool, args)
Ask a human before a write or an irreversible actionapproval(ask)
Limit the calls and the secondsbudget(calls=8, seconds=300)
Run several checks, the first reason winschain(budget(), check_for(task), approval(ask))
Stop every runtouch STOP
Measure the approvalpython3 test_approval.py approval approval required

Logs and alerts (lesson 10)

TaskCommand or code
Run a task and log every steprun_logged(task_id, task)
Mask a secret in the argumentsredact(args)
Count the lines of the logwc -l trace.jsonl
Print the alertspython3 detect.py
Print the alerts of another filepython3 detect.py trace_sample.jsonl
Search the log for a planted secretgrep -c "lab-key" trace.jsonl

Remote servers (lesson 11)

TaskCommand or code
Send a request to the lab serverpost(token, "tools/list")
Token for one server, one user, some scopesrs.issue("ana", {"reports:read"}, aud=rs.URL, ttl=60)
Refuse a token made for another serverclaims["aud"] != aud raises PermissionError, status 401
Refuse a token without the scopestatus 403, WWW-Authenticate: Bearer error="insufficient_scope"
Refuse a page of another websiteOrigin not on the list: status 403
Refuse a header that differs from the bodystatus 400, error -32020
Bind a handle to its userstore owner with the handle, compare with claims["sub"]
Refuse one tool name from two serversmerge_tools(lists)

OWASP MCP Top 10 (beta)

ItemLessons
MCP01 Token Mismanagement & Secret Exposure7, 10, 11
MCP02 Privilege Escalation via Scope Creep4, 11
MCP03 Tool Poisoning7, 8
MCP04 Software Supply Chain Attacks & Dependency Tampering8
MCP05 Command Injection & Execution6
MCP06 Prompt Injection via Contextual Payloads2, 5
MCP07 Insufficient Authentication & Authorization11
MCP08 Lack of Audit and Telemetry10, 11
MCP09 Shadow MCP Servers8
MCP10 Context Injection & Over-Sharing5, 11

The locked-down agent (lesson 12)

TaskCommand or code
Run a task with every controlrun_safe(name, seed, server=None, ask=input)
Replay the attacks on both agents (two runs each, or N runs)python3 evaluate.py, python3 evaluate.py 5
Read the columnsasked: the model was fooled. ran: the damage. answered: the cost

Scenario test (lesson 13)

TaskCommand or code
Rebuild the data of the testpython3 build_lab.py
Read the system message of a scripted runscripted.seen[0][0]["content"]

Safety habits