Agents and MCP in practice
Designing tools agents can use well, how the Model Context Protocol (MCP) connects agents to tools and data, managing state and memory, keeping agents bounded, and testing what an agent actually did, step by step.
🎯 By the end of this lesson you will be able to:
- Design clear, safe tools with good names, descriptions and validation
- Explain MCP hosts, clients and servers, and what servers can expose
- Bound and test agents: limits, approvals, state, and evaluating whole trajectories
In this lesson
Tools are most of the work
In Lesson 10 you saw how agents loop through deciding, acting and observing. In practice, an agent's quality depends less on the model and more on its tools: how clearly they are named and described, what they return, and how safely they behave. Models choose tools by reading their names and descriptions, so a vague tool is as confusing to an agent as a vague function is to a new developer.
Designing good tools
| Principle | Instead of… | Prefer… |
|---|---|---|
| Specific names | do_action(type, data) | create_support_ticket(title, priority, customer_id) |
| Clear descriptions | "Gets data" | "Returns the 20 most recent orders for one customer, newest first" |
| Validated inputs | Any string accepted | Typed fields, allowed values, length limits; reject and explain errors |
| Useful outputs | Huge raw payloads | Concise results with the fields the agent needs, plus clear error messages |
| Safe by default | One tool that reads and deletes | Separate read-only tools; destructive tools need approval |
| Idempotent where possible | Retrying creates duplicates | A request ID so repeating a call is harmless |
The Model Context Protocol (MCP)
Before MCP, every AI application connected to every tool in its own custom way. MCP is an open standard, introduced by Anthropic in 2024 and now supported by many AI tools, that defines one common way for AI applications to connect to tools and data. Build an MCP server for your system once, and any MCP-compatible application can use it.
- Host: the AI application the user works in, such as a desktop assistant, an IDE or your own agent.
- Client: the connector inside the host that talks to one server.
- Server: a small program that exposes a system to AI. It can offer tools (actions), resources (data to read, such as files or records) and prompts (reusable templates).
- Transports: servers can run locally on the user's machine or remotely over HTTP.
State, memory and context
- Working context: the conversation, tool results and plan for the current task. Keep it lean, because long tool outputs fill the context window and confuse the model.
- Task state: track progress outside the model, such as which steps are done, so a long task can resume after an error.
- Long-term memory: facts kept between sessions, such as user preferences. Store them deliberately, let users see and delete them, and never store secrets.
Keeping agents bounded
- Step, time and cost limits for every run, with a clear message when a limit stops the agent.
- Approval gates before irreversible or external actions, showing the user exactly what will happen.
- Scoped credentials per tool and per user, not one powerful shared key.
- A complete trace of every step: the model's decision, the tool called, inputs, outputs and timing.
- Start with one agent and a small toolset. Multi-agent systems add flexibility but also coordination problems and cost, so use them only when one agent clearly isn't enough.
Testing agents
Checking only the final answer is not enough, because an agent can reach a right answer the wrong way, such as calling an expensive tool ten times or reading data it shouldn't. Evaluate the trajectory, the sequence of steps, as well as the result.
| Check | Example |
|---|---|
| Task success | Was the goal achieved correctly? |
| Tool choice | Did it use the right tools in a sensible order? |
| Efficiency | Number of steps, tokens and time compared with a good run |
| Safety | No forbidden tools, no actions without approval, no data outside scope |
| Recovery | Behaviour when a tool fails, returns nothing or returns misleading text |
What this means for your role
Developer
Write tools like public APIs: specific, typed, validated, documented and idempotent. Expose internal systems through MCP servers with scoped credentials, and log full traces.
Tester / QA
Build trajectory tests: scripted tool failures, empty results, injected instructions in tool outputs and approval bypass attempts. Check steps and cost, not just the answer.
Business Analyst / PO
Define each tool's purpose, inputs, outputs and the business rules for approval. Clear tool specs are clear requirements.
Tech Lead / Architect
Decide which systems get MCP servers, who owns them, which credentials they use, and the review process for adding new tools or third-party servers.
Project / Delivery Manager
Start agent projects with a narrow goal and a small toolset. Measure success rate, cost per task and human interventions before widening scope.
Scrum Master / Agile
Agent work needs fast feedback loops. Review traces as a team regularly, the way you would review production incidents.
Try it: spec three tools
- Pick a system your team uses, such as the ticket tracker.
- Write specs for three tools: a name, a one-sentence description, typed inputs with allowed values, and the output fields.
- Mark each tool read-only or write, decide which need approval, and write one helpful error message for each.
Key takeaways
- Agent quality depends heavily on tool design: specific names, clear descriptions, validation, concise outputs and safe defaults.
- MCP is an open standard connecting AI hosts to servers that expose tools, resources and prompts.
- Keep context lean, track task state outside the model, and handle long-term memory deliberately.
- Bound agents with limits, approvals, scoped credentials and traces, and test whole trajectories, not only final answers.
Key terms
- MCP server
- A program that exposes a system's tools, resources and prompts to AI applications using the Model Context Protocol.
- Idempotent
- Safe to repeat: doing it twice has the same effect as doing it once.
- Trajectory
- The full sequence of steps an agent took to complete a task.
- Trace
- A detailed log of an agent run: decisions, tool calls, inputs, outputs and timing.
Check your understanding
Answer all 5 questions. Score 3 or more to complete the lesson.
1. Which tool design is best for an agent?
2. In MCP, what does a server expose?
3. Why should tool errors return clear messages?
4. An agent reached the right answer but called an expensive tool 15 times. Which evaluation catches this?
5. What is a sensible rule for third-party MCP servers?