MCP Server Tutorial 2026: Build Your First MCP Server from Scratch
You understand MCP conceptually but don't know where to start? This tutorial builds two runnable servers (file reader + weather) from scratch in 10 minutes, and explains stdio vs SSE.
💡 What You Will Learn
You understand MCP conceptually but don't know where to start? This tutorial builds two runnable servers (file reader + weather) from scratch in 10 minutes, and explains stdio vs SSE.
📜 Table of Contents
MCP Server Tutorial 2026: Build Your First MCP Server from Scratch
MCP (Model Context Protocol) lets AI clients like Claude Desktop call external tools through a unified standard. Concepts are easy; getting started is the hard part. This hands-on tutorial builds two working servers in about 10 minutes.
1. Prerequisites
- Python 3.10+
- pip
No GPU or server needed.
2. Install the SDK
pip install mcp
3. First Server: File Reader
Register two tools so the AI can read your local files:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("file-reader")
@mcp.tool()
def read_file(path: str) -> str:
"""Read a local file"""
with open(path, "r", encoding="utf-8") as f:
return f.read()
@mcp.tool()
def list_files(directory: str) -> list[str]:
"""List files in a directory"""
import os
return os.listdir(directory)
if __name__ == "__main__":
mcp.run()
Save as file_reader_server.py. The docstrings are the tool descriptions the AI uses to decide when to call each tool—write them clearly.
4. Run It and Connect a Client
python file_reader_server.py
The server communicates over stdio and stays silent until a client connects—that's normal. In Claude Desktop (or any MCP-capable client), add an MCP server with the command python /absolute/path/file_reader_server.py, then restart. Now you can say "read notes.txt on my desktop" and the AI will call read_file automatically.
5. Second Example: Weather Server
Tools can also call external APIs. Here's a weather tool using the free wttr.in service:
from mcp.server.fastmcp import FastMCP
import urllib.request
mcp = FastMCP("weather")
@mcp.tool()
def get_weather(city: str) -> str:
"""Get current weather for a city"""
url = f"https://wttr.in/{city}?format=%C+%t+%w"
with urllib.request.urlopen(url, timeout=10) as r:
return r.read().decode("utf-8")
if __name__ == "__main__":
mcp.run()
This shows the full loop: user asks → AI picks the tool → tool fetches data → AI answers. Follow the same pattern to add your own APIs.
6. stdio vs SSE
| Transport | Use Case | Notes |
|---|---|---|
| stdio | Local, personal use | Simple, no network port |
| SSE / Streamable HTTP | Remote deployment, shared services | Runs over HTTP on a server |
7. One Server, Many Tools
A single server can register unlimited tools. Clients auto-discover them and pick the right one per request. Good tool descriptions make the AI use them correctly.
FAQ
Q: ModuleNotFoundError: mcp? A: Run pip install mcp, and make sure the script runs with the same Python environment.
Q: Nothing shows in the terminal? A: Normal for stdio servers; output appears when a client calls a tool.
Q: MCP vs a normal API? A: With MCP the AI client discovers and calls tools itself—no per-tool integration code, and it works across clients.
Q: Can I deploy remotely? A: Yes, using streamable HTTP transport with your own auth and firewall; see the official docs.
Note: SDK APIs may evolve; check modelcontextprotocol.io if something fails.
❓ FAQ
ModuleNotFoundError: mcp?
Run `pip install mcp`, and make sure the script runs with the same Python environment.
Nothing shows in the terminal?
Normal for stdio servers; output appears when a client calls a tool.
MCP vs a normal API?
With MCP the AI client discovers and calls tools itself—no per-tool integration code, and it works across clients.
Can I deploy remotely?
Yes, using streamable HTTP transport with your own auth and firewall; see the official docs. > Note: SDK APIs may evolve; check modelcontextprotocol.io if something fails.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.
