home / mcp / calculator mcp server

Calculator MCP Server

Provides arithmetic tools and a config resource via an MCP server using PHP; supports STDIO transport for local use.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "modelcontextprotocol-php-sdk": {
      "command": "php",
      "args": [
        "/absolute/path/to/your/server.php"
      ]
    }
  }
}

You can build and run a PHP-based MCP server that exposes tools and resources to an MCP client. This server enables you to define capabilities like arithmetic tools and configuration resources, discover them automatically, and communicate with clients over a local STDIO transport or HTTP transport if you choose to adapt it for web-based use.

How to use

Define your MCP elements as PHP classes using attributes to declare tools and resources. You can implement simple tools that perform calculations or operations and expose resources that return data when requested. Create a server script that builds the MCP server, chooses a transport, and runs the server so your MCP client can discover and execute the available tools and read resources.

How to install

Install the MCP PHP SDK and set up your project environment with Composer.

composer require mcp/sdk

Configuration and usage notes

Configure your server to expose tools and resources, then run the server using a local STDIO transport for command-line integration. If you prefer HTTP-based communication, you can adapt a compatible HTTP transport setup.

Example: You define a CalculatorElements class with MCP tools and a resource, and create a server script that builds and runs the server with a STDIO transport.

Example: complete STDIO MCP server setup

Here is a concrete pattern you can follow to implement a basic calculator MCP server that runs locally and communicates via STDIO.

#!/usr/bin/env php
<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;

$server = Server::builder()
    ->setServerInfo('Calculator Server', '1.0.0')
    ->setDiscovery(__DIR__, ['.'])
    ->build();

$transport = new StdioTransport();

$server->run($transport);

How to test your server

Run the server script locally and use an MCP inspector or client to verify the available tools and resources. You can exercise tools like add and calculate and read a resource such as the calculator settings.

Troubleshooting and notes

Ensure your PHP environment has the required extensions and that Composer dependencies are installed. If you switch to HTTP transport, ensure your web server can handle the MCP protocol via the chosen transport adapter.

Available tools

add

Adds two integers and returns the sum.

calculate

Performs arithmetic operations (add, subtract, multiply, divide) based on the requested operation.

getSettings

Returns configuration settings for the calculator, such as precision and sign allowances.