Calculator MCP Server

Calculator MCP Server

Provides basic calculator operations (add, subtract, multiply, divide) as MCP tools for use with Claude Desktop and other MCP clients.

Category
Visit Server

README

Calculator MCP Server

A simple Model Context Protocol (MCP) server that provides basic calculator operations as tools for Claude Desktop and other MCP clients.

Features

  • Add: Sum two numbers
  • Subtract: Find the difference between two numbers
  • Multiply: Calculate the product of two numbers
  • Divide: Divide numbers with proper zero-division handling

All operations support integers, floating-point numbers, and negative numbers.

Installation

Prerequisites

  • Python 3.10 or higher
  • pip (Python package manager)

Steps

  1. Clone or download this repository
git clone https://github.com/snedea/calc-mcp-server.git
cd calc-mcp-server
  1. Install dependencies
pip install -r requirements.txt

This installs:

  • fastmcp>=2.0.0 - MCP framework
  • pytest>=7.0.0 - Testing framework

Usage

Testing the Server

Run the server standalone to verify it works:

python3 calc_server.py

You should see:

🧮 Calculator MCP Server Starting

šŸ“‹ Available tools:
   - add(a, b): Add two numbers
   - subtract(a, b): Subtract b from a
   - multiply(a, b): Multiply two numbers
   - divide(a, b): Divide a by b (handles zero)

šŸ’” Configure in Claude Desktop to use these tools!

Press Ctrl+C to stop the server.

Configuring in Claude Desktop

To use these calculator tools in Claude Desktop:

  1. Locate your Claude Desktop config file:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json
  2. Edit the config file (create it if it doesn't exist):

{
  "mcpServers": {
    "calculator": {
      "command": "python3",
      "args": ["/absolute/path/to/calc-mcp-server/calc_server.py"]
    }
  }
}

Important: Replace /absolute/path/to/calc-mcp-server/ with the actual full path to this directory.

  1. Restart Claude Desktop

The calculator tools will now be available! Claude can use them automatically when you ask questions involving calculations.

Available Tools

add(a, b)

Add two numbers together.

Parameters:

  • a (float): First number
  • b (float): Second number

Returns: JSON with operation details and result

Example:

Input: add(5, 3)
Output: {
  "operation": "add",
  "a": 5,
  "b": 3,
  "result": 8
}

subtract(a, b)

Subtract the second number from the first.

Parameters:

  • a (float): Number to subtract from
  • b (float): Number to subtract

Returns: JSON with operation details and result

Example:

Input: subtract(10, 3)
Output: {
  "operation": "subtract",
  "a": 10,
  "b": 3,
  "result": 7
}

multiply(a, b)

Multiply two numbers together.

Parameters:

  • a (float): First number
  • b (float): Second number

Returns: JSON with operation details and result

Example:

Input: multiply(4, 5)
Output: {
  "operation": "multiply",
  "a": 4,
  "b": 5,
  "result": 20
}

divide(a, b)

Divide the first number by the second.

Parameters:

  • a (float): Numerator
  • b (float): Denominator

Returns: JSON with operation details and result, or error if dividing by zero

Example:

Input: divide(10, 2)
Output: {
  "operation": "divide",
  "a": 10,
  "b": 2,
  "result": 5
}

Input: divide(10, 0)
Output: {
  "error": "Cannot divide by zero"
}

Testing

Run the test suite to verify all operations work correctly:

pytest tests/test_calculator.py -v

Expected output:

tests/test_calculator.py::TestAddition::test_add_positive_numbers PASSED
tests/test_calculator.py::TestAddition::test_add_negative_numbers PASSED
... (30+ tests)
================================ 30 passed in 0.XX s ================================

All tests should pass, covering:

  • Basic arithmetic operations
  • Edge cases (zero, negatives, decimals)
  • Error handling (division by zero)
  • JSON response format validation

Project Structure

calc-mcp-server/
ā”œā”€ā”€ calc_server.py          # Main MCP server with calculator tools
ā”œā”€ā”€ requirements.txt        # Python dependencies
ā”œā”€ā”€ tests/
│   └── test_calculator.py  # Comprehensive unit tests
ā”œā”€ā”€ README.md               # This file
└── .gitignore              # Git exclusions

Troubleshooting

Tools don't appear in Claude Desktop

  • āœ… Check that the config file path is correct for your OS
  • āœ… Verify the args path is absolute (not relative)
  • āœ… Ensure Python 3.10+ is installed: python3 --version
  • āœ… Confirm FastMCP is installed: pip show fastmcp
  • āœ… Restart Claude Desktop completely

Import errors when running server

  • āœ… Install dependencies: pip install -r requirements.txt
  • āœ… Use Python 3.10 or higher
  • āœ… Consider using a virtual environment:
    python3 -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    pip install -r requirements.txt
    

Tests failing

  • āœ… Ensure pytest is installed: pip install pytest
  • āœ… Run from the project root directory
  • āœ… Check Python version is 3.10+

Technologies Used

  • Python 3.10+: Modern Python with type hints
  • FastMCP 2.0+: Official MCP framework for Python
  • pytest: Testing framework

How It Works

MCP (Model Context Protocol) is a standard for connecting AI assistants like Claude to external tools and data sources. This server:

  1. Defines calculator operations as Python functions
  2. Decorates them with @mcp.tool() to expose them via MCP
  3. Communicates with Claude Desktop using standard input/output (stdio transport)
  4. Returns structured JSON responses for reliable parsing

Claude can then call these tools automatically when needed for calculations!

License

MIT

Contributing

Contributions welcome! Feel free to:

  • Report bugs
  • Suggest new calculator features
  • Improve documentation
  • Add more tests

Created By

šŸ¤– Generated with Context Foundry - Autonomous Development Platform

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured