PHP code example of icdev / claude-agent-sdk-php

1. Go to this page and download the library: Download icdev/claude-agent-sdk-php 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/ */

    

icdev / claude-agent-sdk-php example snippets


use ClaudeAgent\Options;
use function ClaudeAgent\query;

foreach (query('¿Cuánto es 17 * 23?', new Options(model: 'claude-opus-5')) as $m) {
    if ($m['type'] === 'assistant') {
        foreach ($m['message']['content'] as $b) {
            if ($b['type'] === 'text') echo $b['text'], "\n";
        }
    }
}

$client = new ClaudeAgent\Client(new Options(model: 'claude-opus-5'));
foreach ($client->query('Recuerda el número 42.') as $m) { /* … */ }
foreach ($client->query('¿Qué número era?')       as $m) { /* … */ }
$client->close();

$srv = (new SdkMcpServer('facturacion'))->tool(
    'saldo_cliente',
    'Devuelve el saldo pendiente de un cliente.',
    ['type' => 'object', 'properties' => ['cliente_id' => ['type' => 'integer']], '

use ClaudeAgent\{Agent, Plugin, Options, Effort, MemoryScope, PermissionMode};
use ClaudeAgent\Sandbox\{Sandbox, Network, Filesystem};

new Options(
    agents: [
        'revisor' => new Agent(
            description: 'Revisa código PHP en busca de bugs.',
            prompt: 'Eres un revisor de PHP. Sé conciso.',
            tools: ['Read', 'Grep', 'Glob'],
            model: 'sonnet',
            effort: Effort::Low,
            memory: MemoryScope::Project,
            permissionMode: PermissionMode::Plan,
        ),
    ],
    plugins: [
        Plugin::local('/ruta/plugin'),
        Plugin::localWithoutMcp('/ruta/otro'), // carga skills/hooks, ignora su .mcp.json
    ],
    sandbox: new Sandbox(
        enabled: true,
        network: new Network(allowedDomains: ['api.anthropic.com'], strictAllowlist: true),
        filesystem: new Filesystem(allowWrite: ['/tmp/work'], denyRead: ['/root/.ssh']),
    ),
);

use ClaudeAgent\Sandbox\{Credentials, CredentialFile, CredentialEnvVar,
    DecodeFormat, NoMatchBehavior, AwsPair, Sigv4, Sigv4Policy};

new Credentials(
    files: [
        CredentialFile::deny('/root/.ssh'),
        CredentialFile::mask('/root/.netrc',
            extract: 'password\s+(\S+)',            // grupo 1 = el secreto
            onExtractNoMatch: NoMatchBehavior::Error, // fail-closed
            maskDuplicates: true),
        CredentialFile::mask('/root/.token.jwt',
            decode: DecodeFormat::Jwt, maskClaims: ['sub', 'email']),
    ],
    envVars: [
        CredentialEnvVar::mask('AWS_SECRET_ACCESS_KEY', injectHosts: ['sts.amazonaws.com']),
        CredentialEnvVar::mask('DATABASE_URL', extract: '://[^:]+:([^@]+)@'),
        CredentialEnvVar::deny('GITHUB_TOKEN'),
    ],
    awsPairs: [new AwsPair('MI_KEY_ID', 'MI_SECRET', 'MI_TOKEN')],
    sigv4: new Sigv4(streaming: Sigv4Policy::Deny, presigned: Sigv4Policy::Passthrough),
);