PHP code example of hammrouni / nanoagent

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

    

hammrouni / nanoagent example snippets


    // config.php
    return [
        // API Key
        'api_key' => 'your_api_key_here',
        
        // AI Provider (groq, openai, anthropic, etc.)
        'provider' => '',
        
        // Model to use
        'model' => '',
    ];
    

    // index.php
    $config =        llm: $config
        // ... other parameters
    );
    

use NanoAgent\Tools\FunctionTool;

$calculator = new FunctionTool(
    name: 'calculator',
    description: 'Add two numbers',
    parameters: [
        'type' => 'object',
        'properties' => [
            'a' => ['type' => 'integer', 'description' => 'First number'],
            'b' => ['type' => 'integer', 'description' => 'Second number']
        ],
        '

use NanoAgent\Mcp\McpClient;

$agent = new Agent(llm: [...]);

$mcpClient = new McpClient('https://mcp.deepwiki.com/mcp');
$agent->registerMcpServer($mcpClient);

echo $agent->chat('Ask the facebook/react repo what it does.');

use NanoAgent\Memory\FileMemory;

// Run on every request: a fresh Agent picks up where the last request left off.
$agent = new Agent(llm: [...]);
$agent->setMemory(new FileMemory(__DIR__ . '/storage/memory'), sessionId: "user-$userId");

echo $agent->chat($_POST['message']); // e.g. "What is my name?" after "My name is Ada." last request

$agent->clearHistory(); // wipes the stored copy too

use NanoAgent\Memory\PdoMemory;

$agent->setMemory(PdoMemory::sqlite(__DIR__ . '/memory.sqlite'), $sessionId);
$agent->setMemory(new PdoMemory($existingPdo), $sessionId); // creates `nanoagent_memory` if missing