PHP code example of elliottlawson / mcp-php-sdk

1. Go to this page and download the library: Download elliottlawson/mcp-php-sdk 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/ */

    

elliottlawson / mcp-php-sdk example snippets




lliottLawson\McpPhpSdk\Server\McpServer;
use ElliottLawson\McpPhpSdk\Server\ServerCapabilities;
use ElliottLawson\McpPhpSdk\Transport\StdioTransport;

// Create server capabilities
$capabilities = ServerCapabilities::create()
    ->withResources(true)
    ->withTools(true);

// Create an MCP server
$server = new McpServer([
    'name' => 'Example MCP Server',
    'version' => '1.0.0',
    'capabilities' => $capabilities
]);

// Register a simple echo resource
$server->resource(
    'echo',
    'echo://{message}',
    function (string $uri, array $params) {
        return [
            'contents' => [
                [
                    'uri' => $uri,
                    'text' => "You said: {$params['message']}"
                ]
            ]
        ];
    }
);

// Register a calculator tool
$server->tool(
    'calculator',
    [
        'type' => 'object',
        'properties' => [
            'operation' => ['type' => 'string', 'enum' => ['add', 'subtract', 'multiply', 'divide']],
            'a' => ['type' => 'number'],
            'b' => ['type' => 'number']
        ],
        'rt
try {
    $transport = new StdioTransport();
    $server->connect($transport);
    
    // The server will now run until the process is terminated
} catch (\Throwable $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
    exit(1);
}

$server->resource(
    'name',            // The resource name
    'uri://{pattern}', // The URI pattern (with placeholders)
    function (string $uri, array $params) {
        // Handler function that returns resource content
        return [
            'contents' => [
                [
                    'uri' => $uri,
                    'text' => 'Content goes here'
                    // Can also 

$server->tool(
    'name',          // The tool name
    [                // The parameter schema (JSON Schema format)
        'type' => 'object',
        'properties' => [
            'param1' => ['type' => 'string'],
            'param2' => ['type' => 'number']
        ],
        ']
        ];
    }
);

$server->prompt(
    'name',          // The prompt name
    [                // The parameter schema (JSON Schema format)
        'type' => 'object',
        'properties' => [
            'param1' => ['type' => 'string'],
            'param2' => ['type' => 'number']
        ],
        'ut: {$params['param1']}"]
            ],
            'description' => 'A helpful prompt about ' . $params['param1']
        ];
    }
);
bash
composer 
bash
php examples/basic-server.php
bash
php -S localhost:8080 examples/http-server.php