PHP code example of elliottlawson / mcp-laravel-sdk

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


use ElliottLawson\LaravelMcp\Facades\Mcp;
use App\Models\User;
use ElliottLawson\LaravelMcp\Tools\CommandTool;
use ElliottLawson\LaravelMcp\Prompts\FilePrompt;

// Register a resource from an Eloquent model
Mcp::resource('users', User::class);

// Register a tool with a schema and handler
Mcp::tool('echo', [
    'type' => 'object',
    'properties' => [
        'message' => [
            'type' => 'string',
            'description' => 'The message to echo'
        ]
    ]
], new CommandTool('echo', [
    'command' => 'echo',
    'args' => ['message']
]));

// Register a prompt
Mcp::prompt('system', 'You are a helpful assistant integrated with Laravel.');
// Or from a file
Mcp::prompt('system', new FilePrompt('system', storage_path('prompts/system.txt')));



// For Laravel 10-11: config/mcp.php
// For Laravel 12: bootstrap/mcp.php

return [
    // Server information
    'name' => env('MCP_SERVER_NAME', 'Laravel MCP Server'),
    'version' => env('MCP_SERVER_VERSION', '1.0.0'),
    
    // HTTP configuration
    'http' => [
        'route_prefix' => 'mcp',
        'middleware' => ['web'],
        'cors' => [
            'allowed_origins' => ['*'],
            'allowed_methods' => ['GET', 'POST', 'OPTIONS'],
            'allowed_headers' => ['Content-Type', 'X-Requested-With'],
        ],
    ],
    
    // SSE configuration
    'sse' => [
        'heartbeat_interval' => 30, // seconds
        'max_execution_time' => 0, // 0 for no limit
        'retry_interval' => 3000, // milliseconds
    ],
    
    // Register resources
    'resources' => [
        'users' => App\Mcp\Resources\UserResource::class,
        // Or register a model directly
        'products' => App\Models\Product::class,
    ],
    
    // Register tools
    'tools' => [
        'calculator' => App\Mcp\Tools\CalculatorTool::class,
    ],
    
    // Register prompts
    'prompts' => [
        'system' => 'You are a helpful assistant integrated with Laravel.',
        // Or reference a file
        'chat' => resource_path('prompts/chat.txt'),
    ],
];

namespace App\Mcp\Resources;

use ElliottLawson\LaravelMcp\Resources\BaseResource;
use ElliottLawson\LaravelMcp\Contracts\ResourceContract;

class CustomResource implements ResourceContract
{
    public function getData(array $params)
    {
        // Implement your resource logic here
        return [
            'data' => [
                // Your resource data
            ],
            'meta' => [
                'total' => 1,
                'per_page' => 15,
                'current_page' => 1,
            ],
        ];
    }
}

namespace App\Mcp\Tools;

use ElliottLawson\LaravelMcp\Tools\BaseTool;
use ElliottLawson\LaravelMcp\Contracts\ToolContract;

class CalculatorTool implements ToolContract
{
    public function execute(array $params)
    {
        $a = $params['a'] ?? 0;
        $b = $params['b'] ?? 0;
        $operation = $params['operation'] ?? 'add';
        
        switch ($operation) {
            case 'add':
                $result = $a + $b;
                break;
            case 'subtract':
                $result = $a - $b;
                break;
            case 'multiply':
                $result = $a * $b;
                break;
            case 'divide':
                $result = $b != 0 ? $a / $b : 'Error: Division by zero';
                break;
            default:
                $result = 'Error: Unknown operation';
        }
        
        return [
            'result' => $result,
            'operation' => $operation,
            'a' => $a,
            'b' => $b,
        ];
    }
}
bash
php artisan vendor:publish --tag=mcp-config
bash
php artisan vendor:publish --tag=mcp-config