Module 10 β Agents, Tools & MCPΒΆ
π§ β Building AI POCs Β· π Course home Β· NLP βΆ
Goal: Go from βan LLM that calls a toolβ (Module 8, NB 30) to production-grade agentic systems β reliable agent loops, hardened tools, the Model Context Protocol (MCP), and multi-agent orchestration. By the end you can design, build, and evaluate an agent that discovers its tools over a standard protocol and works in Claude Desktop, Claude Code, or your own app.
Estimated time: 6β8 hours.
Prerequisites: Module 8 (AI Engineering β especially NB 30 tools & agents), Modules 1β2 (functions, dicts, JSON). A real LLM is not required β every notebook runs 100% offline.
ββββββββββββββββββββββββββββββββββββββββ
β the agent control loop (NB 30) β
ββββββββββββββββββββ¬ββββββββββββββββββββ
β
ββββββββββββββββββββ¬ββββββββββββββββ΄ββββββββ¬βββββββββββββββββββββββ
βΌ βΌ βΌ βΌ
NB 37 NB 38 NB 39 NB 40
agent robust tools Model Context multi-agent
architectures (schemas, valid- Protocol (MCP): systems +
(ReAct, planning, ation, approval, host/client/server, orchestrator,
reflection, parallel, idem- JSON-RPC, tools/ routing, the
memory) potency) resources/prompts MCP capstone
One running example carries the whole module: an AI support copilot for a SaaS team. NB 37 gives it a brain (a bounded agent loop), NB 38 hardens its tools, NB 39 serves those tools over MCP, and NB 40 grows it into an orchestrated team of specialists β the moduleβs finished artifact.
Notebooks at a glanceΒΆ
# |
Notebook |
β± Time |
Difficulty |
What youβll build |
|---|---|---|---|---|
31 |
|
70β90 min |
Intermediate β Advanced |
A ReAct agent with planning, reflection & memory β plus the failure-mode guardrails |
32 |
|
70β90 min |
Intermediate β Advanced |
A |
33 |
|
80β100 min |
Advanced |
A working MCP server and client from scratch (JSON-RPC 2.0; tools, resources, prompts) |
34 |
|
80β100 min |
Advanced |
An orchestrator + specialist agents, evaluated end-to-end β the MCP-backed support copilot capstone |
Notebook guidesΒΆ
37 Β· Agent Architectures: Loops, Planning & Reflection β 37_agent_architectures.ipynbΒΆ
Notebook 30 introduced the call β execute β return loop; this lesson goes professional. The mental model for the whole module: an agent is an LLM sitting inside a while-loop with a memory and a budget β the clever model isnβt the architecture, the loop around it is. You build the support copilotβs brain against a tiny in-memory world (channel-level CSAT scores plus a calculator) and a deterministic policy() stand-in that makes the same kind of decisions a real model would, so every run is reproducible.
Concretely you implement four escalating architectures β react_agent() (Thought β Action β Observation), plan_and_execute() (decompose first, act second), reflexive_agent() (a critic that triggers a retry), and agent_with_memory() (scratchpad + episodic cache) β then map each common failure mode (loops, hallucinated tools, runaway cost) to its guardrail. Section 9 sketches the one-line swap to a real Anthropic provider.
Learning objectives:
Explain the agent control loop and why every agent needs a budget.
Implement the ReAct pattern: interleaved Thought β Action β Observation.
Add a planner that decomposes a task before acting.
Add a reflection step that critiques an answer and retries on failure.
Give an agent memory (scratchpad + episodic) and know when each matters.
Name the common failure modes (loops, hallucinated tools, runaway cost) and the guardrail for each.
Sections:
The agent control loop in one picture
Setup β a tiny world the agent can act in
The βbrainβ: a deterministic policy stand-in
ReAct: interleave Thought β Action β Observation
Planning: decompose before acting
Reflection: critique, then retry
Memory: scratchpad vs episodic
Failure modes & the guardrail for each
Going live β the real-provider sketch
Practice: 4 β quick exercises Β· 4 π§ͺ practice exercises (ββββ, incl. a Debug me π) Β· 4 π§ stretch exercises (βββ) Β· π bonus mini-project: a self-describing agent
Files/datasets: none β all data is in-memory; the βbrainβ is an offline deterministic stand-in, no API key needed (real anthropic client shown as a commented reference).
38 Β· Designing Robust Tools β 38_designing_robust_tools.ipynbΒΆ
An agent is only as good as its tools. The copilot gains one consequential new tool β estimate_refund(order_id, pct), a tool that moves money β and it becomes the stress test for the whole lesson: what happens when the model sends pct: "fifty", forgets order_id, or asks for pct: 9999? Mental model: the schema is the contract you advertise; the validator is the bouncer at the door β every argument the model sends is untrusted input.
You build the full defensive stack: a Tool dataclass (name, description, JSON-Schema parameters, function), a dependency-free validate_args() covering missing fields / wrong types / enums / ranges, a consistent ok()/err() result envelope via safe_call(), and a ToolRegistry that validates, dispatches, and logs every call. On top come tool selection among many, a human-in-the-loop approval gate (gated_call() with pluggable approvers), parallel calls (call_many()), and output bounding. The patterns map 1:1 onto OpenAI/Anthropic function-calling and onto MCP tools (NB 39).
Learning objectives:
Describe a tool with a JSON-Schema the model can read.
Validate arguments before executing β types, required fields, enums, ranges.
Return structured results and errors (never raw exceptions) in a consistent envelope.
Build a ToolRegistry that validates, dispatches, and logs every call.
Add a human-in-the-loop approval gate for sensitive tools.
Run independent tools in parallel and bound output size.
Sections:
Anatomy of a tool
Validate arguments before running
A consistent result envelope
A ToolRegistry: validate, dispatch, log
Tool selection among many
Approval gate: human-in-the-loop for dangerous tools
Parallel tool calls
Bound the output
Practice: 4 β quick exercises Β· 4 π§ͺ practice exercises (ββββ, incl. a Debug me π) Β· 4 π§ stretch exercises (βββ: idempotency keys, retry with backoff, timeouts, schema-driven coercion) Β· π bonus mini-project: a hardened tool call
Files/datasets: none β everything runs offline on in-memory data, no API key needed.
39 Β· The Model Context Protocol (MCP) β 39_model_context_protocol.ipynbΒΆ
Every framework wires tools differently β an OpenAI tool, a LangChain tool, and a Claude tool are described in three incompatible ways. MCP, open-sourced by Anthropic in late 2024, is the single open standard that fixes this: write one MCP server and it works in Claude Desktop, Claude Code, and any other MCP host. Mental model: MCP is a USB-C port for AI β it turns MΓN bespoke glue into M+N standard plugs, and the plug is just two messages: βwhat can you do?β (discover) and βdo this nowβ (invoke).
This notebook builds a working MCP server and client from scratch, in process, speaking the real protocol β JSON-RPC 2.0, the real method names (initialize β tools/list β tools/call β β¦) and message shapes β so the official SDK holds no surprises. You populate a βsupport-opsβ server (get_csat, count_tickets, a tickets resource, a triage prompt), connect MCPClient over an InProcessTransport, and finish with mcp_agent(): an agent that discovers and calls tools it never hard-coded. Section 9 maps everything onto the real mcp SDK (FastMCP) and the Claude Desktop / Claude Code host config.
Learning objectives:
Explain MCPβs host / client / server architecture and when to use it.
Name the three server primitives β tools, resources, prompts β and who controls each.
Trace the JSON-RPC lifecycle:
initializeβtools/listβtools/callβ β¦Implement a minimal MCP server and client and exchange messages over a transport.
Connect an agent to a server so it discovers and calls tools it never hard-coded.
Map the offline build onto the real
mcpSDK and a Claude Desktop / Claude Code config.
Sections:
Why MCP exists
The three primitives β and who controls them
The wire protocol: JSON-RPC 2.0
A minimal MCP server
Populate a server: the βsupport-opsβ server
The transport + client
The full lifecycle β discover, then use
Connect an agent to the MCP server
The real thing β
mcpSDK & Claude hosts
Practice: 4 β quick exercises Β· 4 π§ͺ practice exercises (ββββ, incl. a Debug me π) Β· 4 π§ stretch exercises (βββ: unknown-method errors, a logging transport, a two-server router, resource templates) Β· π bonus mini-project: a capability report
Files/datasets: none β server data (tickets, CSAT) lives in-memory and the protocol runs in process, no API key needed; the real mcp SDK (pip install "mcp[cli]") and host configs appear as commented references.
40 Β· Multi-Agent Systems & Capstone β 40_multi_agent_systems.ipynbΒΆ
Some tasks have distinct sub-jobs β analyse, then draft, then check β that are cleaner and more testable when handled by specialist agents under an orchestrator. Mental model: a multi-agent system is a team β the orchestrator is the team lead, the βwiringβ is just messages (text or dicts) passed between LLM calls, and you hire a new specialist only when the role is genuinely different. The lesson is honest about cost: it opens with a decision table for when one agent is enough.
The copilot becomes that team: a Specialist analyst that pulls the numbers, a writer that phrases them for a customer, and a human to escalate to via run_with_handoff(), coordinated by an Orchestrator that shares state on a blackboard. Section 6 is the module capstone β the analyst calls its tools through a MiniMCPServer/MiniMCPClient pair, combining NB 37βs loop, NB 38βs tool discipline, and NB 39βs protocol β and Section 7 evaluates the whole system against golden cases. The bonus project ships it all as one support_assistant(request) function: the moduleβs finished artifact.
Learning objectives:
Decide when multiple agents help β and when theyβre overkill.
Build the orchestratorβworker pattern with specialist agents.
Implement routing and handoff between agents.
Share state safely via a blackboard.
Evaluate a multi-agent system end-to-end.
Assemble a capstone: orchestrator + specialists + tools + an MCP server.
Sections:
When to use more than one agent
A specialist agent
A second specialist β the writer
The orchestrator
Routing & handoff
Capstone β an MCP-backed support-operations assistant
Evaluating a multi-agent system
Going live β the real multi-agent sketch
Practice: 4 β quick exercises Β· 4 π§ͺ practice exercises (ββββ, incl. a Debug me π) Β· 4 π§ stretch exercises (βββ: parallel specialists, budgets, capability-based routing, a blackboard log) Β· π bonus mini-project: the full assistant as one function
Files/datasets: none β all data is in-memory; specialists use offline deterministic stand-ins, no API key needed (a real-provider sketch closes the notebook).
How these notebooks workΒΆ
Every notebook runs 100% offline β the agent βbrainβ, every tool call, and every MCP message use deterministic stand-ins, so no API key or install is needed, and each lesson marks exactly where a real provider (anthropic, openai, the mcp SDK) drops in. Each notebook opens with a Colab badge and a metadata line (estimated time & difficulty), then follows the course rhythm: short sections punctuated by β Quick exercise (~2 min) checkpoints with collapsible solutions, followed by π§ͺ practice exercises (β-rated, always including a βDebug me πβ), π§ stretch exercises, a π bonus mini-project, key takeaways, and a self-assessment. The module builds directly on Modules 8β9 β especially NB 30βs tool-calling loop β and all four lessons develop the same AI support copilot, so each pattern lands on one concrete system.
Where nextΒΆ
Turn the capstone into a real FastMCP server (NB 39 Β§9) and register it with Claude Desktop (claude_desktop_config.json) or Claude Code (claude mcp add). Then carry on to Module 11 β NLP.
π Finished this module? Test yourself with the Module 10 quiz β five questions, ~10 minutes.