Agents: a quote-and-order agent over the MCP server¶
Run it, don't just read it
Every lab runs on your laptop with Ollama. Set up once (INSTALL), then:
git clone https://github.com/JSWOne/ai-learning.git && cd ai-learning
cd agents && python 01_agent_loop.py
Lab files and data: agents/ on GitHub. In VS Code, open the repo folder and use Terminal > Run Task > Run this lab.
Small, runnable examples of single agents, using the B2B catalog MCP server from ../mcp-server as the agent's tools. Each numbered file shows one pattern.
New to agents? Read learn-agents.md first: what it is and why, the mental model in code and diagrams, the labs in three stages, key decisions and advanced topics.
Before you start: llm-api and mcp-server: the agent's tools come from the catalog server. After it, you can: run a goal over many steps, with budgets, checks and human approval.
Done with the labs? Try the three exercises and the build-it-yourself task in PRACTICE.md.
Everything is hand-rolled first (agent/, start with agent/loop.py), so you can see every moving part. Lab 07 rebuilds the same agent in LangGraph to show what a framework adds.
Setup¶
Install steps for all topics are in ../INSTALL.md. This topic reuses ../mcp-server (its catalog server and host); the root requirements.txt covers both. From the ai-learning folder:
source .venv/bin/activate # the one venv at ai-learning/ (see ../INSTALL.md section 4)
uv pip install -r requirements.lock # all topics, the tested versions
cd agents && pytest -q && python 01_agent_loop.py
Run every example from this folder. 06 and 07 use the catalog HTTP server: the first run starts it in the background and it keeps running, so a quote made by start is still there at approve. Stop it (fresh stock and credit) with cd ../mcp-server && python -m b2b_catalog.http_app --stop. 04 asks for approval with input(), so run it in a terminal.
Files¶
Track: Core labs are the Core path through the course. Before you ship: safety lessons to do before anything you build goes to real users. Go further: when your work needs that pattern (see Use it when).
| File | Track | Pattern | Use it when | Time |
|---|---|---|---|---|
01_agent_loop.py |
Core | A goal, the loop, a readable trace and a stop reason | You want to see what makes it an agent | ~1 min |
02_plan_then_act.py |
Go further | ReAct vs plan-then-act: a JSON plan, reviewed in code, then executed and compared | Long, costly or risky tasks you want to see before they run | ~2 min |
03_self_check.py |
Core | Amounts and IDs in the answer must appear in tool results; one fix, else hold | Anything a buyer will act on | ~1-2 min |
04_memory.py |
Go further | Multi-turn session: trimmed history plus typed working memory (latest quote, order) | "Add to that quote", "where is my order?" | ~1-2 min |
05_budgets_and_recovery.py |
Before you ship | Flaky and dead tools, safe vs unsafe retries, turn and tool budgets | Real backends that time out | ~2-3 min |
06_pause_and_resume.py |
Core | Stop at a write, save to a file, approve or reject later in a new process | Approvals that come from someone else, later | ~1 min |
07_langgraph.py |
Go further | The same agent as a LangGraph graph: interrupt, SQLite checkpoints, history, a check node | Deciding whether you need a framework | ~1 min |
Shared code:
| File | What it holds | Read? |
|---|---|---|
agent/loop.py |
run(): the loop, budgets and stop reasons, the pause for approval |
Read |
agent/state.py |
AgentState (save, load, decide), Budget, Step |
Read |
agent/memory.py, agent/plan.py, agent/checks.py |
Working memory and history trimming; the plan as JSON; check_grounded() |
Read with labs 04, 02, 03 |
agent/tools.py, agent/approval.py |
McpTools, call_with_retry(); describe_call() |
Skim |
agent/printing.py |
print_trace() |
Skip: plumbing |
lg_agent.py |
build_graph(): agent, approve, tools and check nodes in LangGraph |
Read with lab 07 |
../mcp-server/host.py |
connect(), the servers, MCP-to-tool-spec translation (reused as-is) |
Skip: mcp-server |
Labs import everything from the package (from agent import run, AgentState). Each lab's docstring names the functions to read first.
Exercises to practise with: PRACTICE.md (your own files go in my-work/). Questions and answers are in FAQ.md. The learning guide is learn-agents.md (diagrams in diagrams/).
Rules of thumb¶
- The model chooses the next step; the harness owns everything else: limits, retries, approval, memory, checks and the trace.
- Every run ends with a named status:
done,stoppedwith a reason, orwaiting_approval. Never just "it stopped". - Budget every run: model turns, tool calls, time, and repeated identical calls. Check budgets before running calls, not after.
- Retry only what is safe to repeat (reads, idempotent writes). Never retry business errors or a human's "no". Tell the model plainly not to retry.
- Keep facts (quote ID, order ID, totals) in typed state captured from tool results, not in the chat transcript. Trim history only at a user turn.
- Arithmetic and money belong in tools. Check every amount and ID in the answer against tool results, in code.
- Approval is a saved state, not an
input(). Show the approver lines and totals, not just an ID. Nothing with side effects runs before the pause point. - Plan first when the job is long, costly or needs sign-off; otherwise ReAct is simpler. Re-plan when a step surprises you.
- Hand-roll to learn and for simple loops. Use a framework (LangGraph) for durable pauses, branching or many agents. It organises the loop; it doesn't make it correct.
Tests¶
pytest -q runs model-free tests with a scripted fake model: the loop, budgets, loop detection, safe vs unsafe retries, pause/save/resume, declined calls, the grounding check, history trimming, planning, the real catalog server from ../mcp-server and LangGraph interrupt and resume.
How qwen3:8b actually behaves (which steps it picks, whether it passes the checks) can only be seen by running the labs against a real model.