home / mcp / calculator mcp server
Provides arithmetic tools and a config resource via an MCP server using PHP; supports STDIO transport for local use.
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.
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.
Install the MCP PHP SDK and set up your project environment with Composer.
composer require mcp/sdkConfigure 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.
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);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.
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.
Adds two integers and returns the sum.
Performs arithmetic operations (add, subtract, multiply, divide) based on the requested operation.
Returns configuration settings for the calculator, such as precision and sign allowances.