AI Agents with Tools & MCP
Let a language model call your functions to get real work done: tool calling, the agent loop, well-designed tools, memory and planning, the Model Context Protocol (MCP), and the safety rules that keep an agent from doing harm.
ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។
A chatbot talks. An agent acts. Give a language model a set of tools (functions you wrote: look up a record, read a file, send a message, run a query) and let it decide which tool to call, read the result, and decide what to do next. Repeat until the job is done. That loop is what people mean by an AI agent, and it is the idea behind coding assistants, support bots that can really issue a refund, and assistants that work across your calendar and files.
This series teaches the idea from the inside. You will learn how a model asks for a tool call (it never runs anything itself; your code does), how to write the agent loop with step limits and a trace you can read, how to design tools a model can use reliably, and how agents keep notes and plan longer tasks. Then you meet the Model Context Protocol (MCP), an open standard that lets one tool server work with many agents and applications. You will build a tiny MCP server and client yourself, over the same plain messages the real ones use, and connect an agent to it.
Because an agent can act, safety is half of this series. You will see, safely and on your own machine, how text hidden inside a tool result can hijack a gullible model (prompt injection), why a file tool must be locked inside a folder, why risky actions need a human to approve them, and why untrusted tools should run in a sandbox. That is where this series connects to Docker Sandboxes Crash Course.
You build agentlab: a tool registry, an agent loop, an MCP server
and client, guards and a test harness, with zero dependencies. It runs
against a mock model that supports tool calling (including a sloppy
one that sends broken JSON and a gullible one for the security demos),
so everything is free and works without an account or API key. The mock
follows simple rules and is not intelligent, and the chapters say so
plainly; real models choose tools far better, but the messages, the
loop, the protocol and the risks are the same.
You should have finished Build a Chatbot with an LLM API, and it helps to have read RAG: Chat with Your Own Documents and the prompt-injection section of How LLMs Work. You need Node.js (the LTS version) and a code editor.
A final project chapter puts it all together: a shop assistant for a small business that checks stock, looks up orders and drafts replies, and can issue a refund only with a person's approval, a cap and an audit log. It doubles as a 45-minute workshop plan.
Every chapter ends with Quick Review: Common Questions: short, honest answers to what students and developers most often ask about agents.
- 01What Is an Agent?Learn what an AI agent really is (a model, tools, a loop and a goal), when a plain workflow is the better choice, and see one tool call done by hand with raw HTTP so the idea stops being magic.
- 02Tool Calling BasicsTurn the hand-made round trip into reusable code: describe tools with JSON Schema, parse and validate the arguments a model sends, run the tool safely, return errors as text the model can read, and handle several calls at once.
- 03The Agent LoopWrite the loop that turns tool calling into an agent: ask the model, run the tools it requests, feed the results back and repeat, with step limits, a readable trace, repeated-call detection, token tallies, cancellation and clear failure paths.
- 04Designing Good ToolsMost agent failures are tool-design failures: learn how to choose names, write descriptions the model can act on, shape schemas, return short results and helpful errors, separate reads from writes, and test every tool without a model.
- 05Memory, State and PlanningGive an agent a memory that survives long tasks: shrink the message history safely, keep notes outside the model, plan with a to-do tool, save checkpoints you can resume from, and use search as a tool.
- 06What Is MCP?Meet the Model Context Protocol: the problem it solves, the host/client/server roles, tools, resources and prompts, how stdio and HTTP carry JSON-RPC messages, and what the handshake looks like when you drive a server by hand.
- 07Build an MCP ServerWrite a working MCP server in about sixty lines: read JSON-RPC lines from stdin, answer initialize, tools/list and tools/call, keep logs off stdout, tell protocol errors from tool errors, add your own tool, and test it by hand and with code.
- 08Connect an Agent to MCPBuild the client side: launch MCP servers as child processes, turn their tools into agent tools, merge two servers with name prefixes, and run the agent from a config file, with timeouts, crashes, hangs and failed starts handled on purpose.
- 09Safety, Permissions and SandboxingLock a file tool inside a folder, watch a gullible model get hijacked by text hidden in a web page, then add the guards that matter: allowlists, human approval, limits, an audit log, minimal environments, and a sandbox.
- 10Ship an AgentTest an agent that is not deterministic, measure it, log and limit it in production, decide between a framework and your own loop, be honest about what agents can and cannot do, then recap the whole AI track with a cheat sheet and answers for a talk.
- 11Project: A Shop Assistant AgentPut the whole series to work: build a small shop assistant that checks stock, looks up orders, drafts replies and can issue a refund only with a person's approval, a cap and an audit log, then test it and plan a 45-minute workshop around it.