PHP code example of everrest / context-gate

1. Go to this page and download the library: Download everrest/context-gate 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/ */

    

everrest / context-gate example snippets


// config/context-gate.php
'enabled' => true,
'tools' => [
    'routes.list' => [
        'enabled' => true,
        'class' => ContextGate\Server\Sources\RoutesListTool::class,
        'contract' => ContextGate\Source\Pseudo\RoutesContract::class,
        'gate' => 'routes.list.allow',
        'fingerprint' => '<output of php artisan gate:fingerprint routes.list>',
    ],
],
'roles' => [
    'support' => ['tools' => ['routes.list'], 'ceiling' => 'internal'],
],
'stdio_roles' => [
    'alice' => 'support',
],

// a service provider
Gate::define('routes.list.allow', fn (ContextGate\Server\Caller $caller) => true);

'classification' => [
    'customers' => [
        'id' => 'personal',
        'country' => 'internal',
        'lifetime_value' => 'personal',
        'created_at' => 'internal',
        'notes' => 'internal',
        'email' => 'personal',
        'password' => 'secret',
    ],
    // keep the five source_* tables of the stock file when you add your own
],

namespace App\Gate;

use App\Models\Customer;
use ContextGate\Contract\ContextContract;
use ContextGate\Contract\Field;

final class CustomerContract extends ContextContract
{
    public function model(): string
    {
        return Customer::class;
    }

    public function fields(): array
    {
        return [
            'id' => Field::pseudonym('CUS'),                       // personal: a stand-in for this record
            'country' => Field::plain(),                           // internal
            'lifetime_value' => Field::bucket([100, 1000, 10000]), // personal: a band, never the amount
            'created_at' => Field::date('month'),                  // internal: "2026-03"
            'notes' => Field::text(),                              // internal, user-written: redacted and quarantined
        ];
    }
}

public function schema(): array
{
    return [
        'type' => 'object',
        'properties' => ['id' => ['type' => 'string', 'pattern' => '^CUS_[0-9a-f]{10,64}$', 'description' => 'Customer pseudonym']],
        '

'canaries' => ['values' => [env('CONTEXT_GATE_CANARY_EMAIL'), env('CONTEXT_GATE_CANARY_TOKEN')]],

'tools' => [
    'customers.lookup' => [
        'enabled' => true,
        'class' => App\Mcp\CustomerLookupTool::class,
        'gate' => 'customers.lookup.allow',
        'fingerprint' => '<output of php artisan gate:fingerprint customers.lookup>',
        'contract' => App\Gate\CustomerContract::class,
    ],
],
'roles' => [
    'support' => ['tools' => ['customers.lookup'], 'ceiling' => 'internal'],
],
'stdio_roles' => [
    'alice' => 'support',   // stdio: OS user name. HTTP tokens take their role at issuance: gate:token --role=support
],

  Gate::define('customers.lookup.allow', fn (ContextGate\Server\Caller $caller, array $arguments) => $caller->id === 'token:12');
  

use ContextGate\Contracts\AuthorizesTools;
use ContextGate\Contracts\RedactsText;
use ContextGate\Server\Caller;

final class OrderNumberRedactor implements RedactsText
{
    public function redact(string $text): string
    {
        return preg_replace('/\bORD-\d{6}\b/', '[order]', $text) ?? '';
    }
}

final class OnCallOnly implements AuthorizesTools
{
    public function allows(string $tool, Caller $caller, array $arguments): bool
    {
        return $tool === 'customers.lookup' && $caller->id === 'token:12'; // everything else: false
    }
}

// in a service provider's register()
$this->app->bind(RedactsText::class, OrderNumberRedactor::class);
$this->app->bind(AuthorizesTools::class, OnCallOnly::class);

namespace App\Mcp;

use App\Models\Customer;
use ContextGate\Server\Tool;
use Laravel\Mcp\Response;

final class CustomerLookupTool extends Tool
{
    public function name(): string
    {
        return 'customers.lookup';
    }

    public function description(): string
    {
        return 'Returns one customer by pseudonym: country, lifetime value band, signup month and notes.';
    }

    public function schema(): array
    {
        return [
            'type' => 'object',
            'properties' => ['id' => ['type' => 'string', 'pattern' => '^CUS_[0-9a-f]{10,64}$', 'description' => 'Customer pseudonym']],
            '
bash
composer gate:install                    # publishes config/context-gate.php: enabled=false, tools=[]
php artisan migrate                         # token tables
php artisan gate:check                      # must exit 0 before the gate serves traffic
bash
php artisan gate:fingerprint routes.list    # paste the output into the entry
php artisan gate:check
php artisan gate:preview routes.list        # what alice would receive, plus dropped fields and reasons
bash
php artisan gate:token --holder="Example On-call" --issuer="Example Operator" --role=support --expires=30d
php artisan gate:token --list [--json]
php artisan gate:token --revoke=<id>