MCP-server-with-client-demo-for-calculator-with-SSE-

MCP-server-with-client-demo-for-calculator-with-SSE-

3.2

If you are the rightful owner of MCP-server-with-client-demo-for-calculator-with-SSE- and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcpreview.com.

A demonstration project showcasing a calculator service using the MCP (Model Control Protocol) framework.

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.

Related MCP Servers

View all mathgpt servers →