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

- URL: https://docs.mentoragent.net/concepts/ways-to-declare-actions
- Verified with: MentorAgent 1.5.0
- Last updated: 2026-11-27
- Packages: Novior.MentorAgent, Novior.MentorAgent.Abstractions, Novior.MentorAgent.Server

An [action](https://docs.mentoragent.net/concepts/agent-levels#level-1-actions) is a piece of your code that the model can call. You declare it in one of four ways: an attribute on a method, a registration in `Program.cs`, a command or a query with its handler, or an endpoint that your application already has. The attribute is the oldest way. The other three came in 1.4.0. This page compares them, says when to choose each one, and says what holds for all of them.

The actions that a page runs in the browser are another kind: see [Server actions and UI actions](https://docs.mentoragent.net/concepts/server-and-ui-actions).

## The four ways

| Way | Where you declare it | The action's name | Choose it when |
|---|---|---|---|
| **Attribute** | `[MentorAction]` on a method of a class in `ScanAssemblies` | The method's, in snake case | The class is yours and may depend on MentorAgent. The simplest way, used in the [Quickstart](https://docs.mentoragent.net/get-started/quickstart) and the tutorial |
| **Registration in code** | `options.Actions.Add<T>(method, …)` in `Program.cs` | The method's, in snake case | The class comes from a package, or its team does not want MentorAgent in it |
| **Command or query** | `options.Actions.AddCommand` and `AddQuery`, with a handler | The type's, in snake case | Your application already sends commands and queries to handlers |
| **Endpoint** | `WithMentorAction` on a minimal API, or `[MentorAction]` on a controller's action, with `Novior.MentorAgent.Server` | The name given to `WithMentorAction(name, …)` or `WithName(…)`. Without one, the handler method's name in snake case | The operation is already an endpoint, with its authorization, filters and validation |

In a registration in code, `Name(…)` gives the action another name.

### An attribute

```csharp title="CaseService.cs"
public class CaseService(ICaseStore store)
{
    [MentorAction(
        Description = "Gets the status of a support case",
        ReadOnly = true)]
    public Task<string> GetCaseStatusAsync(string caseNumber)
        => store.GetStatusAsync(caseNumber);
}
```

### A registration in code

The same rules, written in `Program.cs` for a class that carries no attribute:

```csharp title="Program.cs"
builder.Services.AddScoped<StockActions>();
builder.Services.AddMentorAgent(options =>
{
    options.ScanAssemblies = [typeof(Program).Assembly];
    options.Actions
        .Add<StockActions>(nameof(StockActions.CountLowStock),
            action => action
                .Describe("Counts the products low on stock")
                .ReadOnly())
        .Add<StockActions>(nameof(StockActions.Restock),
            action => action
                .Describe("Restocks a product")
                .RequireRoles("Manager")
                .RequireConfirmation());
});

// From a package: no MentorAgent attribute on it
public sealed class StockActions(StockStore store)
{
    public int CountLowStock() => store.CountBelow(10);

    public string Restock(int productId, int quantity) =>
        store.Add(productId, quantity);
}
```

The guide is [Register actions in code](https://docs.mentoragent.net/configure/register-actions-in-code).

### A command or a query

A command or a query is a type of its own, usually a record. Your application registers a handler for it:

```csharp title="Program.cs"
builder.Services.AddScoped<CancelOrderHandler>();
builder.Services.AddScoped<FindOrderHandler>();
builder.Services.AddMentorAgent(options =>
{
    options.ScanAssemblies = [typeof(Program).Assembly];
    options.Actions
        .AddCommand<CancelOrder, CancelOrderHandler>(
            action => action
                .Describe("Cancels an order that has not shipped")
                .RequireRoles("Manager"))
        .AddQuery<FindOrder, FindOrderHandler>(
            action => action.Describe("Reads one order"));
});

public sealed record CancelOrder(int OrderId, string Reason);

public sealed class CancelOrderHandler(OrderStore store)
    : IMentorCommandHandler<CancelOrder>
{
    public Task HandleAsync(CancelOrder command, CancellationToken ct)
        => store.CancelAsync(command.OrderId, command.Reason, ct);
}

public sealed record FindOrder(int OrderId);

public sealed class FindOrderHandler(OrderStore store)
    : IMentorQueryHandler<FindOrder, OrderView?>
{
    public Task<OrderView?> HandleAsync(
        FindOrder query, CancellationToken ct)
        => Task.FromResult(store.Find(query.OrderId));
}
```

The guide is [Commands and queries as actions](https://docs.mentoragent.net/configure/commands-and-queries).

### An endpoint

An endpoint that your application already has becomes an action that runs the endpoint itself, as the user of the turn:

```csharp title="Program.cs"
builder.Services.AddAuthorization(o => o.AddPolicy("Billing",
    p => p.RequireRole("Billing")));
builder.Services.AddMentorAgentServer();

// after var app = builder.Build();
app.MapPost("/invoices/{id:int}/void",
        (int id, VoidRequest body, InvoiceStore store) =>
            store.Void(id, body.Reason))
    .RequireAuthorization("Billing")
    .WithMentorAction("void_invoice", action => action
        .Describe("Voids an invoice that is not paid"));

public sealed record VoidRequest(string Reason);
```

- **The endpoint's checks first**: its authorization, then its filters, its binding and its validation. The middleware before routing, such as rate limiting and Cross-Origin Resource Sharing (CORS), does not run for these calls.
- **The roles come from its authorization**, and the verb decides the confirmation: `GET` and `HEAD` read, the other verbs write.
- **The model fills in the request**: the route values, the query values and one JSON body. An answer of 400 or more is a failed action.
- **Not published**: these actions are not on the [Model Context Protocol (MCP) server](https://docs.mentoragent.net/configure/mcp-server), and not on the Agent-to-Agent (A2A) agent card.

## What holds for all of them

Whatever the way, an action is one of the coordinator's actions:

- **Roles and confirmation.** The gate checks them. When the action does not declare its own confirmation, the default for writes applies: [`RequireConfirmationForWrites`](https://docs.mentoragent.net/reference/options#requireconfirmationforwrites). A query and a `GET` endpoint read. A command and the other verbs write. See [Governance](https://docs.mentoragent.net/concepts/governance).
- **Policies and observers.** Your [action policies](https://docs.mentoragent.net/reference/action-policies) and your [governor](https://docs.mentoragent.net/reference/glossary#governor) decide about the action, and your observers receive its decisions.
- **MCP and A2A.** The action reaches the MCP server and the A2A agent card, as the actions of an attribute do. Endpoints are the exception.
- **Mistakes stop the application at startup.** The message names the method, the type or the endpoint. For example: a method that is not there, a registration without a description, the same action declared twice, or a command declared read-only. One method is an action by its attribute or by its registration, never both.

The rules of each way are areas of `ACTION-RULES.md`: see [Action rules](https://docs.mentoragent.net/concepts/action-rules).

## Where each action came from

`IMentorActionCatalog` says how each action was declared, in `Origin`, one of the `MentorActionOrigins`:

| `Origin` | Declared by |
|---|---|
| `attribute` | `[MentorAction]` or `[Description]` on a method in `ScanAssemblies` |
| `fluent` | `options.Actions.Add` |
| `command` | `options.Actions.AddCommand` |
| `query` | `options.Actions.AddQuery` |
| `endpoint` | `WithMentorAction` on a minimal API, or `[MentorAction]` on a controller's action |
| `candidate` | A public method of a service in the namespaces that the [AutoMentor](https://docs.mentoragent.net/release-notes/1-4-0#candidates-from-your-services) names. It is off until a governor turns it on |
| `page` | The UI actions that a page declared: see [Server actions and UI actions](https://docs.mentoragent.net/concepts/server-and-ui-actions) |

`GetRegistrations()` lists one registration for each origin and class, with the actions it gave and, in `Rejected`, what a rule left out. See [`IMentorActionCatalog`](https://docs.mentoragent.net/reference/action-policies#imentoractioncatalog).

---

- Previous: [How methods become actions](https://docs.mentoragent.net/concepts/action-rules.md)
- Next: [Pages the agent knows](https://docs.mentoragent.net/concepts/pages.md)
