PHP code example of polygonlab / mcp-php-kit

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();

$server = Server::builder('tools', '1.0.0')
    ->withTool('greet', 'Greet someone.', fn(string $name): string => "Hello, {$name}!")
    ->withTool(
        'search',
        'Search the catalogue.',
        fn(string $query, int $limit = 10): array => $catalogue->search($query, $limit),
        inputSchema: [
            'type' => 'object',
            'properties' => [
                'query' => ['type' => 'string', 'minLength' => 2],
                'limit' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 50],
            ],
            '

use PolygonLab\McpKit\Auth\BearerTokenAuthenticator;

$server = Server::builder('calculator', '1.0.0')
    ->withToolsFrom(new CalculatorTools())
    ->withAuthenticator(new BearerTokenAuthenticator([(string) getenv('MCP_TOKEN')]))
    ->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