MentorAgent Docs
Menu

Questa pagina non è ancora tradotta in italiano: qui la trovi in inglese.

ConcettoPacchetti: Novior.MentorAgent, Novior.MentorAgent.Abstractions, Novior.MentorAgent.Server

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.

In questa pagina

An action 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.

WayWhere you declare itThe action's nameChoose it when
Attribute[MentorAction] on a method of a class in ScanAssembliesThe method's, in snake caseThe class is yours and may depend on MentorAgent. The simplest way, used in the Quickstart and the tutorial
Registration in codeoptions.Actions.Add<T>(method, …) in Program.csThe method's, in snake caseThe class comes from a package, or its team does not want MentorAgent in it
Command or queryoptions.Actions.AddCommand and AddQuery, with a handlerThe type's, in snake caseYour application already sends commands and queries to handlers
EndpointWithMentorAction on a minimal API, or [MentorAction] on a controller's action, with Novior.MentorAgent.ServerThe name given to WithMentorAction(name, …) or WithName(…). Without one, the handler method's name in snake caseThe operation is already an endpoint, with its authorization, filters and validation

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

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);
}

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

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.

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

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.

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

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, and not on the Agent-to-Agent (A2A) agent card.

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. A query and a GET endpoint read. A command and the other verbs write. See Governance.
  • Policies and observers. Your action policies and your 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.

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

OriginDeclared by
attribute[MentorAction] or [Description] on a method in ScanAssemblies
fluentoptions.Actions.Add
commandoptions.Actions.AddCommand
queryoptions.Actions.AddQuery
endpointWithMentorAction on a minimal API, or [MentorAction] on a controller's action
candidateA public method of a service in the namespaces that the AutoMentor names. It is off until a governor turns it on
pageThe UI actions that a page declared: see Server actions 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.

  • The three agent levels — Actions, specialists and teams: how the three levels of MentorAgent map to Agent Framework, and when to use each one.
  • How methods become actions — The rules by which MentorAgent turns your methods into actions, names them, turns them on or off, adds up roles and confirmations, and publishes them.
  • 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.
  • Server actions and UI actions — Server actions run in your application, UI actions in the page that declares them: how the model sees each kind, where it runs, and which to choose.
  • MentorAgent 1.4.0 release notes — What's new in MentorAgent 1.4.0: actions registered in code, commands and queries, endpoints as actions, teams from YAML, and UI actions through the gate.