home / mcp / mcp calculator server

MCP Calculator Server

Provides arithmetic tools (add, subtract, multiply, divide) via MCP for interactive and programmatic use.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "ayyappachinpandi-mcp-example-ayyappa": {
      "url": "http://127.0.0.1:8000",
      "headers": {
        "UV_PROJECT_ENVIRONMENT": ".venv"
      }
    }
  }
}

You can run a minimal MCP server that exposes calculator tools (add, subtract, multiply, divide) and connect MCP clients to perform arithmetic operations. This server is designed as a practical, self-contained example you can run locally, test with an inspector, and extend for your own MCP projects.

How to use

You will connect MCP clients to the server using two common transport methods. The standard input/output (stdio) transport runs locally in the same process, making it ideal for quick testing and integration with MCP Inspector. The HTTP transport exposes the server over a network port so clients can connect remotely.

How to install

Prerequisites you need before starting are: Python 3.11 or newer, and the MCP command line tools.

1) Create and activate a virtual environment.

python -m venv .venv
.\.venv\Scripts\Activate.ps1

2) Install the MCP CLI within the virtual environment.

pip install "mcp[cli]"

3) Run the server using stdio transport for local testing.

python calculator_server.py

4) Run tests if you wish to verify functionality.

python -m pytest

Additional setup and usage notes

If you prefer to expose the server over HTTP for remote clients, you can start the server with a streamable-http transport and specify a host and port.

python calculator_server.py --transport streamable-http --host 127.0.0.1 --port 8000

You can bind to all interfaces to allow external access, but be aware of security implications and ensure you understand DNS rebinding protections.

python calculator_server.py --transport streamable-http --host 0.0.0.0 --port 8080

Example host/port combinations to tailor the setup to your environment include binding to a specific IP or localhost with a different port.

Claude Desktop integration and local testing

To use the calculator with Claude Desktop, configure a MCP server entry that runs the calculator server in stdio mode inside a virtual environment.

{
  "mcpServers": {
    "calculator": {
      "command": "uv",
      "args": [
        "--directory",
        "<path-to-project>",
        "run",
        "python",
        "calculator_server.py"
      ],
      "env": {
        "UV_PROJECT_ENVIRONMENT": ".venv"
      }
    }
  }
}

Security and practical considerations

If you enable HTTP binding on all interfaces, understand that this exposes the server to external clients. Only use external binding when you trust connected clients and you have appropriate network security controls.

The stdio transport is suitable for local development and testing with MCP Inspector and Claude Desktop when running on the same machine.

Tests and next steps

Run the test suite to verify arithmetic behavior and error handling, such as division by zero, which is handled by the server.

Expand the server by adding more arithmetic tools or prompts to guide user workflows, and explore additional transport options (HTTP, SSE, stdio) for broader client compatibility.

Available tools

add

Add two numbers together with parameters a and b and return the sum.

subtract

Subtract b from a and return the difference.

multiply

Multiply two numbers and return the product.

divide

Divide a by b and return the quotient, with an error on division by zero.