# 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.

- URL: https://docs.mentoragent.net/configure/commands-and-queries
- Verified with: MentorAgent 1.5.0
- Last updated: 2026-11-27
- Packages: Novior.MentorAgent, Novior.MentorAgent.Abstractions

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](https://docs.mentoragent.net/concepts/ways-to-declare-actions).

The example is the "Cases" app of the [tutorial](https://docs.mentoragent.net/get-started/tutorial/actions), with its operations written as commands and queries.

## 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:

```csharp title="CaseQueries.cs"
// 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

A command has a handler that implements `IMentorCommandHandler<TCommand>`, or `IMentorCommandHandler<TCommand, TResult>` when the model should read a result:

```csharp title="CaseCommands.cs"
// 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

Register the handlers in the services, then each type in `options.Actions` with its handler:

```csharp title="Program.cs"
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 on `FindCase`.
- **The builder** is the same as in [Register actions in code](https://docs.mentoragent.net/configure/register-actions-in-code#the-builder). `Describe` is required, and `RequireRoles`, `RequireConfirmation`, `Category`, `Off`, `NavigateTo` and `Hint` work the same way.

## 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`](https://docs.mentoragent.net/reference/options#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`](https://docs.mentoragent.net/reference/action-policies#imentoractioncatalog) | `query` | `command` |

The same rules apply to them as to any other action: roles, confirmations in [both modes](https://docs.mentoragent.net/concepts/governance#blocking-and-native-modes), the [governor](https://docs.mentoragent.net/reference/glossary#governor), your [action policies](https://docs.mentoragent.net/reference/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

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

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 of `options.Actions` already 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](https://docs.mentoragent.net/concepts/action-rules).

---

- Previous: [Register actions in code](https://docs.mentoragent.net/configure/register-actions-in-code.md)
- Next: [Scan your pages](https://docs.mentoragent.net/configure/scan-your-pages.md)
