R E D R I F T A I

Loading

Build an MCP Server with Python

If you’re looking to build an MCP server with Python, this hands-on guide walks you through every step. Whether you want to connect AI models like ChatGPT, Claude, or Gemma into a single, seamless workflow, or explore how the Model Context Protocol (MCP) works in practice — this is your blueprint.

We’re taking a code-first approach to building your own MCP server, with less theory and more doing. If you’re a developer exploring AI model coordination or building smarter, multi-agent apps, this guide is for you.

Before we dive in, here’s a quick primer:

What is MCP?
MCP stands for Model Context Protocol—a shared communication standard that allows different AI tools, models, and agents to talk to each other in a structured, context-aware way. It’s like giving your AI systems a common language and memory so they can collaborate intelligently.
👉 Want the full deep dive on how MCP works under the hood? Check out our theory article here.

In this hands-on guide, we’ll walk you through the exact steps to get an MCP server up and running using Python. You’ll learn how to:

Prerequisites

Before we dive into building your own MCP (Model Context Protocol) Server with Python, let’s get a few things set up. Don’t worry — this won’t take long.

What You’ll Need

Python 3.12 or higher

Make sure you’ve got Python 3.10+ installed on your system. You can check this by running:

python --version

If not installed, grab the latest version from python.org.

Set Up a Python Environment

We recommend using a virtual environment so your project dependencies stay nice and clean.

Option 1: Using venv (built-in Python virtual environment)

python -m venv mcp-env
source mcp-env/bin/activate  # On Windows, use: .\mcp-env\Scripts\activate

Option 2: Using conda

conda create -n mcp-env python=3.12
conda activate mcp-env

Install the MCP SDK (with CLI)

Once your environment is active, install the Model Context Protocol SDK using pip:

pip install mcp[cli]

This gives you the tools you need to create and run an MCP server from your command line.

Building the MCP Server

Now that you know what the Model Context Protocol (MCP) is, let’s build something real — your first MCP server in Python.

We’ll use FastMCP, a minimal and fast way to get started.

Create server.py

Let’s build a simple server that offers:

  • A basic calculator tool
  • A greeting generator
  • A prompt template to help LLMs interact with the server

Here’s the full code:

from mcp.server.fastmcp import FastMCP

# Initialize MCP server
mcp = FastMCP("Demo", host="0.0.0.0", port=8080)
# - legacy_sse=True only if using modelcontextprotocol>=1.11.0,<1.13.0 else MCP Client throw 404
# - "Demo" is your server's name
# - "0.0.0.0" lets you accept connections from anywhere
# - Port 8080 is where your server will listen

# Define tools – the action functions LLMs can call

@mcp.tool()
def add(a: int, b: int) -> int:
    """
    Add two numbers together.

    Args:
        a (int): First number.
        b (int): Second number.

    Returns:
        int: Sum of the two input numbers.
    """
    return a + b

# Define a resource – read-only data endpoint

@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """
    Generate a personalized greeting.

    Args:
        name (str): Name of the person to greet.

    Returns:
        str: Greeting message for the user.
    """
    return f"Hello, {name}!"

# Define a prompt – reusable text template for LLMs

@mcp.prompt()
def greet_user(name: str, style: str = "friendly") -> str:
    """
    Generate a greeting prompt for LLMs to follow.

    Args:
        name (str): Recipient's name.
        style (str): Greeting style (friendly, formal, casual).

    Returns:
        str: Instructional prompt for LLM to generate a greeting.
    """
    styles = {
        "friendly": "Please write a warm, friendly greeting",
        "formal": "Please write a formal, professional greeting",
        "casual": "Please write a casual, relaxed greeting",
    }

    return f"{styles.get(style, styles['friendly'])} for someone named {name}."

# Start the server using Streamable HTTP transport
if __name__ == "__main__":
    mcp.run(transport="streamable-http")

Tools, Resources & Prompts — What’s the Difference?

TypePurpose
ToolPerform actions (e.g., calculate, store, fetch). Can have side effects.
ResourceProvide read-only data (like a REST API’s GET). No side effects.
PromptHelp LLMs understand how to interact or what to say — like reusable text instructions.

Writing Good Docstrings

LLMs read your tool and resource docstrings to understand what they do. Think of it like leaving instructions for a super-smart robot assistant. If your docstrings are clear and well-formatted, the LLM will know exactly how to use them.

Here’s a solid docstring format to follow:

"""
Add a new storage entry for a specific product.

Args:
    product_id (int): ID of the product to store.
    quantity (int): Number of units to store.
    location (str): Storage location identifier.

Returns:
    str: Confirmation message indicating successful storage entry.
"""

Pro Tips:

  • Keep tools small and simple — LLMs will loop through them as needed.
  • Use clear argument types and return values.
  • Don’t overcomplicate tools with loops unless absolutely needed.

Choosing the Right MCP Transport

The Model Context Protocol (MCP) gives you several ways to handle communication between your app and your AI model. Each option has its strengths depending on where and how you’re using it. Let’s break down the main transport types to help you decide what works best for your project.

Standard I/O

This method uses the standard input and output (stdin/stdout) channels, sending messages in JSON-RPC format. It’s perfect for local testing, debugging, or working offline. If you’re building or testing a model on your own machine, this is a simple and effective option with minimal setup.

HTTP + SSE (Server-Sent Events)

This method uses regular HTTP POST requests for input and Server-Sent Events to stream results back. It’s great for remote tasks or real-time interactions, like sending data to a server and getting updates as the model processes it. If you need async communication and status updates, this is a solid pick.

Streamable HTTP

Streamable HTTP merges both input and output into a single, continuous HTTP stream. It’s especially good for cloud-native deployments, where speed and simplicity matter. This is the method we used in our example, because it makes it easy to build an API endpoint that talks to your model in a clean and efficient way — perfect for modern LLMs or apps needing fast, interactive responses.

Custom (WebSockets)

WebSockets are the most flexible option. They allow for real-time, two-way communication and are driven by the MCP community. If you’re working on more complex workflows, like managing multiple users or live collaboration, this might be the way to go. It requires a bit more setup, but gives you full control over the interaction.

In our example, we chose streamable HTTP because it’s ideal for building clean, responsive APIs — perfect for cloud-based setups and LLMs that need fast, seamless interactions.

Run the MCP Server

Now that your server.py file is ready, it’s time to launch your MCP server and see it in action!

Just run this command in your terminal:

python server.py

If everything is working correctly, you’ll see output like this:

INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

That’s it — your MCP Server is now live!

You can now interact with it or start integrating it into your AI workflows. The server is listening on port 8080, ready to handle requests using the Model Context Protocol.

Create Your Own MCP Client

Once your MCP server is up and running, the next step is to connect to it using a client. Luckily, the MCP Python SDK makes this super easy.

You’ll use the MCPClient class from the SDK, and since we’re running the server locally as a streamable HTTP API, we’ll go with the HTTP-SSE connection method.

Example 1: Connect via Local HTTP-SSE

Here’s how to set up the client to talk to your locally running MCP server:

from mcp_use import MCPClient

client = MCPClient(config={
    "mcpServers": {
        "local": {
            "url": "http://0.0.0.0:8080/mcp/sse"
        }
    }
})

That’s all! This client will now stream responses from your locally hosted MCP server via Server-Sent Events (SSE).

If you’d like, we can also show two additional connection examples for completeness, even if we’re not using them. Here’s a suggestion for how to include them for users who might be deploying differently:

Other Ways to Connect (For Reference)

If you’re deploying your MCP server remotely or want to launch it as a subprocess, here are a couple of alternatives:

Example 2: Connect to a Remote Streamable HTTP Endpoint

client = MCPClient(config={
    "mcpServers": {
        "remote": {
            "url": "https://your-domain.com/mcp"
        }
    }
})

Use this if you’ve deployed your MCP server online and want to access it over HTTPS.

Example 3: Launch the Server via Subprocess

client = MCPClient(config={
    "mcpServers": {
        "local": {
            "command": "python",
            "args": ["server.py"]
        }
    }
})

This will automatically start your MCP server as a subprocess before connecting to it.

Connect Your MCP Server to a Client

Alright! You’ve got your MCP server running locally on http://0.0.0.0:8080. Now it’s time to connect a client to it — so you can actually start using it for real tasks like sending prompts, streaming responses, or integrating with apps like Claude or Cursor IDE.

We’ll show you how to do that in three easy ways:

Option 1: Use Python to Connect (Most Common)

If you’re working in Python (which we are in this tutorial), here’s how to connect your MCP server using the MCPClient from the official SDK:

from mcp_use import MCPClient

client = MCPClient(config={
    "mcpServers": {
        "local": {
            "url": "http://0.0.0.0:8080/mcp/sse"
        }
    }
})

This sets up a simple streamable HTTP connection (SSE) to your local MCP server.

Now your client is ready to send requests and receive responses in real time — super handy for building chatbots, tools, or other AI features.

Option 2: Use Your Server Inside Claude Desktop

Did you know Claude Desktop (the app version of Claude AI) can use your local MCP server as a custom tool?

Here’s how to hook it up:

  1. Open Claude Desktop
  2. Go to Settings → Developer → Edit Config
  3. Add this block to the config file:
{
  "mcpServers": {
    "my-server": {
      "command": "python",
      "args": ["path/to/server.py"]
    }
  }
}
  1. Save and restart Claude
  2. You’ll now see your MCP server listed in Claude’s tool menu — and Claude can call it like a smart assistant!

It’s a great way to give Claude new capabilities using your own Python code.

Note: Claude Desktop is still evolving, and the way it handles MCP tools may change in future updates. Always check the latest developer settings or official docs if something looks different

Option 3: Use Your Server Inside Cursor IDE

If you code with Cursor IDE (an AI-powered coding editor), it also supports MCP out of the box!

To connect your server:

  1. Open your project in Cursor
  2. Create a file called mcp.json inside .cursor/
  3. Paste in something like this:
{
  "mcpServers": {
    "local-python": {
      "url": "http://0.0.0.0:8080/mcp"
    }
  }
}
  1. Save and restart Cursor

Now Cursor will detect your server and treat it as a tool you can call during coding tasks!

Note: Cursor IDE is actively updated, and MCP integration details might shift over time. If something doesn’t work as shown, check Cursor’s official guide or tool gallery for the latest setup steps.

Wrapping Up: You Just Built an MCP Server!

Congrats! You’ve now built a fully functional Model Context Protocol (MCP) server using Python, connected it to a local client, and even explored how to plug it into tools like Claude Desktop and Cursor IDE.

This setup lets you turn your Python code into something AI models can actually talk to — whether it’s a smart tool, a data resource, or a reusable prompt. Think of it as giving your AI models a shared brain and a toolkit they can all use together.

Why This Matters

MCP makes your apps and models smarter, more connected, and way more flexible. Whether you’re building a chatbot, a productivity tool, or a multimodal pipeline, using MCP means your LLMs won’t be working in silos — they’ll be collaborating.

What’s Next?

  • Try adding your own tools and prompts
  • Host your MCP server on the cloud
  • Connect multiple models into a single workflow
  • Or plug your server into custom UIs, apps, or agents

And remember: this is just the beginning. The MCP ecosystem is growing fast — new SDKs, IDE integrations, and community tools are showing up all the time. Keep an eye on modelcontextprotocol.io or the official GitHub repo for updates.

Leave a Comment

Your email address will not be published. Required fields are marked *