mcp-calculator

JRomeoSalazar/mcp-calculator

3.2

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.

Tools
2
Resources
0
Prompts
0

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

  1. Clone the repository:
git clone https://github.com/YOUR_USERNAME/mcp-calculator.git
cd mcp-calculator
  1. 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 number
  • b (integer): The second number

Returns: Integer sum

calculate

Performs basic arithmetic operations.

Parameters:

  • a (number): The first number
  • b (number): The second number
  • operation (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:

  1. Add a public method to the CalculatorElements class in src/Mcp/Calculator/CalculatorElements.php
  2. Decorate with #[McpTool] attribute
  3. Add PHPDoc comments for description and parameters
  4. 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