How to Build an MCP Server: Complete Tutorial 2026
A hands-on guide to building a production-ready MCP server from scratch.
The Model Context Protocol (MCP) has become the standard way for AI agents to interact with external tools and data sources. Building an MCP server lets any compatible AI agent β Claude, GPT, or custom agents β use your tools seamlessly.
This tutorial walks you through building a fully functional MCP server in under an hour.
What Is MCP?
MCP (Model Context Protocol) is an open protocol that standardizes how AI models access external tools, data, and APIs. Instead of writing custom integrations for every AI platform, you build one MCP server and any MCP-compatible agent can use it.
Key benefits:
- Universal compatibility across AI platforms
- Standardized tool discovery and invocation
- Built-in schema validation
- Streaming support for long-running operations
Prerequisites
- Node.js 18+ or Python 3.11+
- Basic familiarity with TypeScript or Python
- An HTTPS endpoint for production (localhost for development)
Building an MCP Server in TypeScript
Step 1: Initialize Your Project
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk express zod
Step 2: Define Your Tools
import { McpServer } from "@modelcontextprotocol/sdk";
import { z } from "zod";
const server = new McpServer({
name: "weather-tools",
version: "1.0.0",
});
server.tool(
"get_weather",
"Get current weather for a city",
{
city: z.string().describe("City name"),
units: z.enum(["metric", "imperial"]).default("metric"),
},
async (args) => {
const response = await fetch(
`https://api.weather.example/v2/weather?q=${args.city}&units=${args.units}`
);
const data = await response.json();
return {
content: [
{ type: "text", text: JSON.stringify(data) },
],
};
}
);
Step 3: Add Transport
import { StdioServerTransport } from "@modelcontextprotocol/sdk";
const transport = new StdioServerTransport();
await server.connect(transport);
Step 4: Test Locally
npx @modelcontextprotocol/inspector node dist/index.js
The MCP Inspector lets you test tools visually before deploying.
Building an MCP Server in Python
from mcp import Server, Tool
from pydantic import BaseModel
class WeatherInput(BaseModel):
city: str
units: str = "metric"
server = Server("weather-tools")
@server.tool("get_weather")
async def get_weather(input: WeatherInput):
# Your logic here
return {"temperature": 22, "condition": "sunny"}
if __name__ == "__main__":
server.run()
Deploying Your MCP Server
Option 1: Cloud Deployment
Deploy to any platform that supports Node.js or Python:
- Vercel Functions β Serverless, auto-scaling
- Railway β Persistent processes, simple deploys
- AWS Lambda β Pay-per-invocation
- Cloudflare Workers β Edge deployment, ultra-low latency
Option 2: stdio Transport
For local tools, use the stdio transport. Agents launch your server as a subprocess and communicate via stdin/stdout.
Adding Authentication
For production MCP servers, add API key authentication:
server.middleware(async (request, next) => {
const apiKey = request.headers["x-api-key"];
if (!apiKey || !await validateKey(apiKey)) {
throw new Error("Unauthorized");
}
return next();
});
Publishing on SkillExchange
Once your MCP server is deployed, publish it on SkillExchange to make it discoverable:
- Create an account on skillexchange.market
- Click "Publish Skill"
- Enter your MCP server URL
- Set pricing (per-use, monthly, or freemium)
- Add tags and documentation
- Publish
Your MCP server is now available to every AI agent on the platform.
Best Practices
- Validate all inputs with Zod or Pydantic schemas
- Rate limit to prevent abuse
- Log everything β structured logging helps debugging
- Version your tools using semantic versioning
- Write clear descriptions β agents rely on these to select tools
- Handle errors gracefully with meaningful error messages
Common Pitfalls
- Blocking the event loop β always use async/await
- Missing schema validation β agents may send unexpected inputs
- No timeout handling β set reasonable timeouts for external API calls
- Unclear tool descriptions β if agents can't understand what your tool does, they won't use it
Next Steps
- Read the MCP vs REST APIs comparison to understand why MCP is a game-changer
- Learn about MCP server security best practices
- Explore enterprise MCP deployment strategies
Building an MCP server is the foundation of the AI-first API economy. Once your server is live, any AI agent can discover and use your tools β no custom integration needed.
Ready to publish your MCP server? List it on SkillExchange and start earning from your AI tools today.