Skip to content
Inference Academy

Lab: an agent with MCP tools, guards and trajectory checks

Put the course together: an agent loop that talks to the MCP server from the first lesson, with an allow-list, step limit, repeated-call detection and a token budget, a printed trace for every run, and trajectory checks that test which tools were used, not just the final words. Then swap the scripted model for your local server.

By

20 min · verified 2026-11Hands-on labPro
Share

This lab assembles the pieces of the course into a small, honest agent: MCP for tools, guards from frameworks compared, a budget from cost and latency budgets, and checks in the spirit of the RAG evaluation loop. The model is scripted so the lab runs offline and gives the same output every time; at the end you replace it with your own local model.

#What you will build

  1. 1
    Scripted modeldecides: call a tool or answer
  2. 2
    Agent loopguards: allow-list, step limit, repeat detection, token budget
  3. 3
    MCP clientinitialize, tools/call
  4. 4
    MCP serversearch_notes, add (from lesson 1)
  5. 5
    Checksexpected tools and a key fact per task

Four tasks exercise the guards:

Task What the model does Expected outcome
refund searches notes, then answers Answer contains "7 days", used search_notes
looping repeats the same search Stopped by repeat detection
forbidden asks for a tool that is not allowed Stopped by the allow-list, nothing sent
maths calls add, then answers Answer contains "1545", used add

#Setup

Put agent_lab.py next to mcp_minimal.py (both are in the course code under code/llm_apps/). No packages are needed.

Pro content

Keep reading with Inference Academy Pro

Unlock this full lab, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lab

  1. The code
  2. Run it
  3. Step 2: break it on purpose
  4. Step 3: plug in a real model
  5. Step 4: extend the evaluation
  6. What to hand in
  7. Going further

You will be able to answer

  • The scripted 'looping' task calls search_notes with the same arguments again. What stops it?
  • Why is the allow-list enforced in the agent loop rather than trusted to the MCP server?
  • What does a trajectory check test that a final-answer check misses?
  • When you replace the scripted model with a real one, what should stay the same?
  • Which result should count as a pass for the 'forbidden' task?