JRomeoSalazar/mcp-calculator
If you are the rightful owner of mcp-calculator and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The MCP Calculator Server is a Model Context Protocol server that provides basic arithmetic operations as MCP tools, allowing for easy integration with MCP-compatible clients.
MCP Calculator Server
A Model Context Protocol (MCP) server that provides calculator functionality. This server exposes basic arithmetic operations as MCP tools that can be consumed by any MCP-compatible client.
Features
- Add Tool: Adds two integers together
- Calculate Tool: Performs basic arithmetic operations (add, subtract, multiply, divide)
- Configuration Resource: Provides calculator settings (precision, negative number support)
Requirements
- PHP 8.1 or higher
- Composer
Installation
- Clone the repository:
git clone https://github.com/YOUR_USERNAME/mcp-calculator.git
cd mcp-calculator
- Install dependencies:
composer install
Usage
Run the MCP server:
php calculator.php
The server communicates via stdio using the MCP protocol (JSON-RPC messages).
MCP Tools
add
Adds two numbers together.
Parameters:
a(integer): The first numberb(integer): The second number
Returns: Integer sum
calculate
Performs basic arithmetic operations.
Parameters:
a(number): The first numberb(number): The second numberoperation(string): The operation to perform (add,subtract,multiply,divide)
Returns: Number result or error message
MCP Resources
calculator_config
URI: config://calculator/settings
Provides calculator configuration settings including precision and negative number support.
Architecture
This server uses the mcp/sdk PHP library with attribute-based discovery:
- Methods decorated with
#[McpTool]are automatically exposed as MCP tools - Methods decorated with
#[McpResource]are exposed as MCP resources - PHPDoc comments provide tool descriptions and parameter documentation
Development
Adding New Functions
To add a new calculator tool:
- Add a public method to the
CalculatorElementsclass insrc/Mcp/Calculator/CalculatorElements.php - Decorate with
#[McpTool]attribute - Add PHPDoc comments for description and parameters
- Use type hints for automatic schema generation
Example:
/**
* Calculates the power of a number.
*
* @param float $base The base number
* @param float $exponent The exponent
* @return float The result
*/
#[McpTool]
public function power(float $base, float $exponent): float
{
return pow($base, $exponent);
}
License
MIT
Author
Jorge Romeo