Build Your First MCP Server
The Model Context Protocol (MCP) is becoming the "HTTP of AI" - a standard way for AI systems to access tools and data. Learn how to build your own MCP server.
What is MCP?
The Model Context Protocol standardizes how AI assistants connect to external tools, data sources, and services. Think of it as giving your AI superpowers.
- Connect AI to your databases & APIs
- Create reusable tools across projects
- Build enterprise-grade AI infrastructure
MCP Architecture (Model Agnostic)
┌─────────────┐ ┌─────────────┐
│ AI Host │────▶│ MCP Server │
│ (Any LLM) │◀────│ (Your App) │
└─────────────┘ └─────────────┘
│ │
│ JSON-RPC │
│ Protocol │
▼ ▼
┌─────────┐ ┌───────────┐
│ Tools │ │ Resources │
│ Prompts │ │ Data │
└─────────┘ └───────────┘
When to Choose Each Technology
Choose TypeScript when:
- - Your team already uses Node.js/JavaScript
- - Building web-integrated MCP servers
- - Need strong typing for large codebases
- - Want excellent IDE support & tooling
- - Deploying to serverless (Vercel, AWS Lambda)
Choose Python when:
- - Working with data science / ML pipelines
- - Integrating with existing Python services
- - Need rich ecosystem (pandas, numpy, etc.)
- - Rapid prototyping is priority
- - Team is more comfortable with Python
TypeScript MCP Server
Initialize Your Project
# Create project directory
mkdir my-mcp-server && cd my-mcp-server
# Initialize with npm
npm init -y
# Install MCP SDK and TypeScript
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node tsx
# Initialize TypeScript
npx tsc --init
Create Your MCP Server
Create src/index.ts:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// Create the MCP server instance
const server = new McpServer({
name: "my-first-mcp-server",
version: "1.0.0",
});
// Define a simple tool: get current time
server.tool(
"get_current_time",
"Returns the current date and time",
{
timezone: z.string().optional().describe("Timezone (e.g., 'UTC', 'America/New_York')"),
},
async ({ timezone }) => {
const now = new Date();
const options: Intl.DateTimeFormatOptions = {
timeZone: timezone || "UTC",
dateStyle: "full",
timeStyle: "long",
};
return {
content: [{
type: "text",
text: now.toLocaleString("en-US", options)
}],
};
}
);
// Define a tool: simple calculator
server.tool(
"calculate",
"Performs basic arithmetic operations",
{
operation: z.enum(["add", "subtract", "multiply", "divide"]),
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
},
async ({ operation, a, b }) => {
let result: number;
switch (operation) {
case "add": result = a + b; break;
case "subtract": result = a - b; break;
case "multiply": result = a * b; break;
case "divide":
if (b === 0) throw new Error("Division by zero");
result = a / b;
break;
}
return {
content: [{ type: "text", text: `Result: ${result}` }],
};
}
);
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Server running...");
}
main().catch(console.error);
Configure & Run
Add to your package.json:
{
"scripts": {
"start": "tsx src/index.ts",
"build": "tsc"
}
}
Run your server:
npm start
MCP is AI Model Agnostic
Important: The Model Context Protocol is an open standard - not tied to any specific AI provider. Your MCP server works with any AI host that implements the protocol:
Currently Supported:
- - Claude Desktop & Claude Code
- - Cursor IDE
- - Zed Editor
- - Continue (VS Code)
Growing Ecosystem:
- - OpenAI integration (coming)
- - Custom AI applications
- - Enterprise AI platforms
- - Your own AI hosts
Build once, connect to any MCP-compatible AI system. That's the power of an open protocol.
Example: Connect to Claude Desktop
Optional - One of Many HostsThis shows how to connect to Claude Desktop as an example. Each MCP-compatible host has its own configuration method.
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"my-mcp-server": {
"command": "npx",
"args": ["tsx", "/path/to/your/src/index.ts"]
}
}
}
For Python, use: "command": "python", "args": ["/path/to/server.py"]
What You've Built
- A working MCP server with 2 tools
- Input validation with schemas
- Error handling patterns
- Foundation for enterprise tools
Go Deeper in Chapter 11
This lab covers the basics. The book teaches you:
- - Resources & dynamic data exposure
- - Prompt templates as MCP services
- - Authentication & security patterns
- - Enterprise deployment strategies
- - Multi-server orchestration
"Just as HTTP standardized web communication, MCP standardizes AI-to-system communication. Early adopters gain years of advantage."
- From Blueprint to Application, Chapter 11