Basics
What this part of the system is, what it is responsible for, and the vocabulary you need before the mechanics.
1.1Definition
The one-paragraph summary, then where its responsibility starts and stops.
Tool use lets a model call your functions: it outputs a structured request, your code runs it, and the result returns to the conversation. Agents loop that pattern towards a goal. MCP standardises the connection, so one integration works across many AI clients.
- Look up live data through APIs
- Take actions such as creating tickets or bookings
- Plan multi-step tasks and recover from errors
- Work across systems through standard MCP servers
- Irreversible or costly actions without human confirmation
- Act with more permissions than the user who asked
- Trust instructions found inside tool results or documents
- Loop without limits on steps, time and cost
1.2Key concepts
Ideas this deep dive relies on, in one sentence each.
Model Context Protocol: a standard for exposing tools and data to AI applications.
Instructions hidden in content that try to steer a model into unintended actions.
1.3An operations agent in practice
A concrete example first, so the mechanics have something to attach to.
An on-call engineer asks the internal assistant: ‘Why did checkout errors spike at 14:00, and open an incident if it’s still happening?’ The assistant is an MCP host connected to MCP servers for metrics, logs, the deploy system and the incident tool.
The model calls metrics.query and sees errors rising at 13:58. It calls deploys.list and finds a payments-service release at 13:55, then logs.search for that service and finds a timeout to the payment provider introduced by a config change.
Errors are still elevated, so it proposes incidents.create with a summary, links and severity. Because creating an incident pages people, the host shows a confirmation card. The engineer approves; the incident opens with the evidence attached.
Traces record every tool call, its arguments and results. The agent had read-only access to metrics and logs and write access only to incidents, scoped to the engineer’s own permissions.
Question
Know what you are reading for. These are the questions this deep dive prepares you to answer, and the rounds where they come up.
2.1Target questions
Keep them in mind while you read the detail.
- What is MCP and why would you adopt it?
- Workflow or agent?
- How do you make an agent safe?
Model answers are in part 4, after the detail that supports them.
2.2Interview rounds
System design rounds where this component is part of a strong answer.
Detailed answer
How it actually works: the components, the states it moves through, a full run, and the code.
3.1Agent architecture with MCP
The parts and how they connect.
- Tool calling
- You describe tools with names, descriptions and JSON schemas. The model returns a tool call with arguments; your code executes it and returns the result as a message. The model never runs code itself.
- Agent loop
- Repeat: send messages and tools to the model, execute any tool calls, append results, stop when the model answers or a limit is hit. Limits on steps, time and tokens are mandatory.
- MCP
- An open protocol introduced by Anthropic in late 2024 and adopted widely since. A host application runs MCP clients that connect to MCP servers. Servers expose tools (actions), resources (data to read) and prompts (templates). Messages use JSON-RPC 2.0 over local stdio or HTTP-based transports.
- Why MCP matters
- Without a standard, every AI app writes custom integrations for every system. With MCP, a team writes one server for its system and any MCP-capable client can use it.
- Policy and approvals
- Read tools run freely within the user’s scope; write tools need confirmation; destructive tools may be excluded entirely.
3.2Key concepts
| Name | Meaning |
|---|---|
| Tool | A callable function with a schema, e.g. create_incident. |
| Resource | Readable context exposed by a server, e.g. a release record or file. |
| Host / client / server | The AI application / its connector to one server / the integration exposing capabilities. |
| Workflow vs agent | Workflows follow fixed steps with LLM calls inside; agents choose their own steps. Prefer workflows when steps are known. |
| Human in the loop | Required confirmation before consequential actions. |
3.3One agent loop over MCP
One run, step by step.
- The host, not the model, decides whether a call needs approval.
- Tool results are data; instructions inside them must not be followed blindly.
3.4Code
Working code for the core of it.
A minimal MCP server (TypeScript SDK)
1import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";2import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";3import { z } from "zod";4 5const server = new McpServer({ name: "incidents", version: "1.0.0" });6 7server.tool("create_incident",8 { title: z.string(), severity: z.enum(["sev1", "sev2", "sev3"]), summary: z.string() },9 async ({ title, severity, summary }) => {10 const inc = await incidentsApi.create({ title, severity, summary }); // runs with the caller's scopes11 return { content: [{ type: "text", text: "Created " + inc.id }] };12 });13 14await server.connect(new StdioServerTransport());Agent loop with limits (TypeScript)
1const MAX_STEPS = 8, MAX_COST_USD = 0.50;2for (let step = 0; step < MAX_STEPS; step++) {3 const res = await model.create({ messages, tools: mcpTools });4 cost += res.usage.cost; if (cost > MAX_COST_USD) return stop('budget');5 if (!res.toolCalls.length) return res.text;6 for (const call of res.toolCalls) {7 if (policy.needsApproval(call) && !(await askUser(call))) return stop('declined');8 const out = await mcp.callTool(call.name, call.args); // scoped to the user9 messages.push(res.message, { role: 'tool', toolCallId: call.id, content: truncate(out, 4000) });10 }11}12return stop('step limit');3.5Tested code, explained
A working implementation, then what each part does and why.
Never trust model output
Treat a model's reply as untrusted input. Validate it, and re-ask with the exact errors.
1export type Field = { type: "string" | "number" | "boolean"; required?: boolean; oneOf?: string[]; min?: number; max?: number };2export type Schema = Record<string, Field>;3 4export function validate(value: unknown, schema: Schema): string[] {5 if (typeof value !== "object" || value === null) return ["expected a JSON object"];6 const obj = value as Record<string, unknown>;7 const errors: string[] = [];8 for (const [key, f] of Object.entries(schema)) {9 const v = obj[key];10 if (v === undefined) {11 if (f.required) errors.push(`${key} is required`);12 continue;13 }14 if (typeof v !== f.type) errors.push(`${key} must be a ${f.type}`);15 else if (f.oneOf && !f.oneOf.includes(v as string)) errors.push(`${key} must be one of ${f.oneOf.join(", ")}`);16 else if (typeof v === "number" && ((f.min !== undefined && v < f.min) || (f.max !== undefined && v > f.max))) errors.push(`${key} is out of range`);17 }18 return errors;19}20 21/** Validate, then re-ask with the exact errors. Never pass unvalidated output downstream. */22export async function guardedCall<T>(prompt: string, llm: (p: string) => Promise<string>, schema: Schema, maxRetries = 2) {23 let ask = prompt;24 let errors: string[] = [];25 for (let attempt = 1; attempt <= maxRetries + 1; attempt++) {26 const raw = await llm(ask);27 try {28 const parsed = JSON.parse(raw) as T;29 errors = validate(parsed, schema);30 if (errors.length === 0) return { ok: true as const, value: parsed, attempts: attempt };31 } catch {32 errors = ["reply was not valid JSON"];33 }34 ask = `${prompt}\n\nYour last reply failed validation:\n- ${errors.join("\n- ")}\nReply with corrected JSON only.`;35 }36 return { ok: false as const, errors, attempts: maxRetries + 1 };37}validate(parsed, schema)- Checks type, range and allowed values, including for tool arguments.
ask = `${prompt}...`- Sends the exact errors back, which fixes most replies in one retry.
ok: false- After the retry limit, return a failure. Never pass bad output downstream.
Tested by npm run test:modules.
A safety gate before the model
It runs before the expensive model, and has to decide what happens if it fails.
1export interface Verdict {2 allow: boolean;3 categories: string[];4 reason: "safe" | "unsafe" | "blocked-category" | "classifier-error";5}6 7/** Llama Guard replies "safe" or "unsafe" followed by category codes such as "S1,S10". */8export function parseGuardOutput(text: string): { safe: boolean; categories: string[] } {9 const [first, second = ""] = text.trim().split("\n");10 const safe = first.trim().toLowerCase() === "safe";11 return { safe, categories: safe ? [] : second.split(",").map((c) => c.trim()).filter(Boolean) };12}13 14/** Runs before the main model. allowed lists categories this product accepts despite the flag. */15export async function gate(input: string, classify: (text: string) => Promise<string>, allowed: string[] = [], failOpen = false): Promise<Verdict> {16 try {17 const { safe, categories } = parseGuardOutput(await classify(input));18 if (safe) return { allow: true, categories: [], reason: "safe" };19 const blocked = categories.filter((c) => !allowed.includes(c));20 if (categories.length > 0 && blocked.length === 0) return { allow: true, categories, reason: "safe" };21 return { allow: false, categories: blocked, reason: blocked.length ? "blocked-category" : "unsafe" };22 } catch {23 // Actions fail closed. A read-only product can choose to fail open.24 return { allow: failOpen, categories: [], reason: "classifier-error" };25 }26}parseGuardOutput- Llama Guard replies safe, or unsafe plus category codes.
allowed- A medical product may accept a category that a children's product blocks.
failOpen = false- If the classifier errors, products that take actions should refuse.
Tested by npm run test:modules.
3.6Design rules
Design rules
- Start with a workflow; promote to an agent only when steps genuinely vary.
- Few, well-described tools beat many overlapping ones.
- Return compact, structured tool results; truncate large outputs.
Security
- Run tools with the end user’s permissions, never a super-user.
- Require confirmation for writes; block destructive tools.
- Treat tool output as untrusted input (prompt injection).
Operations
- Trace every step with arguments, results, tokens and cost.
- Evaluate on scripted tasks with expected tool trajectories.
Q&A
The questions from part 2, now with model answers. Try each one out loud before opening it.
4.1Interview questions
What is MCP and why would you adopt it?
Workflow or agent?
How do you make an agent safe?
Extra depth
Practical context for real projects: which technology to pick, what successful teams do, what to measure and the edge cases to plan for.
5.1Frameworks and protocols
Show detailsHide details
| Need | Options | When to pick |
|---|---|---|
| Protocols | MCP (tools, resources, prompts) · provider tool-calling APIs | MCP to expose systems once for many clients; direct tool calling inside a single app. |
| Agent frameworks | Provider agent SDKs · LangGraph · LlamaIndex · plain code | Plain code for simple loops; a framework when you need state machines, retries and persistence. |
| MCP servers | Official SDKs (TypeScript, Python, others) · existing servers for GitHub, databases, docs | Wrap internal APIs as servers owned by the teams that own the APIs. |
| Coding agents | Claude Code and similar terminal or IDE agents | Agents that read code, run commands and edit files under developer supervision. |
5.2How real products do it
Show detailsHide details
Summaries of what these teams and projects have described publicly. Check their own write-ups for detail.
| Who | What they describe |
|---|---|
| Anthropic | Introduced MCP as an open standard in November 2024, with SDKs and reference servers. |
| Industry adoption | Through 2025, major AI platforms and developer tools added MCP client support, making it a common way to connect assistants to tools. |
5.3Agent metrics
Show detailsHide details
| Metric | Why it matters |
|---|---|
| Task success rate | Did the agent achieve the goal on the evaluation set? |
| Steps and cost per task | Detects loops and inefficient plans. |
| Tool error rate by tool | Points to unclear tool descriptions or flaky APIs. |
| Approval rate | How often humans accept proposed actions. |
5.4Risks and controls
Show detailsHide details
| Case | How it is handled |
|---|---|
| Prompt injection via a ticket or web page | Untrusted content cannot authorise actions; writes need user confirmation. |
| Agent loops forever | Step, time and cost limits; detect repeated identical calls. |
| Over-privileged tools | Scope tokens per user and per server; least privilege. |
| Huge tool output floods context | Paginate and summarise in the server; truncate in the host. |