Core / Securing AI agents and MCP
Securing AI agents and MCP cheat sheet
Set up (lesson 0)
| Task | Command or code |
|---|---|
| Prove that the lab works | python3 labcheck.py |
| Point the lab at another model server | export KR_OLLAMA=http://127.0.0.1:11434 |
The agent and its trace (lesson 1)
| Task | Command or code |
|---|---|
| Run the agent on a task | python3 agent.py "task text" |
| Run the loop with a step limit, a check and a data marker | run(task, seed=0, max_steps=6, check=check, wrap=mark) |
| Test your own code with a scripted model | scripted.use({"tool": "list_files", "args": {"path": "."}}, {"final": "done"}) |
| Read what the model saw at a step | input_sha in the trace line |
| Measure the clean agent | python3 baseline.py |
Hijack (lesson 2)
| Task | Command or code |
|---|---|
| Run a task N times and count a test | count(runs(task, 5, **kw), hit) |
| Fix the task in code | TASKS[name]["text"] |
| Mark a tool result as data | mark(text) |
| Allow only the tools of the task | check_for(task) |
| Measure the three controls | python3 test_hijack.py |
Arguments and paths (lesson 3)
| Task | Command or code |
|---|---|
| Test that a path stays in the sandbox | inside(path) |
| Compare whole path parts, not text | path.resolve().is_relative_to(SANDBOX.resolve()) |
| Make a link for a test | ln -s ../secret.txt sandbox/link.txt |
| Measure the argument check | python3 test_misuse.py check argument check |
Identity and tokens (lesson 4)
| Task | Command or code |
|---|---|
| Issue a token with a scope and a life | labapi.issue({"tickets:read"}, ttl=60) |
| Compare two signatures without a timing leak | hmac.compare_digest(a, b) |
| Give a tool one folder only | folder_tools("inbox") |
| Give a tool one token only | ticket_tool(token) |
| Find every use of the admin key | grep -rn "ADMIN_KEY" --include=*.py . |
| Measure full power and a narrow token | python3 test_scope.py full full power and python3 test_scope.py narrow read-only token |
Memory (lesson 5)
| Task | Command or code |
|---|---|
| Add a note with its source | memory.add(text, "user", days=7) |
| List the notes and the pending ones | python3 memory.py review |
| Approve a note by its number | python3 memory.py approve 0 |
| Read the text that goes into the prompt | memory.context() |
| Measure a later, clean task | python3 test_memory.py label |
Code execution (lesson 6)
| Task | Command or code |
|---|---|
| Cut a command line into a list, run nothing | shlex.split(command) |
| Run a program with no shell | subprocess.run(["ls", name], capture_output=True, text=True) |
| Ask why a command is refused | shell_problem(command) |
| Run an allowed command in a bare environment | safe_shell(command) |
| Start a command with no network (needs user namespaces) | unshare -rn python3 script.py |
| Measure the dangerous-command test | python3 test_exec.py |
MCP servers (lesson 7)
| Task | Command or code |
|---|---|
| Send one request to a server by hand | printf '%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 tools | Client("server_good.py").tools() |
| Show what the model will read | python3 show_tools.py |
| Measure a good and a poisoned server | python3 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 exist | error -32602 (invalid params) |
| Probe a server that may be older | no 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)
| Task | Command or code |
|---|---|
| Fingerprint a file | sha256sum server_good.py |
| Read the tools and pin the server | python3 vet.py approve server_good.py |
| Check a server against its pins | python3 vet.py check server_good.py |
| Start a server only through the pins | vet.connect(script) |
Approval, budgets and the kill switch (lesson 9)
| Task | Command or code |
|---|---|
| Classify an action | risk(tool, args) |
| Ask a human before a write or an irreversible action | approval(ask) |
| Limit the calls and the seconds | budget(calls=8, seconds=300) |
| Run several checks, the first reason wins | chain(budget(), check_for(task), approval(ask)) |
| Stop every run | touch STOP |
| Measure the approval | python3 test_approval.py approval approval required |
Logs and alerts (lesson 10)
| Task | Command or code |
|---|---|
| Run a task and log every step | run_logged(task_id, task) |
| Mask a secret in the arguments | redact(args) |
| Count the lines of the log | wc -l trace.jsonl |
| Print the alerts | python3 detect.py |
| Print the alerts of another file | python3 detect.py trace_sample.jsonl |
| Search the log for a planted secret | grep -c "lab-key" trace.jsonl |
Remote servers (lesson 11)
| Task | Command or code |
|---|---|
| Send a request to the lab server | post(token, "tools/list") |
| Token for one server, one user, some scopes | rs.issue("ana", {"reports:read"}, aud=rs.URL, ttl=60) |
| Refuse a token made for another server | claims["aud"] != aud raises PermissionError, status 401 |
| Refuse a token without the scope | status 403, WWW-Authenticate: Bearer error="insufficient_scope" |
| Refuse a page of another website | Origin not on the list: status 403 |
| Refuse a header that differs from the body | status 400, error -32020 |
| Bind a handle to its user | store owner with the handle, compare with claims["sub"] |
| Refuse one tool name from two servers | merge_tools(lists) |
OWASP MCP Top 10 (beta)
| Item | Lessons |
|---|---|
| MCP01 Token Mismanagement & Secret Exposure | 7, 10, 11 |
| MCP02 Privilege Escalation via Scope Creep | 4, 11 |
| MCP03 Tool Poisoning | 7, 8 |
| MCP04 Software Supply Chain Attacks & Dependency Tampering | 8 |
| MCP05 Command Injection & Execution | 6 |
| MCP06 Prompt Injection via Contextual Payloads | 2, 5 |
| MCP07 Insufficient Authentication & Authorization | 11 |
| MCP08 Lack of Audit and Telemetry | 10, 11 |
| MCP09 Shadow MCP Servers | 8 |
| MCP10 Context Injection & Over-Sharing | 5, 11 |
The locked-down agent (lesson 12)
| Task | Command or code |
|---|---|
| Run a task with every control | run_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 columns | asked: the model was fooled. ran: the damage. answered: the cost |
Scenario test (lesson 13)
| Task | Command or code |
|---|---|
| Rebuild the data of the test | python3 build_lab.py |
| Read the system message of a scripted run | scripted.seen[0][0]["content"] |
Safety habits
- Allow, do not deny. A list of what is allowed has nothing to forget.
- Build a command as a list, never as a string.
- Code decides, not the prompt. A request in the prompt is text that an attack can overrule.
- Resolve a path, then compare it. Test the resolved path, never the text that the model sent.
- Give each task the least power for the shortest time. The admin key stays outside the agent.
- Pin what you approved, and compare it each time. Read the descriptions and the schemas before you pin.
- A server accepts only tokens made for it, checks the scope of each tool and the owner of each handle, and never forwards a client token. A token is never in a URL or a log.
- Mask a secret when you write the log, and log the decision, not the whole conversation.
- A tool that nobody classified counts as irreversible. A human sees the real action, not the words of the model.
- Test after every change of model, prompt, tool or control, and keep the old numbers.