Lesson 278 of 296
Agents, MCP and multi-agent systems
What you will learn
- What an agent is
- The agent loop
- Guardrails for agents
- MCP (Model Context Protocol)
- Multi-agent systems
- When not to build an agent
On this page
What is an agent?
An agent is an LLM that works in a loop: it decides the next step, calls a tool, looks at the result, and repeats until the task is done or a limit is reached. Tool calling (Lesson 275) is one step. An agent is many steps chosen by the model.
Examples: a coding assistant that reads files, edits code and runs tests until they pass; a support agent that looks up an order, checks the refund policy and drafts a reply.
The agent loop
type Action =
| { type: "tool"; name: string; input: string }
| { type: "finish"; answer: string };
type Tool = (input: string) => string;
function runAgent(
decide: (observations: string[]) => Action,
tools: Record<string, Tool>,
maxSteps: number,
): { answer: string; steps: number } {
const observations: string[] = [];
for (let step = 1; step <= maxSteps; step++) {
const action = decide(observations);
if (action.type === "finish") return { answer: action.answer, steps: step };
const tool = tools[action.name];
const result = tool ? tool(action.input) : `error: unknown tool ${action.name}`;
observations.push(`${action.name}(${action.input}) -> ${result}`);
}
return { answer: "Stopped: step limit reached", steps: maxSteps };
}
// A scripted fake "model" for the example.
const tools: Record<string, Tool> = {
findOrder: (email) => (email === "sam@example.com" ? "A100" : "none"),
orderStatus: (id) => (id === "A100" ? "dispatched" : "unknown"),
};
function decide(obs: string[]): Action {
if (obs.length === 0) return { type: "tool", name: "findOrder", input: "sam@example.com" };
if (obs.length === 1) return { type: "tool", name: "orderStatus", input: "A100" };
return { type: "finish", answer: "Order A100 has been dispatched." };
}
console.log(runAgent(decide, tools, 5)); // { answer: 'Order A100 has been dispatched.', steps: 3 }Notice the important parts: a step limit, unknown tools return an error message instead of crashing, and every step is recorded so you can debug it later.
Guardrails for agents
Agents can do real damage because they act. Guardrails:
| Guardrail | Example |
|---|---|
| Least privilege | The agent can read orders but cannot delete customers |
| Step, time and cost limits | Stop after 10 steps, 60 seconds or a token budget |
| Human approval | A person confirms refunds, emails, deploys and deletes |
| Validate every tool input | Treat model output as untrusted input |
| Sandbox | Run code-executing agents in an isolated container with no secrets |
| Logging and tracing | Record each prompt, tool call and result for review |
MCP (Model Context Protocol)
Without a standard, every AI app needs custom code for every tool or data source. The Model Context Protocol (MCP) is an open standard for connecting AI applications to tools and data:
- An MCP server exposes tools (actions), resources (data to read) and prompts (templates). For example a GitHub, database or calendar server.
- An MCP client (inside an AI app, such as a chat assistant or code editor) connects to servers and lets the model use them.
Build a tool once as an MCP server, and any MCP-compatible app can use it. Anthropic introduced MCP in late 2024. In December 2025 it was donated to the Agentic AI Foundation, a fund under the Linux Foundation co-founded by Anthropic, Block and OpenAI, so it is now run as a vendor-neutral open standard.
Security note: an MCP server runs with real permissions. Only install servers you trust, give them the least access possible, and check what each tool can do.
Multi-agent systems
A multi-agent system uses several agents with different jobs, for example:
- an orchestrator that splits a task and hands out parts,
- worker agents that each research or code one part (often in parallel),
- a reviewer agent that checks the result.
Benefits: each agent has a smaller, clearer job and context; parts can run in parallel.
Costs: many more tokens, more places to fail, and harder debugging.
When not to build an agent
Start with the simplest thing that works:
- A single model call with a good prompt.
- A fixed workflow: steps you code in order (for example classify, then retrieve, then answer). Predictable and easy to test.
- An agent, only when the steps truly cannot be known in advance.
Most business features are best as fixed workflows with one or two model calls.
How to say it in an interview
"An agent is a model in a loop that chooses tool calls until the task is done. I give it a step, time and cost limit, least-privilege tools, validation on every tool input, human approval for actions that change data, and full tracing. For connecting tools I'd use MCP, the open standard now under the Linux Foundation's Agentic AI Foundation, and only trusted servers with minimal permissions. I prefer a fixed workflow when the steps are known, and use agents or multiple agents only when the task really needs open-ended steps, because they cost more and are harder to test."
Key words
| Word | Meaning |
|---|---|
| Agent | An LLM in a loop that chooses actions with tools |
| Workflow | Fixed steps written in code, with model calls inside |
| Guardrail | A limit or check that keeps an AI system safe |
| MCP | An open standard to connect AI apps to tools and data |
| MCP server / client | Exposes tools and data / uses them inside an AI app |
| Orchestrator | An agent that splits work and hands it to other agents |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.