Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 5.82 KB

File metadata and controls

65 lines (49 loc) · 5.82 KB

Chapter 03: Agents, Tools & MCP

Tip

TL;DR

  • Agents move from "chatting" to "doing" by orchestrating external tools.
  • The Model Context Protocol (MCP) is the universal bridge for AI-tool integration.
  • Agent reliability is achieved through "Action-Confirm-Execute" loops, not full autonomy.
  • Zero-trust security is mandatory when granting LLMs write access to your infrastructure.

What this chapter covers

This chapter defines the bleeding edge of AI Engineering: the transition from static text generation to autonomous, stateful systems. We explore the architecture of "Agents"—systems that use reasoning to plan tasks and call external "Tools" to execute them.

A central theme of this chapter is the Model Context Protocol (MCP). You will learn how this standard is replacing brittle, custom-built middleware, allowing you to connect any AI model to any database, API, or local file system securely. We focus on the "Human-in-the-Loop" patterns necessary to keep autonomous systems from causing non-deterministic damage to your production environments.

When you need this

  • Use this when: You need an AI system to perform actions (e.g., query a database, update a ticket, write code) rather than just answer questions.
  • Do NOT use this when: Your use case only requires simple information retrieval without the need for the model to interact with external systems.

Core Concepts

From Chat to Agency

The distinction between an LLM and an Agent is "Reasoning-Action Loops." While a chatbot follows a linear path, an Agent takes an objective, creates a multi-step plan, evaluates the outcome of each step, and adjusts its plan in real-time. This requires the model to have access to a "Toolbox"—a set of programmatic interfaces it can invoke using structured outputs (like JSON schema).

The Model Context Protocol (MCP)

In the early days of AI engineering, every tool integration required custom code. MCP standardizes this by providing a client-server architecture for tool execution. An "MCP Server" exposes available tools and resources, and the "AI Client" connects to it via a unified interface. This decoupling of the model from the integration layer is the single most important advancement for scaling autonomous workflows in 2026.

Agentic State Management

Running an agent is not a single request-response cycle. It is a stateful process. You must maintain a "Message History" that includes not just what the user said, but every tool call the agent made and the resulting system output. Managing this "execution graph" requires robust database backends and strategies like "Message Pruning" to keep the agent focused on its current objective without overflowing its context window.

Human-in-the-Loop (HITL) Patterns

Fully autonomous agents are high-risk. Enterprise-grade AI engineering relies on "Action-Confirm-Execute" patterns. For low-stakes actions (reading a file), the agent operates autonomously. For high-stakes actions (deleting a production database), the system pauses execution and requests explicit human approval. This "Sanbox-to-Production" flow is critical for maintaining security and reliability.

Common Failure Modes

1. The "Tool-Calling Hallucination" Loop

  • Symptom: Agent repeatedly calls a tool with invalid arguments or calls a tool that doesn't exist, leading to an infinite cycle of errors.
  • Root Cause: Poor tool descriptions or the model lacking the reasoning depth for the specific task.
  • Fix: Provide clear, concise "docstrings" for every tool and implement a "Maximum Turn Limit" to force the agent to stop and ask for help after N failed attempts.

2. State Explosion and Context Degradation

  • Symptom: Agent starts "losing its mind," forgetting the original goal as it gets bogged down in technical tool-call errors.
  • Root Cause: The execution graph has grown too large, filling the context window with low-signal system logs.
  • Fix: Implement "Summary-based Memory," where long tool-call sequences are condensed into a single "Summary of Actions" before being fed back into the next reasoning step.

3. Unauthorized Exfiltration (Tool Poisoning)

  • Symptom: An agent is tricked by a malicious user into exfiltrating secret data or performing unauthorized database writes.
  • Root Cause: Granting the agent "God Mode" access to tools without per-operation role-only permissions.
  • Fix: Apply the Principle of Least Privilege. Every MCP connection should have an associated "Access Token" that restricts it to only the specific data and actions required for the task.

Annotated Resources

What to read next