MCP (Model Context Protocol): Building Tool Integrations for AI
Model Context Protocol (MCP) is an open standard protocol developed by Anthropic for connecting AI assistants with external data sources and tools. Think of MCP as "USB-C for AI" — a single universal standard that allows any AI model to connect with any tool without requiring custom integrations for each combination.
In this tutorial, we will explore MCP architecture in depth, build MCP servers and clients from scratch, and create a practical example of a database query server that enables AI assistants to query PostgreSQL safely.
Why MCP Matters
Before MCP, every integration between AI and external tools required a custom implementation. If you had 5 AI models and 10 tools, you would need 50 different integrations. MCP solves this problem by providing a single standard protocol.
Key benefits of MCP:
- Standardization: One protocol for all AI-tool integrations
- Security: Granular access control over data and operations
- Modularity: Servers and clients can be developed independently
- Ecosystem: A growing community with many ready-to-use servers
- Vendor-agnostic: Not tied to any single AI provider
MCP Architecture
MCP uses a client-server architecture with three main components:
1. Host
The host is the AI application used by the end user, such as Claude Desktop, Claude Code, or an IDE with AI features. The host manages connections and provides the user interface.
2. Client
The client is a component within the host that manages a connection to one MCP server. Each client has a 1:1 connection with a single server. The client handles capability negotiation and message routing.
3. Server
The server is a program that exposes resources, tools, and prompts to clients. Servers can run locally or remotely and communicate through supported transport layers.
┌─────────────────────────────────────┐
│ HOST │
│ (Claude Desktop / Claude Code) │
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ Client 1 │ │ Client 2 │ │
│ └────┬─────┘ └────┬─────┘ │
└───────┼───────────────┼─────────────┘
│ │
┌────▼─────┐ ┌────▼─────┐
│ Server A │ │ Server B │
│ (DB) │ │ (Files) │
└──────────┘ └──────────┘
Core MCP Primitives
MCP defines three core primitives that servers can expose:
Resources
Resources are data that AI can read, such as files, database records, or API responses. Resources are identified by URIs and are read-only.
Tools
Tools are functions that AI can call to perform actions. Tools have parameters defined with JSON Schema and return results in a structured format.
Prompts
Prompts are reusable templates for AI interactions. Prompts help users accomplish common tasks in a structured way.
Installation
Python SDK
# Install using pip
pip install mcp
Or using uv (recommended)
uv add mcp
For development
uv add "mcp[cli]"
TypeScript SDK
# Install using npm
npm install @modelcontextprotocol/sdk
Or using pnpm
pnpm add @modelcontextprotocol/sdk
MCP Inspector (for testing)
# Run MCP Inspector
npx @modelcontextprotocol/inspector
Building an MCP Server with Python
Let's start by building a simple MCP server that exposes resources, tools, and prompts.
Basic Server
# server.py
from mcp.server.fastmcp import FastMCP
Initialize server
mcp = FastMCP("Demo Server")
Define a resource
@mcp.resource("config://app")
def getconfig() -> str:
"""Returns application configuration."""
return "App Configuration: debug=true, version=1.0"
Define a tool
@mcp.tool()
def calculatebmi(weightkg: float, heightm: float) -> str:
"""Calculate Body Mass Index (BMI).
Args:
weightkg: Weight in kilograms
heightm: Height in meters
"""
bmi = weightkg / (heightm * 2)
category = ""