Introduction
The Model Context Protocol (MCP) is an open protocol developed by Anthropic that standardizes how AI models connect to external tools, data sources, and services. Think of it as "USB for AI" -- a universal interface that allows any AI model to work with any tool.
In this tutorial, we will explain what MCP is, why it matters, and how to build your first MCP server.
Prerequisites
- Node.js 18+ or Python 3.10+
- Basic understanding of APIs and client-server architecture
- An MCP-compatible client (Claude Desktop, Claude Code, or Cursor)
What is MCP?
MCP defines a standard way for AI applications (clients) to communicate with data sources and tools (servers). The protocol specifies three main capabilities:
Resources
Expose data that the AI can read:
- File contents
- Database records
- API responses
- Live data feedsTools
Define actions the AI can execute:
- Send emails
- Create database records
- Execute code
- Call external APIsPrompts
Provide reusable prompt templates:
- Code review prompts
- Data analysis templates
- Report generation formatsStep 1: Create a Basic MCP Server
Let us build an MCP server that provides weather data:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather-server",
version: "1.0.0",
});
// Define a tool
server.tool(
"get-weather",
"Get current weather for a city",
{
city: z.string().describe("City name"),
units: z.enum(["celsius", "fahrenheit"]).default("celsius"),
},
async ({ city, units }) => {
// Fetch weather data from your preferred API
const response = await fetch(
`https://api.weather.example/current?city=${city}&units=${units}`
);
const data = await response.json();
return {
content: [{
type: "text",
text: `Weather in ${city}: ${data.temperature}${units === "celsius" ? "C" : "F"}, ${data.condition}`,
}],
};
}
);
// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);Step 2: Configure the Client
Add your server to Claude Desktop's configuration:
{
"mcpServers": {
"weather": {
"command": "node",
"args": ["path/to/weather-server.js"]
}
}
}Step 3: Add Resources
Expose data for the AI to read:
server.resource(
"weather-forecast",
"weather://forecast/{city}",
async (uri) => {
const city = uri.pathname.split("/").pop();
const forecast = await fetchForecast(city);
return {
contents: [{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify(forecast, null, 2),
}],
};
}
);Architecture Overview
+------------------+ +------------------+
| AI Client | MCP | MCP Server |
| (Claude, Cursor) | <------> | (Your Tools) |
+------------------+ +------------------+
|
+------+------+
| | |
APIs DBs FilesWhy MCP Matters
- Standardization: One protocol instead of custom integrations for each AI tool
- Composability: Mix and match servers for different capabilities
- Security: Clear permission model and sandboxing
- Open ecosystem: Anyone can build and share MCP servers
Troubleshooting
- Server not connecting: Check the command path and ensure Node.js is installed
- Tools not appearing: Verify the server starts without errors in standalone mode
- Permission errors: Ensure the server has access to required resources
- Timeout issues: Implement proper error handling and timeouts in tool handlers
Conclusion
MCP is rapidly becoming the standard for AI tool integration. By building MCP servers, you enable any compatible AI client to use your tools and data, creating a powerful and flexible ecosystem.
Next Steps
- Build an MCP server for your company's internal APIs
- Explore the MCP server registry for pre-built integrations
- Contribute to the open-source MCP specification