AI tools of a module
How a module describes its actions for the AI assistant through CapabilityRegistry and ToolRegistry: permissions, argument schema, confirmation of risky calls, and what happens without the FluteAi module.
By the end, the assistant can read your module's data and change it on behalf of an administrator, with permission checks and confirmation of risky actions. The assistant itself is the FluteAi module, sold by subscription, but a module can register capabilities at any time: without FluteAi they stay available through the REST API.
Minimal example
The Example module shows player reports. The example capability returns open reports and closes one. The permissions are the ones from module permissions: admin.example.view to read, admin.example to write.
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Assistant;
use Flute\Core\Ai\Capabilities\Attributes\Capability;
use Flute\Core\Ai\Capabilities\Attributes\Method;
use Flute\Core\Ai\Capabilities\Support\Act;
use Flute\Modules\Example\database\Entities\Report;
use Flute\Modules\Example\Exceptions\ReportRefused;
use Flute\Modules\Example\Services\Reports;
#[Capability(
'example',
summary: 'Player reports: list the open ones, close one with a reason',
terms: 'complaint abuse ticket',
guide: 'Call open() first and close only the report the owner named; repeat the reason back.',
)]
final class ExampleCapability
{
#[Method(
permission: 'admin.example.view',
summary: 'open(limit = 20) — open reports: id, author, text, created (ISO, UTC)',
)]
public function open(int $limit = 20): array
{
return app(Reports::class)->open(max(1, min($limit, 50)));
}
#[Method(
permission: 'admin.example',
summary: 'close(id, reason) — close a report; its author gets the reason',
writes: Report::class,
approve: true,
)]
public function close(int $id, string $reason): array
{
$report = Act::run(
static fn(): Report => app(Reports::class)->close(Act::by(), $id, $reason),
[ReportRefused::class],
);
return ['closed' => $report->id, 'undo' => 'none'];
}
}<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Providers;
use DI\Container;
use Flute\Core\Ai\Capabilities\CapabilityRegistry;
use Flute\Core\Support\ModuleServiceProvider;
use Flute\Modules\Example\Assistant\ExampleCapability;
final class ExampleProvider extends ModuleServiceProvider
{
public function register(Container $container): void
{
}
public function boot(Container $container): void
{
$this->bootstrapModule();
$container->get(CapabilityRegistry::class)->register(ExampleCapability::class);
}
}The assistant gets two tools: example_open (a read) and example_close (a write that always asks for confirmation). The Wipes, GiveCore and BattlePass modules build their capabilities the same way.
How it works
Registration happens in boot(), by class name. The assistant also runs from the queue and cron, and a provider's boot() runs in every context. The object is created only on first use, so requests without the assistant pay nothing for the registration. The class is created with new and no arguments: take dependencies inside methods through app(), as the Wipes module does.
A capability is the set of public methods of the class marked with #[Method]. A method without the attribute and a static method are invisible to everyone. A method with writes: is a write, one without it is a read.
FluteAi names the tool <id>_<method in snake_case>, builds the argument schema from parameter types (int → integer, float → number, bool → boolean, array, anything else string), and treats parameters without a default that are not nullable as required. The model reads the summary as the description, so write the signature with defaults and the answer fields into it.
Every call checks the permission from permission:. A list means "any one of them", null lets in any admin user, and admin.boss always passes.
The method runs on behalf of the caller: Act::by() returns that user. The model reads an InvalidArgumentException as a refusal and can fix its arguments. Turn your own refusal exception into one with Act::run($work, [ReportRefused::class]).
FluteAi decides who confirms a write. Reads run at once. A write asks for confirmation in the mode with confirmations and runs by itself in the mode without them.
approve: true asks in every mode: put it on money, permissions, commands to a live server and deletions. Writes with delete, remove, ban, revoke or reset in the name, as well as publish and activate, always ask in FluteAi, even without approve.
Without FluteAi
Without FluteAi, capabilities stay registered and work through the REST API: POST /api/v1/actions/{capability}/{method} with the body {"args":{…}}. An API key gets the actions.example:read scope for methods without writes: and actions.example:write for the rest, and the call runs with the key's permissions. Through the API, a method with approve: true requires an Idempotency-Key header, and such calls go into the audit log. How to issue a key is described in REST API.
Tools from ToolRegistry::later() are never built without FluteAi: only the assistant reads the registry. So later() costs a site without a subscription nothing.
Reference
| Parameter | Type | Default | Meaning |
|---|---|---|---|
#[Capability] id | string | — | the capability name, global. One taken by the core or another module throws |
summary | string | '' | what the capability does, in English |
terms | string | '' | English synonyms for tool search; the model never sees them |
guide | string | '' | how to do the usual jobs: call order, what to ask first. The model gets it with the first tool |
#[Method] permission | string, list, null | null | the permission for the call |
summary | string | '' | the method description for the model |
writes | string, null | null | the entity class the method changes, or Change::FILE, Change::BALANCE and others. null means a read |
undo | bool | false | the change can be reverted from the journal; the method returns a Change |
approve | bool | false | a human confirms every call |
tool | bool | true | false when your own tool already covers this method |
cost | int | 1 | how many units of the write budget a call spends |
CapabilityRegistry::register(object|string $capability): void takes a class name or a ready object. ToolRegistry::later(\Closure $contribute): void defers registering your own tools until the registry is first read.
When a tool needs a card in the chat, a change preview or a check of the site's state, write a class implementing Flute\Core\Ai\Tools\ToolInterface and register it in boot(): $container->get(ToolRegistry::class)->later(static fn(ToolRegistry $tools) => $tools->register(new ExampleReportTool())). If a capability has a method that does the same, give it tool: false, otherwise the model gets two identical tools.
| Method | What to return |
|---|---|
name(): string | the tool name, example_report |
title(): string | the card title in the admin |
kind(): string | 'read' or 'write' |
permission(): string|array|null | the permission, as in #[Method] |
description(): string | the description for the model |
schema(): array, required(): array | JSON schema properties of the arguments and the list of required ones |
display(array $args): string | a short line for the card, example.report(812) |
execute(array $args): string|array | text for the model, or an array of text, diff, widget, images, sources |
Optional interfaces from Flute\Core\Ai\Tools: ToolGuardInterface::guard(array $args): ?string refuses a call based on the site's state, PreviewInterface::preview(array $args): ?string shows a diff on the confirmation card, AlwaysApproveInterface::requiresApproval(array $args): bool asks for confirmation in every mode.
Common mistakes
| What you see | Cause |
|---|---|
InvalidArgumentException: …ExampleCapability is not marked with #[Capability]. | the class has no attribute, or the import is wrong |
InvalidArgumentException: Capability 'example' is already registered. | register() was called twice, or the id is taken |
InvalidArgumentException: AI tool 'example_report' is already registered. | the tool name clashes with another one, including a tool built from a capability method |
Permission 'admin.example' is required for example.close(). | the user or the API key lacks the permission |
example.close() writes data and this run is read-only. | a write was called in read-only mode |
Capability 'example' has no method 'list'; it has: open, close. | the model or an API client called a method without #[Method] |
| the assistant does not see the capability from the queue | the registration sits in an admin package or in bootRequest() instead of boot() |