Building Your Own MCP Server for AI

Your First MCP Server

Create a Python project, install the official MCP SDK, and run a hello server locally over stdio.

You need a folder, a virtual environment, and the official SDK. The first win is a process that starts and stays up — not a clever tool.

Project

Use Python 3.10 or newer. In an empty directory:

python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate

python -m pip install --upgrade pip
python -m pip install "mcp[cli]>=2,<3"

The mcp package is the official SDK. Pin the 2.x line so you match this course: MCPServer lives in mcp.server.mcpserver. The [cli] extra gives you the mcp command for inspecting a server from a terminal. Older tutorials still say FastMCP — that was the 1.x name for the same high-level API.

A server that does almost nothing

Create server.py:

from mcp.server.mcpserver import MCPServer

mcp = MCPServer("hello")


@mcp.tool()
def ping() -> str:
    '''Reply so you know the server is alive.'''
    return "pong"


if __name__ == "__main__":
    mcp.run()

MCPServer is the high-level API. mcp.run() defaults to stdio. The decorator registers ping as a tool with a description taken from the docstring. The host shows that text to the model.

Prove it from a terminal

With the venv active:

python server.py

The process should sit there with no greeting on stdout. That silence is success. Stop it with Ctrl+C. To list tools without an IDE:

mcp dev server.py

The inspector opens a small UI against your module. You should see the ping tool. If the command is missing, confirm the venv is active and mcp[cli] installed.

Layout that will survive later lessons

hello-mcp/
  server.py
  requirements.txt
  .venv/

Pin the SDK in requirements.txt (mcp[cli] plus a version when you freeze). Next: turn ping into a tool with a real schema.