MentorAgent Docs
Menu

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

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

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.

In questa pagina

The agent calls two kinds of actions. A server action is a method of your application, which runs on the server and returns a result to the model. A UI action is a function that a page declares, which runs in that page and changes what the user sees: it highlights a row, opens a panel, fills a form. This page explains how the model sees each kind, where each one runs, and which to choose.

Server actionUI action
Declared byYour code: [MentorAction] on a methodThe page that is open, through IMentorPageContext, or a client of the hub
RunsIn your applicationIn the page
ChannelsEvery channel: widget, hub, Server-Sent Events (SSE), Agent-to-Agent (A2A) and Model Context Protocol (MCP)Only where there is a page: a Blazor circuit, .NET MAUI Blazor Hybrid, a client of the hub
What the model reads backWhat the method returnsThat the action was done, or why it failed. On the hub, that the action was done as soon as the request is sent, whatever the result
Roles and confirmationIts own, on the attributeNone of its own: a policy can ask for a confirmation

A server action is a method with [MentorAction], in a class that MentorAgent scans at startup. The model reads its name, description and parameters, and reads what it returns:

InvoiceService.cs
// A server action: it runs in your application, on every channel,
// and the model reads what it returns
public class InvoiceService(IInvoiceStore store)
{
    [MentorAction(
        Description = "Gets the status of an invoice",
        ReadOnly = true)]
    public Task<string> GetInvoiceStatusAsync(string number)
        => store.GetStatusAsync(number);
}

It runs in your application, whichever channel the message came from, with the user of the turn. Its roles, its confirmation and the rules by which a method becomes an action are in Governance and Action rules.

A page registers its UI actions when it opens, through IMentorPageContext, and clears them when it closes:

InvoicesPage.razor
@page "/billing/invoices"
@inject IMentorPageContext PageContext
@inject InvoiceSelection Selection
@implements IDisposable

<ul>
    @foreach (var number in _numbers)
    {
        <li class="@(number == Selection.Highlighted ? "highlighted" : null)">
            @number
        </li>
    }
</ul>

@code {
    protected override void OnInitialized()
    {
        // A UI action: it runs in this page, and changes what the user sees
        PageContext.SetPageName("Invoices")
            .RegisterUIActionAsync<string>("highlight_invoice",
                "Highlights an invoice in the list",
                number => InvokeAsync(() =>
                {
                    Selection.Highlight(number!);
                    StateHasChanged();
                }));
        PageContext.SignalReady();
    }

    public void Dispose() => PageContext.Clear("Invoices");
}
  • Call SetPageName first. Everything that the page registers after it belongs to that page. Clear("Invoices") in Dispose() removes only what this page registered. It leaves what the next page registered, if each page calls SetPageName with a name of its own.
  • The handler. RegisterUIAction and RegisterUIActionAsync take a name, a description and a handler. The typed overloads, RegisterUIAction<T> and RegisterUIActionAsync<T>, give the handler a value of type T. They also write the type of the parameter for the model, here string, unless you give your own parameterHint. The untyped overloads give the handler the model's value as an object. That object is a JsonElement, not a number or a string, or null when the model sends no value.
  • Change the page inside InvokeAsync. On Blazor Server and in .NET MAUI Blazor Hybrid, the handler is not called on the page's renderer. Call InvokeAsync to change the page, as the example does. The page's own services, such as InvoiceSelection here, are registered as scoped: one for each circuit.
  • SignalReady() tells navigate_to that the page has registered its actions: see After navigate_to. On WebAssembly it does nothing.
  • UnregisterUIAction(name) removes one action. A name registered twice keeps the last handler.

The model sees a UI action as a tool, like a server action. What it reads depends on how MentorAgent reaches the model, Path A or Path B in Model providers:

  • Path A, options.ChatClient. Each UI action of the page is a tool with its own name. Its description is the action's, followed by the parameter's hint when there is one: Highlights an invoice in the list (parameter: string). Every UI action has one optional argument, parameter: a string, a number or a JSON object. A UI action with the name of another tool is left out, with a warning in the log.
  • Path B, options.Agent. There is one tool, invoke_ui_action, with the action's name and its parameter as arguments, and a description of its own. The model is not told the names or the descriptions of the actions. It can learn a name only from the error that it gets when it calls a name that does not exist. Your action policies are not asked about this tool.

Tool filtering always sends the UI actions of the page.

The page runs in the process of the turn. The turn calls the handler and waits for it, so the model can call one UI action after another, each one done before the next. When the handler returns, the model reads that the action was done. When it throws, the action is Failed for your observers, and the model reads the message of OnException, or the exception's.

A client of the hub: WebAssembly, React, Angular, Vue

Section titled “A client of the hub: WebAssembly, React, Angular, Vue”

The page runs in the browser, or in a native app, and the actions travel on the connection to the hub:

  1. The client sends the page context to the hub with UpdatePageContext. It holds the page's name, its data as text values, and its UI actions. Each action has a name, a description and a parameter hint, all written by the client. The hub keeps the last page context that the connection sent, and offers its actions at each message.
  2. When the model calls one, the hub sends UIActionRequested to the client, with the action's name and its parameter as JSON text, such as "INV-1002" or {"id":42}.
  3. The client runs its handler. The hub does not wait for it: the model reads that the action was done as soon as the request is sent, even if the handler then fails. The server is not told whether the handler succeeded.

The WebAssembly client of Novior.MentorAgent.Blazor does all this by itself: the page registers its actions as on Blazor Server, and the client sends them before each message. A React, Angular, Vue or JavaScript front end sends them itself: see Declare UI actions.

POST /mentor/chat and an A2A task carry text only, with no page. On these channels there is no UI action: on Path A the model is offered none, and on Path B invoke_ui_action answers that no action is available. Only server actions run there.

When the model opens another page with navigate_to, the actions of the new page reach it at different times:

  • On a Blazor circuit and in .NET MAUI Blazor Hybrid, MentorAgent waits for a page described with HasUIActions = true on [MentorPage] until the page calls SignalReady(), for at most ReadyTimeout. The next request of the same turn offers the new page's actions, and no longer those of the page that was left.
  • On the hub, the client sends the new page after the navigation, and its actions reach the model at the user's next message. Until then, the actions of the page that was left are still offered, and the model reads that a call to one was done. The WebAssembly client of Novior.MentorAgent.Blazor ignores a call to an action that the page shown does not have. The examples of Declare UI actions do the same.

Since 1.4.0, on Path A, a UI action goes through the gate of every action:

  • your action policies, and their hook before an action, decide whether it is offered and whether a call runs, with Kind set to MentorActionKind.UI and the page in PageName;
  • your action observers receive its decisions, and it has its span in the traces;
  • Stop keeps a call from starting, but does not reach a handler that is already running;
  • a model that keeps calling actions is stopped by the same limit as your server actions: 40 rounds of tool calls in a turn, which is the default of Microsoft.Extensions.AI.

A UI action has no roles and no confirmation of its own. A policy can refuse it, or ask the user to confirm it.

What the client declares is the client's. On the hub, the name, description and hint of each action, and the page's name, come from the client, and the server does not check them. A policy that reads PageName gets what the client sent. So a UI action should only do what the user may do in that page anyway. What needs a role, a check of your own or a confirmation belongs in a server action.

Recipes, forms and grids are UI actions that do not need a handler of your own:

  • Recipes: a recipe is a fixed sequence of steps on a page, approved outside your code. It has parameters with names and types, and roles and a confirmation of its own. Unlike the other UI actions, the model waits for the browser's answer, also on the hub. A React, Angular, Vue or JavaScript front end runs recipes with /mentor/agent.js.
  • Forms and grids: a Blazor form registered as one action that fills it, and a grid as actions that open a row, sort and filter. They are UI actions like the others, with the same parameter and the same gate, and only for Blazor pages.
  • A server action when the work reads or changes data, needs roles or a confirmation, or must work on every channel, SSE, A2A and MCP included.
  • A UI action when the work belongs to the page the user sees: show, select, scroll, fill. It needs no endpoint of its own, and the model can call it without leaving the conversation.
  • Both for most pages: a server action changes the data, and a UI action shows the result on the page.
  • The three agent levels — Actions, specialists and teams: how the three levels of MentorAgent map to Agent Framework, and when to use each one.
  • 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.
  • Pages the agent knows — With the AutoMentor on, MentorAgent knows the pages of your Blazor app from their routes, and what a scan or your users saw of each one.
  • Any front end: React, Angular, Vue — Build the chat in React, Angular, Vue or a mobile app with a .NET AI agent behind it: SignalR or Server-Sent Events, confirmation over HTTP, stopping an answer.
  • Forms and grids — Register a Blazor form as one UI action that the model fills, and a grid as UI actions that open a row, sort and filter, with sensitive fields left out.
  • Recipes — Give the agent procedures on your pages, approved outside your code: sources, steps, rows of a grid, limits, and how each recipe is governed.