Introduction

As developers, we are increasingly integrating AI into our workflows using IDEs like Cursor, Windsurf, or Claude Desktop. However, Large Language Models (LLMs) can sometimes struggle with precise mathematical conversions or lack access to specific local logic.

This is where the Model Context Protocol (MCP) shines. Think of MCP as a standardized “USB-C port” that connects AI models to your local tools and data.

In this tutorial, I will show you how to build a Unit Converter MCP Server from scratch using Python. This tool will allow your AI agent to perform precise conversions for temperature, length, and weight without “hallucinating” the numbers.

Prerequisites

Python 3.10+ installed.

A code editor (VS Code, Cursor, 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:

mkdir mcp-unit-converter
cd mcp-unit-converter

2. Create a virtual environment

python -m venv venv

3. Activate the environment

On Windows:

venvScriptsactivate

On Mac/Linux:

source venv/bin/activate

4. Install the MCP SDK

pip install mcp[cli]

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), and 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") 

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

This will open a local web interface. You can select the celsius_to_fahrenheit tool, input 0, and check if it returns 32.

Step 4: Connecting to Cursor (AI Client)

Now for the magic. We will connect our server to Cursor (or any MCP-compliant client).

Create a .cursor folder in your project (optional, but good for organization).

Create or edit your mcp.json configuration file (usually located in your global Cursor settings or project root).

Add the following configuration:

{
“mcpServers”: {
“unit_converter”: {
“command”: “/ABSOLUTE/PATH/TO/YOUR/venv/bin/python”,
“args”: [“/ABSOLUTE/PATH/TO/YOUR/converter.py”]
}
}
}

⚠️ 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.

Source Code

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

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.

If you found this useful, check out the video demonstration below!

Happy coding!


Source: DEV Community.


Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.