Download the PHP
package hammrouni/nanoagent without Composer
On this page you can find all versions of the php package
hammrouni/nanoagent. It is possible to download/install
these versions without Composer. Possible dependencies are resolved
automatically.
Vendor hammrouni Package nanoagent Short Description NanoAgent is a lightweight library designed to strip away the complexity of modern AI development. License
MIT
FAQ
After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.
Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.
In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories.
In this case some credentials are needed to access such packages.
Please use the auth.json textarea to insert credentials, if a package is coming from a private repository.
You can look here for more information.
Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
To use Composer is sometimes complicated. Especially for beginners.
Composer needs much resources. Sometimes they are not available on a simple webspace.
If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
## Why this exists
Most "AI agent" libraries assume you'll bolt a Python microservice behind your app, manage a fleet of config files, or adopt a whole framework. In existing PHP environments - where your service is already talking to legacy databases, internal APIs, and core business logic - that overhead is the wrong shape. You don't need a sidecar. You need a small, boring, local library you can `composer require` into the PHP you already run, hand it your own functions, and ship.
NanoAgent is that library. It is deliberately **lightweight and local-first**: one Composer package, zero runtime dependencies, no DSL, no framework lock-in, and memory that lives in your own files or database. It points at whichever provider you configure - a hosted API *or* a local model behind a compatible endpoint - so the agent can run right alongside your existing infrastructure.
## What it does
* **⚡ One config array, any provider.** OpenAI, Groq, Anthropic, DeepSeek, OpenRouter - or any OpenAI-compatible endpoint, including a **local** model server. Swap in one line.
* **🛠️ Tool-first architecture.** Map plain PHP functions as tools the agent calls - your database, your APIs, your device telemetry.
* **🔌 MCP support.** Register any Model Context Protocol server (Streamable HTTP); its tools behave exactly like your local ones - and you can allowlist which ones to register.
* **🧠 Persistent memory.** Conversations survive across requests via pluggable drivers: files, SQLite, MySQL, PostgreSQL, or your own.
* **💎 Minimal footprint.** One package, no hidden magic, easy to audit.
## 🚀 Quick Start
### Prerequisites
- **PHP 8.0** or higher (8.4 tested in CI)
- **Composer**
### 1. Install
### 2. Point it at a provider
A minimal agent - note it takes a plain config array, so a **local** model behind an OpenAI-compatible `base_url` works the same way as a hosted one:
Or load the array from a file (see `NanoAgent/config.php.example`):
### 3. Give it a tool
> **Want to run it fully locally?** Point `base_url` at a local OpenAI-compatible server (e.g. `http://localhost:8080/v1`) with `provider => 'openai'`. The agent, your tools, and your memory all stay on the same host - no cloud round-trip.
## 🛠️ Tools & MCP
**MCP:** register every tool a [Model Context Protocol](https://modelcontextprotocol.io) server exposes in one call - they behave exactly like local tools from there on.
Every registered MCP tool ships its full JSON Schema to the model **on every turn**, so a server with 50 tools costs 50 tool definitions per request. Pass an allowlist to register only the ones you need:
**Which names?** Ask the server directly with `McpClient::listTools()` — it returns every tool it exposes, with descriptions, and registers nothing:
Each entry is `['name' => ..., 'description' => ..., 'inputSchema' => [...]]`. Skip the allowlist on a first call to just register everything and read the names back:
Keep the same `$mcpClient` object for both: the MCP handshake is memoized per client, so re-using it skips a re-initialize (the `tools/list` call itself is repeated). Note that there is no `Agent::getTools()` — the return value of `registerMcpServer()` is the only listing of what the agent currently holds.
Calling `registerMcpServer()` again on the same client **reconciles** rather than duplicates: tools you keep are untouched, tools you drop are removed from the agent, and locally registered tools are never affected. A name the server doesn't expose is ignored, not an error - handy when the allowlist comes from config.
## 🧠 Persistent Memory
PHP forgets everything between requests. Attach a memory driver and the agent loads the conversation for a session, then saves it after every completed `chat()` / `stream()` call.
| Driver | Storage | Requires |
| --- | --- | --- |
| `ArrayMemory` | Current process only (tests, workers) | - |
| `FileMemory` | One JSON file per session | - |
| `PdoMemory` | SQL table (SQLite, MySQL, PostgreSQL) | `ext-pdo` + driver |
See the [PdoMemory guide](docs/pdo-memory.md) for MySQL/PostgreSQL connections, table schemas and cleanup.
Need another backend (Redis, a framework cache, Eloquent)? Implement `NanoAgent\Contracts\Memory`: `load()`, `save()`, `clear()`.
## 📂 Examples
The `examples/` directory has runnable demos (open `examples/index.php` in a browser or run any file with `php`):
| Example | Description |
| --- | --- |
| **[Basic Usage](examples/basic.php)** | Agent init, a custom tool, and a task with context. |
| **[Chat](examples/chat.php)** | Stateful conversation loop using PHP sessions. |
| **[Memory Chat](examples/memory_chat.php)** | Persistent history with a pluggable memory driver. |
| **[Structured Output](examples/structured_output.php)** | Extract JSON from unstructured text. |
| **[Knowledge Base](examples/knowledge_base_search.php)** | RAG: ask questions against private documents. |
| **[Streaming](examples/streaming.php)** | Real-time token streaming (SSE). |
| **[Agent Chain](examples/agent_chain.php)** | Multi-agent workflow (Researcher feeds Writer). |
| **[Multi-Tool](examples/multi_tool_workflow.php)** | Orchestrate several tools for one request. |
| **[MCP Tools](examples/mcp_tools.php)** | Discover a remote MCP server's tools and allowlist which ones to register. |
| **[Multi-Provider](examples/multi_provider.php)** | Switch providers programmatically. |
| **[API Integration](examples/api_integration.php)** | Fetch data from external APIs. |
| **[Advanced Tools](examples/advanced_tools.php)** | Multi-step tool use with simulated DB state. |
## Supported Providers
Every provider below is covered by the automated test suite (see the CI badge). Groq has also been verified against the real API.
* Groq
* OpenAI
* OpenRouter
* DeepSeek
* Anthropic - experimental. Built separately from the others, not yet verified against the real API, and streaming isn't fully wired up yet.
* **Any OpenAI-compatible endpoint** - including a **local** model server, via `base_url`.
## Changelog
### 0.6.0
1. MCP allowlist: `registerMcpServer($client, ['tool_a'])` registers only the tools you name. Every registered tool ships its JSON Schema on every turn, so this is how you keep a large server's cost down. Unknown names are ignored, not an error.
2. Calling `registerMcpServer()` again on the same client now reconciles instead of duplicating: tools dropped from the allowlist are removed from the agent, kept ones are untouched, and locally registered tools are never affected.
### 0.5.0
1. Persistent memory: `Agent::setMemory()` keeps conversations across requests, with `ArrayMemory`, `FileMemory` and `PdoMemory` (SQLite, MySQL, PostgreSQL) drivers, or your own via `NanoAgent\Contracts\Memory`. See the [PdoMemory guide](docs/pdo-memory.md).
2. Clarified in the README that all providers are covered by the mocked unit test suite.
### 0.4.0
1. MCP (Model Context Protocol) client support: connect to any Streamable HTTP MCP server and register its tools with `registerMcpServer()`.
### 0.3.0
1. Inject a custom provider directly into `Agent`, no config array needed.
2. Configure `temperature`, `max_tokens`, and a custom `base_url` per provider.
3. Streaming now supports tool calls.
4. More reliable request encoding for non-English text.
### 0.2.0
1. HTTP requests now have timeouts and surface real API errors instead of hanging or failing silently.
2. Tool-calling loops are capped to avoid runaway API usage.
3. Task context no longer leaks between calls.
4. Added static analysis (PHPStan) and a wider PHP version test matrix (8.0–8.4).
### 0.1.0
First public release.
## License
[MIT](LICENSE)
## Author
**Khaled Hammrouni** - Technical PM & Software Architect, working in Industry 4.0 / industrial IoT with local-first AI tooling.
* [Personal site / project docs](https://hammrouni.github.io/nanoagentphp/)
* [More open-source projects](https://github.com/hammrouni)
* [Writing: *Why I Keep Coming Back to PHP (Even for AI Agents)*](https://dev.to/khaled_hammrouni_bc97ab98/why-i-keep-coming-back-to-php-even-for-ai-agents-39g2) · [LinkedIn article](https://www.linkedin.com/pulse/why-i-keep-coming-back-php-even-ai-agents-khaled-hammrouni-dj53f)
Requiresphp Version
^8.0 ext-curl Version
* ext-json Version
*
Composer command for our command line client (download client)This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free.Standard composer command
The package hammrouni/nanoagent contains the following files
Loading the files please wait ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.