PHP code example of polaris / mcp

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

    

polaris / mcp example snippets


use Polaris\Mcp\McpPlugin;
use Polaris\OAuth\OAuthPlugin;

$polaris = Polaris::create(new Config(..., plugins: [
    new AuditPlugin(),
    new AdminPlugin(),
    new OAuthPlugin(
        baseUrl: 'https://app.example.com/auth',
        audiences: ['https://app.example.com/auth/mcp'],   // tokens minted for the MCP resource are accepted on every route
        ...
    ),
    new McpPlugin(
        baseUrl: 'https://app.example.com/auth',
        requests: $psr17, responses: $psr17, streams: $psr17,   // PSR-17 factories (Nyholm, Laminas, ...)
    ),
]));

use Polaris\Mcp\Protect\{Jwks, McpProtectedResource, TokenVerifier};

// A remote issuer:
$verifier = TokenVerifier::withJwks(
    issuer: 'https://auth.example.com',
    audiences: ['https://tools.example.com/mcp'],
    jwks: new Jwks('https://auth.example.com/auth/.well-known/jwks.json', $psr18, $psr17, $psr16),
    clock: $clock,
);
// Or local keys: TokenVerifier::withKeys($issuer, $audiences, ['<kid>' => $pem], $clock)
// Or Polaris's own: McpProtectedResource::fromGraph($graph, $resource, $psr17)

$middleware = new McpProtectedResource(
    resource: 'https://tools.example.com/mcp',
    verifier: $verifier,
    responses: $psr17, cache: $psr16, clock: $clock,
    scopes: ['deploy'],          // 
sh
POLARIS_BOOTSTRAP=/path/to/polaris.php POLARIS_MCP_TOKEN=pk_... bin/polaris mcp