1. Go to this page and download the library: Download polygonlab/mcp-php-kit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
polygonlab / mcp-php-kit example snippets
declare(strict_types=1);
Request;
use PolygonLab\McpKit\Server;
$server = Server::builder('minimal-example', '1.0.0')
->withTool('echo', 'Return the given text unchanged.', fn(string $text): string => $text)
->withTool('add', 'Add two numbers.', fn(float $a, float $b): array => ['sum' => $a + $b])
->build();
$server->handle(HttpRequest::fromGlobals())->send();
use PolygonLab\McpKit\Attribute\Param;
use PolygonLab\McpKit\Attribute\Tool;
use PolygonLab\McpKit\Exception\ToolError;
final class CalculatorTools
{
#[Tool(name: 'divide', description: 'Divide a number by another one.')]
public function divide(
#[Param(description: 'Number to divide')]
float $dividend,
#[Param(description: 'Number to divide by, not zero')]
float $divisor,
): array {
if ($divisor === 0.0) {
throw new ToolError('Cannot divide by zero: choose another divisor.');
}
return ['quotient' => $dividend / $divisor];
}
#[Tool(name: 'round_number', description: 'Round a number to a given number of decimals.')]
public function roundNumber(
float $value,
#[Param(description: 'Number of decimals', minimum: 0, maximum: 10)]
int $decimals = 2,
): string {
return number_format(round($value, $decimals), $decimals, '.', '');
}
}
$server = Server::builder('calculator', '1.0.0')
->withToolsFrom(new CalculatorTools())
->build();
use PolygonLab\McpKit\Auth\RequireAll;
use PolygonLab\McpKit\Auth\SharedSecretAuthenticator;
// Only requests relayed by the Worker are accepted.
$authenticator = new SharedSecretAuthenticator((string) getenv('ORIGIN_SECRET'));
// Worker secret and bearer token (the Worker then relays Authorization, see below).
$authenticator = new RequireAll(
new SharedSecretAuthenticator((string) getenv('ORIGIN_SECRET')),
new BearerTokenAuthenticator([(string) getenv('MCP_TOKEN')]),
);
bash
composer
bash
php -S 127.0.0.1:8080 index.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.