A simple calculator that uses OpenAI's function calling to process mathematical expressions and return results in JSON format.
-
Clone this repository:
git clone https://github.com/s2005/openai-calculator.git cd openai-calculator
-
Set up a virtual environment:
# Create virtual environment python3 -m venv venv # Activate virtual environment source venv/bin/activate # For bash/zsh # OR . venv/bin/activate # Alternative method
# Create virtual environment python -m venv venv # Activate virtual environment .\venv\Scripts\activate # For PowerShell # OR venv\Scripts\activate.bat # For Command Prompt
-
Install the requirements:
pip install -r requirements.txt
-
Copy
.env.template
to.env
and add your OpenAI API key:cp .env.template .env # Edit .env and add your OpenAI API key
Note: On Windows, you can copy the file using:
copy .env.template .env
Basic usage:
python calculator.py "2 + 2"
With optional parameters:
python calculator.py --model gpt-4 --temperature 0.2 "23 * 45"
expression
: The mathematical expression to evaluate (required)--model
: OpenAI model to use (default: gpt-3.5-turbo)--temperature
: Temperature for response generation (default: 0.0)
{
"expression": "2 + 2",
"result": 4,
"operation_type": "addition"
}
This server implements Anthropic's Model Context Protocol (MCP) to expose the calculator functionality. It uses the mcp
Python SDK (version 1.9.0 or higher).
The necessary MCP libraries, including the CLI, are part of the project's requirements. If you installed dependencies using pip install -r requirements.txt
, the MCP tools should be available. The specific dependency is mcp[cli]>=1.9.0
.
Ensure your OPENAI_API_KEY
is correctly set in your .env
file.
You can run the server using:
python mcp_calculator_server.py
Alternatively, you can use the MCP CLI for development:
mcp dev mcp_calculator_server.py
The server will register with the name "OpenAICalculatorService".
The server exposes the following tool:
- Name:
evaluate_expression
- Description: Evaluates a mathematical expression using OpenAI's function calling.
- Parameters:
expression
(string, required): The mathematical expression string to evaluate.model
(string, optional, default: 'gpt-3.5-turbo'): The OpenAI model to use (e.g., 'gpt-3.5-turbo', 'gpt-4').temperature
(float, optional, default: 0.0): The sampling temperature for generation (0.0 to 2.0).
- Returns: A dictionary containing the evaluation result. This dictionary will include fields like
expression
,result
,operation_type
, andstatus
. If an error occurs (either in input validation, during the OpenAI call, or if the OpenAI client is not initialized), the dictionary will contain anerror
field and thestatus
will be "failed".
This server is designed to be used with MCP-compatible clients (e.g., an LLM that supports MCP for tool use). For development and inspection, you can explore using the mcp
CLI. For instance, after starting the server, you might use commands like mcp list
or mcp call
(refer to MCP SDK documentation for precise usage).
A typical interaction flow would be:
- An MCP client connects to the "OpenAICalculatorService".
- The client invokes the
evaluate_expression
tool with necessary parameters (e.g.,{"expression": "100 / (5 + 5)"}
). - The server executes the tool using the
process_calculation
logic and returns the JSON result to the client.
Example of a successful result structure:
{
"expression": "100 / (5 + 5)",
"result": 10,
"operation_type": "mixed",
"status": "success"
}
For more details on the Model Context Protocol (MCP) and the design philosophy behind this server's implementation, please refer to docs/llm.txt
.
If an error occurs, the output will be in the following format:
{
"error": "error message",
"expression": "original expression",
"status": "failed"
}
- Python 3.6+
- OpenAI API key
- Required packages (see requirements.txt):
- openai>=1.0.0
- python-dotenv>=0.19.0
- argparse>=1.4.0
- mcp[cli]>=1.9.0
- pydantic>=2.0.0