How to Build an MCP Server in Python | Video

An MCP server is a program that gives an AI model a list of functions it can call. You can write one in Python in a single short file and connect it to an app like Claude Desktop.

In this video, our AI engineer  will build an MCP server in Python from start to finish. It will have one tool that checks the free space on your computer, so by the end, Claude can answer with real numbers from your own machine.

What Is an MCP Server?

MCP stands for Model Context Protocol. It is an open standard that describes how an AI application connects to outside tools and data. With MCP, you write a tool one time, and any app that supports MCP can use it.

There are two sides to an MCP setup:

  • The client is the app where the model lives, like Claude Desktop or Cursor.
  • The server is the program you write. It gives the model a list of functions it is allowed to call. We call those functions tools.

How Does an MCP Server Work?

To see how it works, let’s follow one question. You ask Claude: “How much free space do I have on my computer?” Claude cannot see your disk, so it needs a tool. Here is what happens next:

  1. The app starts. It launches your server and does a handshake. Then it asks which tools the server has.
  2. The server answers. It sends a name, a description, and an input schema for each tool, all as JSON messages.
  3. Claude picks a tool. It reads the list, chooses the disk tool, and writes a request.
  4. Your server runs the function. The app sends the request to your server, and your server runs the Python function.
  5. Claude answers. The result goes back to Claude, and Claude delivers your answer.

How to Build an MCP Server in Python

Our server has one tool that checks the free space on your computer, so Claude can tell you how much space you have left. You will need:

  • Python 3.10 or higher
  • uv, which is a Python package manager
  • Claude Desktop

Step 1: Set up the project

Create the project with uv init, add the MCP package with uv add, and create a file called disk.py.

uv init diskcheck
cd diskcheck
uv add "mcp[cli]"
touch disk.py

The touch command works only on macOS and Linux. On Windows, create the file with new-item disk.py.

Step 2: Create the server

Import the MCP server class and name your server.

import shutil

from mcp.server import MCPServer

mcp = MCPServer("disk")

Step 3: Add the tool

The tool is a normal Python function with the tool decorator on top. The type hints tell the model what input to send, and the docstring tells it what the tool does. Claude reads that text every time it decides whether to use your tool, so write it clearly. The rest is plain Python that reads the disk.

@mcp.tool()
def check_disk_space(path: str = "/") -> str:
    """Check how much disk space is free on this computer.

    Args:
        path: Folder or drive to check (for example / on macOS and Linux)
    """
    try:
        usage = shutil.disk_usage(path)
    except (FileNotFoundError, PermissionError) as e:
        return f"Could not read {path}: {e}"
    gb = 1024**3
    return (
        f"Total: {usage.total / gb:.1f} GB, "
        f"used: {usage.used / gb:.1f} GB, "
        f"free: {usage.free / gb:.1f} GB"
    )

If the path cannot be read, the try and except block returns a clear message instead of an error.

Step 4: Run the server

To run the server, add two lines at the bottom of the file.

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

One rule for this server: never use print. The server and the app talk through standard output, and print writes there too, so it corrupts the messages. Use logging instead, which writes to standard error by default.

Step 5: Connect Claude Desktop

In Settings, open Developer, choose Edit Config, and add the server with the full path to your folder. Replace the path below with the real absolute path to your diskcheck folder.

{
  "mcpServers": {
    "disk": {
      "command": "uv",
      "args": [
        "--directory",
        "/ABSOLUTE/PATH/TO/PARENT/FOLDER/diskcheck",
        "run",
        "disk.py"
      ]
    }
  }
}

If Claude Desktop can’t find uv, use the full path to uv in the command. On macOS and Linux, run which uv in your terminal to find it. Then fully quit the app and open it again. Closing the window is not enough.

Step 6: Test it

Ask Claude: “How much free space do I have?” It should ask permission, call your function, and answer with the real numbers from your computer. That is one short Python file giving Claude new, real information to provide.

Considerations Before Building Your Own

Our server should work. For your own tools, there are two things to remember:

  • Test your MCP server on its own first. Do it before you connect an app like Claude Desktop. Run npx @modelcontextprotocol/inspector uv run disk.py, and the MCP Inspector opens in your browser so you can run your tools by hand.
  • Decide what each tool is allowed to do. Our tool can read any path the user can access, so in production, restrict it to allowed folders.

Closing Thoughts

An MCP server is a program that gives an AI model a list of functions it can call. We built one in Python with a single tool, connected it to Claude Desktop, and Claude used our function to answer a question it couldn’t before.

MCP gives an AI model a standard way to use your tools and your data, and you only write each tool once. To see the full build, watch the video on our YouTube channel and subscribe for more videos on building with AI.

Frequently Asked Questions (FAQs)

What is an MCP server?

An MCP server is a program that gives an AI model a list of functions it can call. These functions are called tools.

How does an MCP server work?

The app launches your server and asks which tools it has. The server answers with a name, a description, and an input schema for each tool. When Claude picks one, your server runs the function and the result goes back to Claude.

How do I build an MCP server in Python?

Set up a project with uv, add the MCP package, write a Python function with the tool decorator, and run it with mcp.run. Then connect it to an app like Claude Desktop. The steps above include the full code.

What is the difference between MCP and an API?

An API is how two programs talk to each other. MCP is a standard way for an AI to discover what tools it has and use them. Many MCP servers call an API behind the scenes, and others access local files or databases.

Tags:

Subscribe to our newsletter

Table of Contents
AI-Driven Software, Delivered Right.
Subscribe to our newsletter
Table of Contents
We Make
Development Easier
ClickIt Collaborator Working on a Laptop
From building robust applications to staff augmentation

We provide cost-effective solutions tailored to your needs. Ready to elevate your IT game?