β 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
3. Activate the environment:
On Windows:
venv\Scripts\activate
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)
- βοΈ 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_fahrenheittool - Input
0 - 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:
- Locate your client's MCP configuration folder
- Create or edit your
mcp.jsonconfiguration file - 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
venvfolder and the absolute path to yourconverter.pyfile.
π¬ 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)