How to Build an MCP Server from Scratch
Learn step-by-step how to create your own Model Context Protocol (MCP) server using Python and FastMCP, with practical examples and tips.
Introduction
MCP Server Setup
What is MCP?
Model Context Protocol (MCP)
MCP is a standardized protocol that allows AI models like Claude to interact with your local tools, data, and applications. By building an MCP server you create a bridge where the AI can call functions, retrieve data, and perform actions securely.
"MCP servers provide the missing link, giving AI agents the tools they need to access live data and perform real actions." – Dev.to tutorial
Prerequisites
- Basic knowledge of Python
- Python 3.9+ installed
- Access to a terminal/command prompt
- An internet connection to install packages
Step 1: Set Up Your Project
Use the MCP CLI
The fastest way to scaffold a project is with the MCP command‑line tool.
1mcp create my-mcp-projectThis creates a new folder with a basic structure and a sample tool. For more details see the Reddit post introducing the CLI: Introducing MCP‑Framework.
Step 2: Install FastMCP
FastMCP is the recommended Python framework for building MCP servers.
1cd my-mcp-project
2pip install fastmcpStep 3: Define a Calendar Tool
We'll build a simple event‑calendar MCP server, similar to the one in the Codecademy tutorial.
1# calendar_tool.py
2from fastmcp import tool, MCPServer
3
4@tool(name="create_event", description="Create a new calendar event")
5def create_event(title: str, date: str) -> str:
6 # In a real app this would store the event in a DB
7 return f"Event '{title}' scheduled for {date}."
8
9@tool(name="list_events", description="List all calendar events")
10def list_events() -> list:
11 # Placeholder list
12 return ["Meeting at 10am", "Lunch with Alex at 1pm"]
13
14server = MCPServer()
15server.register_tool(create_event)
16server.register_tool(list_events)
17
18if __name__ == "__main__":
19 server.run(host="0.0.0.0", port=8000)The @tool decorator exposes functions to any MCP‑compatible client.
Step 4: Run the Server
1python calendar_tool.pyYou should see something like:
1MCP server listening on http://0.0.0.0:8000Step 5: Test with the MCP Inspector
The MCP Inspector is a web‑based debugging UI. Open your browser and navigate to http://localhost:8000/inspect (or the URL provided by your server). You can manually invoke create_event and list_events to verify they work.
Step 6: Connect Claude Desktop
- Open Claude Desktop.
- Go to Settings → MCP and add a new endpoint pointing to
http://localhost:8000. - Ask Claude: "Show me my calendar events".
- Claude will call
list_eventsvia the MCP server and display the results.
The Codecademy tutorial walks through this exact flow: Build an MCP Server.
Tips & Best Practices
- Secure your server – Use HTTPS and authentication if you expose the server beyond localhost.
- Validate input – Always sanitize parameters received from the AI to avoid injection attacks.
- Modular design – Keep each tool in its own module for easier maintenance.
- Logging – Enable detailed logging to troubleshoot communication issues.
Quick Reference Table
| Command | Description |
|---|---|
mcp create <project> | Scaffold a new MCP project |
pip install fastmcp | Install the FastMCP framework |
python <file>.py | Run your MCP server |
/inspect | Open the MCP Inspector UI |
Conclusion
By following these steps you now have a functional MCP server that can create, list, and manage calendar events via natural‑language commands. The same pattern applies to other domains—weather data, file management, or custom business logic. Happy hacking!