Skip to main content
Glama

MCP Calculator Server

by RMahshie

MCP Calculator Demo

A demonstration of the Model Context Protocol (MCP) using a simple calculator server that enables large language models to perform mathematical calculations through tool calls.

Overview

This project showcases how to integrate external tools with Large Language Models (LLMs) using the Model Context Protocol (MCP). The demo creates a calculator server that exposes mathematical functions as tools, which can be discovered and used by an LLM through the MCP framework.

What is MCP?

Model Context Protocol (MCP) is an open standard that enables LLMs to securely connect to external data sources and tools. In this demo, we create a calculator server that runs as a separate process and communicates with the LLM through stdin/stdout, allowing the AI to perform mathematical operations it couldn't do natively.

Features

  • Calculator Server: Provides basic mathematical operations (add, subtract, multiply, divide, square, power, square root)
  • LLM Integration: Uses LangChain and LangGraph to orchestrate tool calls
  • Error Handling: Includes validation for mathematical operations (e.g., division by zero, negative square roots)
  • Tool Flow Visualization: Shows the sequence of tool calls made during calculations

Project Structure

MCP-Calculator/ ├── README.md ├── requirements.txt ├── calculator_server.py # MCP server with calculator tools ├── mcp-calculator-demo.ipynb # Jupyter notebook demo └── .env # OpenAI API key (create this)

Prerequisites

  • Python 3.8+
  • OpenAI API key
  • VS Code with Python extension
  • Jupyter extension for VS Code

Installation

  1. Clone the repository
    git clone <repository-url> cd MCP-Calculator
  2. Install dependencies
    pip install -r requirements.txt
  3. Set up environment variables Create a .env file in the project root:
    OPENAI_API_KEY=your_openai_api_key_here
  4. Open in VS Code
    code .

Usage

Running the Demo

  1. Open the demo notebook
    • In VS Code, navigate to mcp-calculator-demo.ipynb in the Explorer panel
    • Click on the file to open it in the notebook editor
  2. Select Python kernel
    • When prompted, select your Python interpreter (the one where you installed the requirements)
    • If not prompted, click on the kernel selector in the top-right of the notebook
  3. Run the cells sequentially
    • Important: First run the cell that starts the calculator server (look for the server initialization code)
    • Use Shift+Enter to run each cell, or
    • Use the "Run All" button in the notebook toolbar
    • Make sure the server is running before attempting calculations

Example Calculations

The demo can handle complex mathematical expressions:

# Examples that work well: "What's the square root of 156.789, plus 47.234, then multiply by 3?" "Calculate 2 to the power of 8, then divide by 4" "What's 25 squared minus 100?"

Understanding the Tool Flow

The notebook includes a visualization function that shows the sequence of tool calls:

TOOL CALL FLOW: 🔧 square_root({'number': 156.789}) → 12.521541438656824 🔧 add_numbers({'a': 12.521541438656824, 'b': 47.234}) → 59.75554143865683 🔧 multiply_numbers({'a': 59.75554143865683, 'b': 3}) → 179.2666243159705

Technical Implementation

Calculator Server (calculator_server.py)

The MCP server is built using FastMCP and exposes mathematical functions as tools:

  • add_numbers(a, b) - Addition
  • subtract_numbers(a, b) - Subtraction
  • multiply_numbers(a, b) - Multiplication
  • divide_numbers(a, b) - Division with zero-check
  • square_number(number) - Squaring
  • power(base, exponent) - Exponentiation
  • square_root(number) - Square root with negative-check

LLM Integration

The demo uses:

  • LangChain: For LLM initialization and tool binding
  • LangGraph: For orchestrating the conversation flow and tool calls
  • MCP Adapters: For connecting to the calculator server

Communication Flow

  1. User provides a mathematical query
  2. LLM analyzes the query and determines needed tools
  3. MCP client discovers available tools from the calculator server
  4. LLM makes sequential tool calls as needed
  5. Results are processed and returned to the user

Key Components

  • MultiServerMCPClient: Manages connections to MCP servers
  • StateGraph: Orchestrates the conversation and tool execution flow
  • ToolNode: Executes the discovered MCP tools
  • Conditional Routing: Determines when to use tools vs. end conversation

Error Handling

The demo includes robust error handling for:

  • Invalid mathematical operations (division by zero, negative square roots)
  • MCP server connection issues
  • Malformed queries
  • Tool execution failures

Cleanup

The notebook includes a cleanup function to properly close MCP connections:

await cleanup() # Call when finished

Contributing

This is a demonstration project. Feel free to extend it by:

  • Adding more mathematical functions
  • Implementing other types of MCP servers
  • Enhancing error handling and validation
  • Adding more complex mathematical operations

License

This project is for educational and demonstration purposes.

Resources

-
security - not tested
F
license - not found
-
quality - not tested

A calculator server that exposes mathematical functions as tools (add, subtract, multiply, divide, square, power, square root), enabling language models to perform calculations through Model Context Protocol (MCP).

  1. Overview
    1. What is MCP?
  2. Features
    1. Project Structure
      1. Prerequisites
        1. Installation
          1. Usage
            1. Running the Demo
            2. Example Calculations
            3. Understanding the Tool Flow
          2. Technical Implementation
            1. Calculator Server (calculator_server.py)
            2. LLM Integration
            3. Communication Flow
          3. Key Components
            1. Error Handling
              1. Cleanup
                1. Contributing
                  1. License
                    1. Resources

                      Related MCP Servers

                      • -
                        security
                        F
                        license
                        -
                        quality
                        A Model Context Protocol (MCP) server that provides mathematical calculations and operations using NumPy, enabling users to perform numerical computations like matrix operations, statistical analysis, and polynomial fitting directly through Claude.
                        Last updated -
                        1
                        Python
                        • Linux
                        • Apple
                      • A
                        security
                        F
                        license
                        A
                        quality
                        A server that provides tools for performing basic arithmetic operations (addition, subtraction, multiplication, division) via the Model Context Protocol.
                        Last updated -
                        4
                        JavaScript
                        • Apple
                      • A
                        security
                        A
                        license
                        A
                        quality
                        A mathematical computation service that enables users to perform symbolic calculations including basic arithmetic, algebra, calculus, equation solving, and matrix operations through the MCP protocol.
                        Last updated -
                        1
                        3
                        Python
                        MIT License
                      • -
                        security
                        F
                        license
                        -
                        quality
                        Provides basic arithmetic operations and advanced mathematical functions through the Model Context Protocol (MCP), with features like calculation history tracking and expression evaluation.
                        Last updated -
                        Python

                      View all related MCP servers

                      MCP directory API

                      We provide all the information about MCP servers via our MCP API.

                      curl -X GET 'https://glama.ai/api/mcp/v1/servers/RMahshie/MCP-Calculator'

                      If you have feedback or need assistance with the MCP directory API, please join our Discord server