PHP code example of dalehurley / laravel-php-mcp-sdk

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

    

dalehurley / laravel-php-mcp-sdk example snippets


'servers' => [
    'main' => [
        'name' => 'My MCP Server',
        'version' => '1.0.0',
        'transport' => 'stdio',
        'tools' => [
            'discover' => [app_path('Mcp/Tools')],
            'auto_register' => true,
        ],
    ],

    'api' => [
        'name' => 'API Server',
        'transport' => 'http',
        'tools' => [
            'discover' => [app_path('Mcp/Api/Tools')],
            'auto_register' => true,
        ],
    ],
],



namespace App\Mcp\Tools;

use MCP\Laravel\Laravel\LaravelTool;

class CalculatorTool extends LaravelTool
{
    public function name(): string
    {
        return 'calculator';
    }

    public function description(): string
    {
        return 'Performs basic arithmetic operations';
    }

    protected function properties(): array
    {
        return [
            'operation' => ['type' => 'string', 'enum' => ['add', 'subtract', 'multiply', 'divide']],
            'a' => ['type' => 'number'],
            'b' => ['type' => 'number'],
        ];
    }

    protected function 

use MCP\Laravel\Facades\Mcp;
use MCP\Laravel\Facades\McpServer;
use MCP\Laravel\Facades\McpClient;

// Start multiple servers
McpServer::start('main', 'stdio');
McpServer::start('api', 'http');
McpServer::start('websocket-server', 'websocket');

// Add tools dynamically
McpServer::addTool('main', 'weather', function($params) {
    return ['content' => [['type' => 'text', 'text' => 'Sunny, 72°F']]];
});

// Connect clients to different servers
McpClient::connect('local', 'http://localhost:3000');
McpClient::connect('remote', 'https://api.example.com/mcp');

// Call tools on different servers
$result = McpClient::callTool('local', 'calculator', ['operation' => 'add', 'a' => 5, 'b' => 3]);
$weather = McpClient::callTool('remote', 'weather', ['city' => 'New York']);

// Get system overview
$status = Mcp::getSystemStatus();

use MCP\Laravel\Laravel\LaravelTool;

class DataProcessingTool extends LaravelTool
{
    public function handle(array $params): array
    {
        $progress = app(\MCP\Laravel\Utilities\ProgressManager::class);
        $progressToken = $progress->start('Processing data...', 100);

        for ($i = 0; $i < 100; $i++) {
            // Do work
            usleep(50000); // 50ms delay
            $progress->update($progressToken, $i + 1, "Processing item " . ($i + 1));
        }

        $progress->complete($progressToken, 'Processing completed');
        return $this->textContent('Data processing completed successfully');
    }
}

class CancellableTask extends LaravelTool
{
    public function handle(array $params): array
    {
        $cancellation = app(\MCP\Laravel\Utilities\CancellationManager::class);
        $token = $cancellation->createToken('task-' . uniqid());

        for ($i = 0; $i < 1000; $i++) {
            if ($cancellation->isCancelled($token)) {
                return $this->textContent('Task was cancelled');
            }
            // Do work
        }

        return $this->textContent('Task completed');
    }
}

use MCP\Laravel\Laravel\LaravelResource;

class UserResource extends LaravelResource
{
    public function uri(): string
    {
        return 'user://{id}';
    }

    public function read(string $uri): array
    {
        $variables = $this->extractUriVariables($uri);
        $user = User::find($variables['id']);

        return $this->jsonContent($user->toArray());
    }
}

class SecureUserTool extends LaravelTool
{
    public function Scopes(): array
    {
        return ['mcp:tools', 'user:read'];
    }

    public function handle(array $params): array
    {
        $user = $this->user(); // Gets authenticated user
        return $this->textContent("Hello, {$user->name}!");
    }
}

'servers' => [
    'main' => [
        'name' => 'Main Server',
        'transport' => 'stdio',
        'capabilities' => ['tools', 'resources', 'prompts'],
    ],
    'api' => [
        'name' => 'API Server',
        'transport' => 'http',
        'capabilities' => ['tools', 'resources'],
    ],
    'websocket' => [
        'name' => 'WebSocket Server',
        'transport' => 'websocket',
        'capabilities' => ['tools', 'resources', 'prompts', 'roots'],
    ],
],

'transports' => [
    'stdio' => [
        'enabled' => true,
        'buffer_size' => 8192,
    ],
    'http' => [
        'enabled' => true,
        'host' => '127.0.0.1',
        'port' => 3000,
        'security' => [
            'cors_enabled' => true,
            'rate_limiting' => '60,1',
        ],
    ],
    'websocket' => [
        'enabled' => true,
        'host' => '127.0.0.1',
        'port' => 3001,
        'max_connections' => 1000,
    ],
],

'authorization' => [
    'enabled' => true,
    'provider' => 'oauth', // oauth, bearer, api_key
    'oauth' => [
        'scopes' => [
            'mcp:tools' => 'Access to MCP tools',
            'mcp:resources' => 'Access to MCP resources',
            'mcp:prompts' => 'Access to MCP prompts',
        ],
        'pkce_
bash
# This creates app/Mcp/Tools/CalculatorTool.php
bash
# Run all MCP system tests
php artisan mcp:test

# Test specific components
php artisan mcp:test --server=main
php artisan mcp:test --client=main
php artisan mcp:test --url=http://localhost:3000

# Health check
php artisan mcp:test --health

# Verbose output
php artisan mcp:test --verbose