DEV Community

Darshan Karanth
Darshan Karanth

Posted on

A Python based MCP server calculator tool

Summary
Leverage MCP server python SDK to build a local MCP server.

Requirements
Install python version 3.14+
vscode or any IDE
Claude desktop as MCP Client

Steps
Create project directory and configure python virtual env

mkdir calculator
python -m venv venv
source .venv/Scripts/activate
Enter fullscreen mode Exit fullscreen mode

Install and initialize uv

pip install uv
uv init
Enter fullscreen mode Exit fullscreen mode

Add MCP CLI for the project dependencies

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

Edit the created main.py file with the calculator tool

from mcp.server.fastmcp import FastMCP

# Initialize the MCP server
mcp = FastMCP("Simple Calculator")

@mcp.tool()
def add(a: float, b: float) -> float:
    """Adds two numbers together."""
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtracts b from a."""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiplies two numbers."""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> str:
    """Divides a by b. Handles division by zero errors."""
    if b == 0:
        return "Error: Cannot divide by zero."
    return str(a / b)

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

Install claude code and run the MCP server

uv run mcp install main.py
[01/18/26 13:34:04] INFO     Added server 'Simple Calculator' to Claude config                                                                      claude.py:136
                    INFO     Successfully installed Simple Calculator in Claude app                                                                    cli.py:485
(.venv) 
Enter fullscreen mode Exit fullscreen mode

Check File -> Settings -> Developer on claude code if the recently created MCP server is running.
Claudecodemcprunning

Click Edit config to see the MCP servers configured on the Claude desktop

{
    "mcpServers": {
        "Simple Calculator": {
            "command": "C:\\Users\\MCP\\calculator\\.venv\\Scripts\\uv.EXE",
            "args": [
                "run",
                "--frozen",
                "--with",
                "mcp[cli]",
                "mcp",
                "run",
                "C:\\Users\\MCP\\calculator\\main.py"
            ]
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Claudedesktopconfig

Test the MCP server

Start the conversation and provide an arithmetic calculation. Notice that claude leverages the calculator MCP server.
Claude asks for tool access and performs subsequent calculation.

claudeconversation

ClaudeMultiply

ClaudeDivide

Top comments (0)