PHP code example of shaxzodbek-uzb / laravel-mcp-auth

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

    

shaxzodbek-uzb / laravel-mcp-auth example snippets


use App\Mcp\Servers\DemoServer;
use Laravel\Mcp\Facades\Mcp;

Mcp::web('/mcp/demo', DemoServer::class)->middleware('mcp-auth');

use Laravel\Mcp\Facades\Mcp;

// Requires both files:read AND files:write on the access token
Mcp::web('/mcp/files', FileServer::class)
    ->middleware('mcp-auth:files:read,files:write');

// config/mcp-auth.php
'

use Blaze\McpAuth\Facades\McpAuth;

$token = McpAuth::token();           // ?Blaze\McpAuth\ValidatedToken

$token->subject;                     // ?string  — the "sub" claim
$token->scopes;                      // list<string>
$token->audiences;                   // list<string> (canonicalized)
$token->clientId;                    // ?string
$token->issuer;                      // ?string
$token->expiresAt;                   // ?int (unix timestamp)
$token->claims;                      // array<string,mixed> — full claim bag

$token->hasScope('files:write');     // bool
$token->hasAllScopes(['a', 'b']);    // bool
$token->missingScopes(['a', 'b']);   // list<string>
$token->hasAudience('https://api.example.com/mcp/demo'); // bool
$token->isExpired();                 // bool

McpAuth::hasScope('files:read');     // shortcut for the current request

namespace App\Mcp;

use App\Models\User;
use Blaze\McpAuth\Contracts\UserResolver;
use Blaze\McpAuth\ValidatedToken;
use Illuminate\Contracts\Auth\Authenticatable;

class ResolveUserFromToken implements UserResolver
{
    public function resolve(ValidatedToken $token): ?Authenticatable
    {
        return User::firstWhere('idp_subject', $token->subject);
    }
}

// config/mcp-auth.php
'user_resolver' => \App\Mcp\ResolveUserFromToken::class,

// ...or a closure:
'user_resolver' => fn (\Blaze\McpAuth\ValidatedToken $t) =>
    \App\Models\User::firstWhere('idp_subject', $t->subject),
bash
php artisan mcp-auth:install