mcpField
M
MCP Calculator Demo

MCP Calculator Demo

Local
github.com/pijush2022Developer Tools0Updated 78w ago

About MCP Calculator Demo

MCP Calculator Demo is a Python demonstration of a calculator server and client for arithmetic and expression evaluation. It combines direct operations with parenthesized expressions and RPN evaluation, communicating over SSE.

Key features

  1. 01Addition, subtraction, multiplication, and division
  2. 02Evaluate complex expressions
  3. 03Support parentheses
  4. 04RPN evaluation
  5. 05Handle invalid expressions and division by zero

Use cases

  1. 01Demonstrate an MCP calculator service
  2. 02Evaluate arithmetic expressions through a client
  3. 03Test expression parsing and error handling

MCP support

The FastMCP server exposes arithmetic operations to MCP clients. Its included client communicates with the server over Server-Sent Events (SSE).

Configuration

Local server config is the same for every client. Copy it into your client’s MCP config file.

No install method listed yet

We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.

Repository

Tools

No tool list yet

Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.

Details

From the project README

MCP Calculator Demo

A demonstration project showcasing a calculator service using the MCP (Model Control Protocol) framework. This project consists of a server that provides basic arithmetic operations and a client that can evaluate both simple and complex mathematical expressions.

Project Structure

  • calculator_server.py: The MCP server that provides arithmetic operations
  • client.py: The client application that connects to the server and processes user input
  • requirements.txt: Project dependencies

Features

  • Basic arithmetic operations (addition, subtraction, multiplication, division)
  • Support for both simple operations (e.g., "add 2 3") and complex expressions
  • Error handling for division by zero
  • Server-Sent Events (SSE) communication between client and server
  • Support for parentheses in expressions
  • RPN (Reverse Polish Notation) evaluation for complex expressions

Prerequisites

  • Python 3.7+
  • pip (Python package manager)

Installation

  1. Clone the repository
  2. Create and activate a virtual environment:
    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install dependencies:
    pip install -r requirements.txt
    

Usage

Starting the Server

Run the calculator server:

python calculator_server.py

The server will start on http://127.0.0.1:5005 by default.

Running the Client

In a separate terminal, run the client:

python client.py
Available Operations

The calculator supports the following operations:

  1. Direct tool syntax:

    • add 2 3
    • subtract 5 2
    • multiply 4 3
    • divide 10 2
  2. Complex expressions:

    • 5+7-2
    • 5+7-2*3
    • (5+7)*2
    • 10/2+3*4
Example Usage
» add 2 3
Result: 5

» 5+7-2*3
Result: 6

» (5+7)*2
Result: 24

» divide 10 0
Error: Division by zero.

Technical Details

  • The server uses FastMCP for handling MCP operations
  • Communication between client and server is done via Server-Sent Events (SSE)
  • The client implements the Shunting-Yard algorithm for parsing complex expressions
  • Error handling is implemented for invalid expressions and division by zero

Dependencies

  • fastapi
  • uvicorn
  • sse-starlette
  • fastmcp>=0.1.0
  • llama-index
  • llama-index-llms-anthropic
  • anthropic
  • python-dotenv
  • requests
  • sseclient-py
  • uv

License

This project is open source and available under the MIT License.

Comments

Markdown supported

New to MCP?

From what the protocol is to installing your first server, these answers cover most first questions.

An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.