Skip to main content
The Python samples use FastMCP, a high-level decorator-based API that makes it straightforward to define tools, resources, and prompts with minimal boilerplate.

Basic calculator server

Located at 03-GettingStarted/samples/python, this is a minimal MCP server with four calculator tools.
1

Clone and navigate

git clone https://github.com/microsoft/mcp-for-beginners.git
cd mcp-for-beginners/03-GettingStarted/samples/python
2

Install dependencies

pip install -r requirements.txt
Or install directly:
pip install mcp>=1.26.0
3

Run the server

python mcp_calculator_server.py
When run directly in a terminal you will see JSON-RPC validation errors — this is expected. The server waits for properly formatted MCP client messages on stdin.
4

Test the functions (optional)

python test_calculator.py

Server code

mcp_calculator_server.py
#!/usr/bin/env python3
"""Sample MCP Calculator Server implementation in Python."""

from mcp.server.fastmcp import FastMCP

# Create a FastMCP server
mcp = FastMCP("Calculator MCP Server")

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

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtract b from a and return the result."""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiply two numbers together and return the result."""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """
    Divide a by b and return the result.

    Raises:
        ValueError: If b is zero
    """
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

if __name__ == "__main__":
    mcp.run()
FastMCP reads type hints and docstrings to automatically generate the MCP tool schema — no manual JSON schema required.

Test script

test_calculator.py
from mcp_calculator_server import add, subtract, multiply, divide

def test_calculator_functions():
    assert add(5, 3) == 8
    print("Addition: OK")

    assert subtract(10, 4) == 6
    print("Subtraction: OK")

    assert multiply(7, 6) == 42
    print("Multiplication: OK")

    assert divide(15, 3) == 5
    print("Division: OK")

    try:
        divide(10, 0)
    except ValueError as e:
        assert str(e) == "Cannot divide by zero"
        print("Divide-by-zero guard: OK")

test_calculator_functions()

Build docs developers (and LLMs) love