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
- 01Addition, subtraction, multiplication, and division
- 02Evaluate complex expressions
- 03Support parentheses
- 04RPN evaluation
- 05Handle invalid expressions and division by zero
Use cases
- 01Demonstrate an MCP calculator service
- 02Evaluate arithmetic expressions through a client
- 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.
We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.
Tools
Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.
Details
From the project READMEMCP 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 operationsclient.py: The client application that connects to the server and processes user inputrequirements.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
- Clone the repository
- Create and activate a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate - 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:
-
Direct tool syntax:
add 2 3subtract 5 2multiply 4 3divide 10 2
-
Complex expressions:
5+7-25+7-2*3(5+7)*210/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
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.