Questa pagina non è ancora tradotta in italiano: qui la trovi in inglese.
Commands and queries as actions
Offer your commands and queries to the model as actions: queries read, commands write and ask for a confirmation, through their handlers.
In questa pagina
Use this page when your application already has commands, which change data, and queries, which only read data, each with its own handler. MentorAgent offers them to the model as actions. A query reads, and asks for no confirmation unless you ask for one. A command writes, and asks the user to confirm. The other ways to declare an action are compared in Ways to declare actions.
The example is the "Cases" app of the tutorial, with its operations written as commands and queries.
The queries
Section titled “The queries”A query is a type of its own, usually a record. Its handler is a class that implements IMentorQueryHandler<TQuery, TResult> and does the work. The model reads what the handler returns:
// A query only reads: the model reads what its handler returns
public sealed record FindCase(
[property: Description("The case number, for example C-1")]
string CaseNumber);
public sealed class FindCaseHandler(CaseStore store)
: IMentorQueryHandler<FindCase, SupportCase?>
{
public Task<SupportCase?> HandleAsync(FindCase query, CancellationToken ct)
=> Task.FromResult(store.Find(query.CaseNumber));
}
public sealed record ListOpenCases;
public sealed class ListOpenCasesHandler(CaseStore store)
: IMentorQueryHandler<ListOpenCases, IReadOnlyList<SupportCase>>
{
public Task<IReadOnlyList<SupportCase>> HandleAsync(
ListOpenCases query, CancellationToken ct)
=> Task.FromResult(store.List("Open"));
}The commands
Section titled “The commands”A command has a handler that implements IMentorCommandHandler<TCommand>, or IMentorCommandHandler<TCommand, TResult> when the model should read a result:
// A command changes data: it asks for a confirmation by default
public sealed record CloseCase(string CaseNumber, string Resolution);
public sealed class CloseCaseHandler(CaseStore store)
: IMentorCommandHandler<CloseCase>
{
public Task HandleAsync(CloseCase command, CancellationToken ct)
{
store.Close(command.CaseNumber, command.Resolution);
return Task.CompletedTask;
}
}
// A command with a result: the model reads it
public sealed record AddCaseNote(string CaseNumber, string Text);
public sealed class AddCaseNoteHandler(CaseStore store)
: IMentorCommandHandler<AddCaseNote, string>
{
public Task<string> HandleAsync(AddCaseNote command, CancellationToken ct)
{
store.AddNote(command.CaseNumber, command.Text);
return Task.FromResult($"Note added to case {command.CaseNumber}.");
}
}Register them
Section titled “Register them”Register the handlers in the services, then each type in options.Actions with its handler:
builder.Services.AddSingleton<CaseStore>();
builder.Services.AddScoped<FindCaseHandler>();
builder.Services.AddScoped<ListOpenCasesHandler>();
builder.Services.AddScoped<CloseCaseHandler>();
builder.Services.AddScoped<AddCaseNoteHandler>();
builder.Services.AddMentorAgent(options =>
{
options.ScanAssemblies = [typeof(Program).Assembly];
options.Actions
.AddQuery<FindCase, FindCaseHandler>(action => action
.Describe("Gets a support case: its status, "
+ "priority and notes"))
.AddQuery<ListOpenCases, ListOpenCasesHandler>(
action => action.Describe("Lists the open cases"))
.AddCommand<AddCaseNote, AddCaseNoteHandler>(action => action
.Describe("Adds a note to a support case")
.RequireRoles("Agents", "Supervisors"))
.AddCommand<CloseCase, CloseCaseHandler>(action => action
.Describe("Closes a support case with a resolution")
.RequireRoles("Supervisors"));
});- The name is the name of the type in snake case:
find_case,list_open_cases,add_case_note,close_case.Name(…)sets another name. - The parameters are the type's public properties. The model gives them as the action's arguments, and MentorAgent builds the command or the query from them and passes it to
HandleAsync. If the arguments cannot build a command or a query, the call fails, and the model reads why. A[property: Description("…")]on a property is the parameter's description, as onFindCase. - The builder is the same as in Register actions in code.
Describeis required, andRequireRoles,RequireConfirmation,Category,Off,NavigateToandHintwork the same way.
Queries read, commands write
Section titled “Queries read, commands write”| Query | Command | |
|---|---|---|
| Read-only | Always | Never: ReadOnly() on a command stops the application at startup |
| Confirmation | None by default; RequireConfirmation() asks for one | By default, from RequireConfirmationForWrites, unless the command declares its own with RequireConfirmation(false) |
| What the model reads | What the handler returns | The result of IMentorCommandHandler<TCommand, TResult>, or that the command was done |
Origin in IMentorActionCatalog | query | command |
The same rules apply to them as to any other action: roles, confirmations in both modes, the governor, your action policies, the Model Context Protocol (MCP) server, the Agent-to-Agent (A2A) card, and the members of a team that name the action. In the example, close_case is offered to the Supervisors only, and add_case_note to the Agents and the Supervisors: both ask for a confirmation, because they are commands.
With a dispatcher
Section titled “With a dispatcher”MentorAgent needs no dispatcher, a library that sends each command to its handler: it calls your handlers itself. If your application sends its commands with a library such as MediatR, write a handler that passes the command to the library's sender, so that the library's pipeline still runs.
Mistakes caught at startup
Section titled “Mistakes caught at startup”A mistake stops the application when it starts, with a message that names the type:
- a type that is not a class, a record or a struct, such as a string, a number or a collection;
- a handler that is not a concrete class, or does not handle the type, or handles it twice;
- a registration without
Describe; - a command declared
ReadOnly(); - a name that is not valid for a tool (letters, digits,
_and-, up to 64 characters), or that another registration ofoptions.Actionsalready uses; - the same type registered twice.
The rules are fluent.command, fluent.query, fluent.message-checked and fluent.message-trace in ACTION-RULES.md: see Action rules.
Vedi anche
Sezione “Vedi anche”- Ways to declare actions — Attributes, registration in code, commands and queries, endpoints: the ways to make your code an action, when to choose each, and what they all share.
- Register actions in code — Make the methods of a class actions from Program.cs, without attributes: options.Actions, the builder, roles and confirmation, the mistakes caught at startup.
- Ruoli, conferma e governance — Ruoli e approvazione human-in-the-loop per i tool di un agente AI in .NET: controlli nel codice, non nel prompt, su ogni canale, e cosa resta a te.
- Configuration options — Every MentorOptions property with its type, default and effect, grouped by area, plus the options of the WebAssembly client and the YAML loader.
