13 Building and consuming MCP servers

 

This chapter covers

  • The purpose and architecture behind the Model Context Protocol (MCP)
  • Building and exposing your own MCP server, with a practical weather tool example
  • Testing and consuming MCP servers and related tools in applications
  • Integrating remote MCP tools into agents alongside local tools

Building AI agents that can effectively access and use external context is a central challenge for application developers. Previously, integrating context from different sources meant wrapping each one as a tool, following specific protocols—a time-consuming and repetitive process repeated by countless teams. Imagine if, instead, data and service providers could expose their resources as ready-made tools, instantly available for any agent or application. This is the promise of the Model Context Protocol (MCP), introduced by Anthropic.

MCP defines a standardized way for services to expose “tools” through MCP servers. Agents, or “MCP hosts,” connect to these servers via MCP clients, discovering and using remote tools as easily as local ones. This approach moves much of the integration work to where it belongs—at the source—allowing developers to focus on building more capable agents rather than reinventing the same plumbing. Once the connection is set up, tools from MCP servers work seamlessly with existing agent architectures.

13.1 Introduction to MCP Servers

13.1.1 The Problem: Context Integration at Scale

13.1.2 The Solution: The Model Context Protocol (MCP)

13.1.3 The MCP Ecosystem

13.2 How to Build MCP Servers

13.2.1 Essential Resources for MCP Server Development

13.2.2 Official Language-Specific MCP SDKs

13.2.3 Consuming MCP Servers in LLM Applications and Agents

13.3 Building a Weather MCP Server

13.3.1 Implementing the MCP Server

13.3.2 Trying out the MCP Server with MCP Inspector

13.3.3 Consuming the MCP Server from a Test MCP Host

13.4 Integrating the Weather MCP Tool into an Agent

13.4.1 Preparing the Travel Agent for Live Weather Data

13.4.2 Integrating the AccuWeather MCP Tool

13.4.3 Updating the Agent Chat Loop

13.4.4 Combining Local and Remote Tools

13.4.5 Testing and Verification

13.4.6 Using the Agent for Complex Queries

13.5 Summary