Used for environment variable management to securely store and access the OpenAI API key.
The calculator demo is implemented as a Jupyter notebook, allowing interactive execution of calculations and visualization of tool call flows.
Used for LLM initialization and tool binding, enabling the calculator server to expose mathematical functions as tools.
Used for orchestrating conversation flow and tool calls in the calculator server, managing the sequence of mathematical operations.
Requires an API key for connecting to OpenAI's models, which are used to interpret mathematical queries and execute the appropriate calculator functions.
The server is implemented in Python, requiring version 3.8+ to run the calculator functionality.
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
Prerequisites
Python 3.8+
OpenAI API key
VS Code with Python extension
Jupyter extension for VS Code
Installation
Clone the repository
git clone <repository-url> cd MCP-CalculatorInstall dependencies
pip install -r requirements.txtSet up environment variables Create a
.env
file in the project root:OPENAI_API_KEY=your_openai_api_key_hereOpen in VS Code
code .
Usage
Running the Demo
Open the demo notebook
In VS Code, navigate to
mcp-calculator-demo.ipynb
in the Explorer panelClick on the file to open it in the notebook editor
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
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:
Understanding the Tool Flow
The notebook includes a visualization function that shows the sequence of tool calls:
Technical Implementation
Calculator Server (calculator_server.py
)
The MCP server is built using FastMCP and exposes mathematical functions as tools:
add_numbers(a, b)
- Additionsubtract_numbers(a, b)
- Subtractionmultiply_numbers(a, b)
- Multiplicationdivide_numbers(a, b)
- Division with zero-checksquare_number(number)
- Squaringpower(base, exponent)
- Exponentiationsquare_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
User provides a mathematical query
LLM analyzes the query and determines needed tools
MCP client discovers available tools from the calculator server
LLM makes sequential tool calls as needed
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:
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
This server cannot be installed
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).
Related MCP Servers
- -securityFlicense-qualityA 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 -2
- AsecurityFlicenseAqualityA server that provides tools for performing basic arithmetic operations (addition, subtraction, multiplication, division) via the Model Context Protocol.Last updated -4
- AsecurityAlicenseAqualityA 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 -14MIT License
- -securityFlicense-qualityProvides basic arithmetic operations and advanced mathematical functions through the Model Context Protocol (MCP), with features like calculation history tracking and expression evaluation.Last updated -