DEV Community

Cover image for Building a Custom MCP Server: A Python Calculator (Step-by-Step)

Building a Custom MCP Server: A Python Calculator (Step-by-Step)

βœ… Prerequisites

Before we begin, make sure you have:

  • 🐍 Python 3.10+ installed
  • πŸ’» A code editor (VS Code, etc.)
  • πŸ–₯️ Basic command line knowledge

πŸ› οΈ Step 1: Environment Setup

First, let's create a directory for our project and set up a clean Python environment.

Open your terminal and run:

# 1. Create the project folder
mkdir mcp-unit-converter
cd mcp-unit-converter

# 2. Create a virtual environment
python -m venv venv
Enter fullscreen mode Exit fullscreen mode

3. Activate the environment:

On Windows:

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

On Mac/Linux:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

4. Install the MCP SDK:

pip install mcp[cli]
Enter fullscreen mode Exit fullscreen mode

πŸ“ Step 2: Writing the Server Code

We will use the FastMCP class to build our server quickly. Create a file named converter.py.

This code defines tools for converting:

  • 🌑️ Temperature (Celsius/Fahrenheit)
  • πŸ“ Length (Meters/Feet)
  • βš–οΈ Weight (Kg/Pounds)
from mcp.server.fastmcp import FastMCP

# Initialize the server
mcp = FastMCP("UnitConverter")

# --- Temperature Tools ---

@mcp.tool()
def celsius_to_fahrenheit(celsius: float) -> float:
    """Convert Celsius to Fahrenheit"""
    return (celsius * 9/5) + 32

@mcp.tool()
def fahrenheit_to_celsius(fahrenheit: float) -> float:
    """Convert Fahrenheit to Celsius"""
    return (fahrenheit - 32) * 5/9

# --- Length Tools ---

@mcp.tool()
def meters_to_feet(meters: float) -> float:
    """Convert Meters to Feet"""
    return meters * 3.28084

@mcp.tool()
def feet_to_meters(feet: float) -> float:
    """Convert Feet to Meters"""
    return feet / 3.28084

# --- Weight Tools ---

@mcp.tool()
def kg_to_pounds(kg: float) -> float:
    """Convert Kilograms to Pounds"""
    return kg * 2.20462

@mcp.tool()
def pounds_to_kg(pounds: float) -> float:
    """Convert Pounds to Kilograms"""
    return pounds / 2.20462

if __name__ == "__main__":
    mcp.run(transport="stdio")
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 3: Testing with MCP Inspector

Before connecting it to an AI, let's verify it works locally using the MCP Inspector.

Run this command in your terminal:

mcp dev converter.py
Enter fullscreen mode Exit fullscreen mode

This will open a local web interface. You can:

  1. Select the celsius_to_fahrenheit tool
  2. Input 0
  3. Check if it returns 32 βœ…

πŸ”— Step 4: Connecting to an AI Client

Now for the magic! ✨ We will connect our server to any MCP-compliant client.

Configuration Steps:

  1. Locate your client's MCP configuration folder
  2. Create or edit your mcp.json configuration file
  3. Add the following configuration:
{
  "mcpServers": {
    "unit_converter": {
      "command": "/ABSOLUTE/PATH/TO/YOUR/venv/bin/python",
      "args": ["/ABSOLUTE/PATH/TO/YOUR/converter.py"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Important: You must use the absolute path to your python executable inside the venv folder and the absolute path to your converter.py file.


🎬 Step 5: Seeing it in Action

Once connected, you can open your AI Chat and ask:

"I have a shipping crate that weighs 450 kg and is 12 meters long. Convert these to pounds and feet using my tools."

The AI will intelligently call kg_to_pounds and meters_to_feet and give you the exact result based on your code.

Example Output:

Input Tool Used Result
450 kg kg_to_pounds 992.08 lbs
12 m meters_to_feet 39.37 ft

πŸ“¦ Source Code

You can find the complete repository with the source code here:

πŸ”— GitHub: https://github.com/GregoBHM/mcp-unit-converter


🎯 Conclusion

Building an MCP server is surprisingly simple. With just a few lines of Python, we extended the capabilities of our AI assistant, making it more reliable for specific tasks like unit conversion.

What We Learned:

  • βœ… How to set up an MCP server with Python
  • βœ… How to define custom tools using @mcp.tool() decorator
  • βœ… How to test locally with MCP Inspector
  • βœ… How to connect to an MCP-compliant AI client

If you found this useful, check out the video demonstration below! πŸŽ₯

Happy coding! πŸš€

Top comments (0)