7 Tool Integration and MCP
This chapter explains how the Model Context Protocol helps AI systems connect to real-world tools and data sources without creating a fragile web of custom integrations. Instead of writing separate glue code for every combination of AI application and external API, MCP standardizes the interface between models and tools through a client-server pattern. External systems expose capabilities as MCP servers, while AI applications connect through MCP clients, reducing integration complexity from an exponential problem to a more modular and predictable workflow.
The chapter then walks through building a practical MCP-powered product catalog tool. Using a CSV file as a simple product database, it shows how a Python server can expose a search_products function that filters products by query, price, availability, and result limit. The tool returns structured data that language models can interpret and use in customer-facing responses. A second inventory-checking tool demonstrates how models can chain multiple tools together, such as searching for products first and then verifying whether a specific item is in stock.
The chapter also emphasizes that reliable tool integration requires thoughtful design beyond simply exposing functions. Clear tool names, descriptions, schemas, structured outputs, and consistent error responses help models decide when to call tools and how to recover from failures. It further introduces the idea of production agent harnesses, which manage state, approvals, routing, tracing, memory, and sandboxed execution around the model. The main takeaway is that MCP provides the standardized “hands” for AI systems, while secure harnesses and well-designed environments make those tools safe, reliable, and useful in production.
Without MCP (left), each application must individually integrate with each external API, creating N×M integrations. On the right, the MCP architecture shows the scalability benefits of standardized client-server interactions.
How AI discovers and uses MCP tools: once tools are registered on the server, the AI can automatically discover, reason about, and invoke them.
Comparison of poor vs well-designed MCP tool schemas. The left example shows a vague tool definition that confuses AI models, while the right example demonstrates clear naming, descriptions, and self-documenting parameters that enable intelligent tool usage.
The harness owns the agent loop, memory, and LLM communication. The sandbox executes code in isolation with no direct model access. Tasks flow right; results flow left. If the sandbox crashes, the harness recovers.
Summary
- The N×M integration problem creates exponential complexity when connecting AI applications to external systems. Five apps × ten APIs = fifty custom integrations.
- Model Context Protocol (MCP) provides a standardized way for language models to interact with external tools and APIs, eliminating custom glue code through structured schemas. MCP reduces exponential complexity to linear growth (5 apps + 10 MCP servers = 15 connectors).
- MCP tools are self-describing: LLMs can discover, understand, and call them automatically by reading their schemas from the MCP server's /tools/list endpoint.
- To implement MCP, you define Python functions that perform useful tasks, decorate them as tools using FastMCP, and run them on a server that exposes their interfaces in a machine-readable format.
- Tool design is interface design: clear names, descriptions, and parameter schemas help models understand when and how to use each tool effectively.
- Multiple MCP tools can work together through tool chaining where the model automatically determines which tools to call and in what order based on user intent.
- Production-grade MCP tools require structured error handling that distinguishes between failures (database down) and empty results (no matches found).
- Consistent response structures with success flags, error codes, and human-readable messages help models provide helpful responses even when things go wrong.
- Timeouts and input validation protect your tools from hanging indefinitely or processing invalid requests.
- Production agent systems separate the harness (control plane: agent loop, approvals, tracing, state) from the sandbox (compute plane: file I/O, shell, code execution). This pattern keeps credentials out of environments where model-generated code runs.
- Agent-legible environments use structured documentation (like AGENTS.md files) as a table of contents rather than a monolithic instruction manual, enabling progressive disclosure where agents start with a small map and follow pointers to depth.
- Agent adoption is outpacing agent security: most organizations use agents but few have full security approval. Design your MCP tools with the assumption that prompt injection and data exfiltration will be attempted.
Building Reliable AI Systems ebook for free